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!
AddKeysToAgent. Specifies whether keys should be automatically added to a running ssh-agent(1). If this option is set to yes and a key is loaded from a file, the key and its passphrase are added to the agent with the default lifetime, as if by ssh-add(1).
equiv files during host-based authentication. Specifies a file containing a private host key used by SSH. It is possible to have multiple host key files. The default is /etc/ssh/ssh_host_dsa_key , /etc/ssh/ssh_host_ecdsa_key , /etc/ssh/ssh_host_ed25519_key and /etc/ssh/ssh_host_rsa_key for SSH protocol version 2.
What we do for this sort of configuration is maintain a directory of configuration fragments, which can be added/removed as necessary, and then doing something along the lines of:
cat .ssh/config.d/* > .ssh/config
This will append things in lexical order, which means the ordering depends on how you elect to name your files. This makes it very easy to expire old configurations, remove specific items, and otherwise control the config file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With