framework
framework copied to clipboard
Support sshconfig file in the sshclient
Feature description
When configuring server that have access to multiple other nodes using sshkeys, its mandatory to specify in the .ssh/config file which host is using which indentityfile. These configs can be something as simple as:
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_rsa_old
IdentityFile ~/.ssh/id_ed25519
or specified as:
Host host2
HostName 147.75.80.219
User root
IdentityFile ~/.ssh/id_rsa_internal
Host 147.75.80.219
HostName 147.75.80.219
User root
IdentityFile ~/.ssh/id_rsa_internal
I'd prefer the latter to be documented and used if we were to implement this.
This would require some reconfiguration to the SSHClient. The underlying paramiko client will attempt to use Any “id_rsa”, “id_dsa” or “id_ecdsa” key discoverable in ~/.ssh/ -- src: http://docs.paramiko.org/en/2.1/api/client.html This list does not contain self assigned identityfiles. To fetch these files we could use http://docs.paramiko.org/en/2.1/api/config.html Implementation logic for paramiko:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_config = paramiko.SSHConfig()
user_config_file = os.path.expanduser("~/.ssh/config")
if os.path.exists(user_config_file):
with open(user_config_file, 'r') as f:
ssh_config.parse(f)
host = '147.75.80.219'
client.connect(hostname=host, port=22, username='root', key_filename=ssh_config.lookup(host).get('identityfile'))