我在用着Fabric
自动化我的一些工作流程,其中大部分涉及操作 EC2 实例。
我正在寻找一种方法来保持我的.ssh/config
文件是最新的,因为我经常启动和关闭 EC2 实例,如果我可以轻松 ssh 到它们进行调试等,这对我非常有帮助。
我的 SSH 配置文件中的条目如下所示
Host ins_id
Hostname xxxxxxxx.com
User ubuntu
IdentityFile ~/.ssh/kp.pem
目前,我正在做类似以下的事情(利用Fabric
and boto
),坦率地说,这是一种垃圾方法:
def my_cool_spin_up_function(self):
. . .
. . .
ssh_conf = os.path.join(homedir, '.ssh/config')
ssh_info = '\n'.join(['Host %s' % name,
'Hostname %s' % ins.dns_name,
'User %s' % env.user,
'IdentityFile %s' % kp_loc,
'\n'])
w_com = 'echo %s | cat - %s | tee %s > /dev/null' % (ssh_info, ssh_conf, ssh_conf)
local(w_com)
正如您所看到的,每次调用时,这都会保留在我的配置文件之前,这很好,因为 SSH 会获取配置中每个主机的第一部分,但这意味着文件会不断构建。 。 。
我想知道是否有任何Python库允许人们处理.ssh/config
作为一个配置文件,其相关部分可以随时更新。例如,如果你能简单地对待.ssh/config
作为字典并抽象出文件读/写。 。 。
感谢您的任何建议!