From f3ce07c65f44dd0c93c315a263a38536fbc128d8 Mon Sep 17 00:00:00 2001 From: John Spray Date: Wed, 11 Dec 2013 13:08:51 -0800 Subject: [PATCH] Respect .ssh/config when opening SSH connections This handles that case where your private key is in a non-default location that you're pointing to in ~/.ssh/config. --- teuthology/orchestra/connection.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/teuthology/orchestra/connection.py b/teuthology/orchestra/connection.py index 7db5d7dcd3..b674f84bbb 100644 --- a/teuthology/orchestra/connection.py +++ b/teuthology/orchestra/connection.py @@ -1,5 +1,6 @@ import base64 import paramiko +import os from ..config import config @@ -45,11 +46,27 @@ def connect(user_at_host, host_key=None, keep_alive=False, key=_create_key(keytype, key) ) - # just let the exceptions bubble up to caller - ssh.connect( + connect_args = dict( hostname=host, username=user, - timeout=60, - ) + timeout=60 + ) + + ssh_config_path = os.path.expanduser("~/.ssh/config") + if os.path.exists(ssh_config_path): + ssh_config = paramiko.SSHConfig() + ssh_config.parse(open(ssh_config_path)) + opts = ssh_config.lookup(host) + opts_to_args = { + 'identityfile': 'key_filename', + 'host': 'hostname', + 'user': 'username' + } + for opt_name, arg_name in opts_to_args.items(): + if opt_name in opts: + connect_args[arg_name] = opts[opt_name] + + # just let the exceptions bubble up to caller + ssh.connect(**connect_args) ssh.get_transport().set_keepalive(keep_alive) return ssh -- 2.39.5