I'm using Fabric
to automate some of my workflow, most of which involves manipulating EC2 instances.
I'm looking for a way to keep my .ssh/config
file up-to-date, as I regularly spin up and shutdown EC2 instances, and it's very helpful to me if I can ssh into them easily for debugging and so on.
Entries within my SSH config file look like this
Host ins_id
Hostname xxxxxxxx.com
User ubuntu
IdentityFile ~/.ssh/kp.pem
At the moment, I'm doing something like the following (making use of Fabric
and boto
), which is frankly a rubbish approach:
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)
As you can see, this will just keep prepending to my config file every time it's called, which is fine, because SSH takes the first section for each Host in config, but it means the file builds up and up. . .
I'm wondering if there are any Python libraries that allow one to treat .ssh/config
as a more of a configuration file, whose relevant parts can be updated as and when. For example, it would be brilliant if you could simply treat .ssh/config
as a dictionary and abstract away the file reading/writing. . .
Thanks for any suggestions!