diff --git a/lib/jnpr/junos/device.py b/lib/jnpr/junos/device.py index b3ff6dfec..86577e77a 100644 --- a/lib/jnpr/junos/device.py +++ b/lib/jnpr/junos/device.py @@ -819,6 +819,7 @@ def __init__(self, *vargs, **kvargs): self._hostname = 'localhost' self._ssh_private_key_file = None self._ssh_config = None + self._allow_agent = False else: # -------------------------- # making a remote connection @@ -838,6 +839,12 @@ def __init__(self, *vargs, **kvargs): self._ssh_private_key_file = kvargs.get('ssh_private_key_file') self._auth_password = kvargs.get( 'password') or kvargs.get('passwd') + # we want to enable the ssh-agent if-and-only-if we are + # not given a password or an ssh key file. + # in this condition it means we want to query the agent + # for available ssh keys + self._allow_agent = bool((self._auth_password is None) and + (self._ssh_private_key_file is None)) # ----------------------------- # initialize instance variables @@ -904,14 +911,6 @@ def open(self, *vargs, **kvargs): try: ts_start = datetime.datetime.now() - # we want to enable the ssh-agent if-and-only-if we are - # not given a password or an ssh key file. - # in this condition it means we want to query the agent - # for available ssh keys - - allow_agent = bool((self._auth_password is None) and - (self._ssh_private_key_file is None)) - # open connection using ncclient transport self._conn = netconf_ssh.connect( host=self._hostname, @@ -920,7 +919,7 @@ def open(self, *vargs, **kvargs): password=self._auth_password, hostkey_verify=False, key_filename=self._ssh_private_key_file, - allow_agent=allow_agent, + allow_agent=self._allow_agent, ssh_config=self._sshconf_lkup(), device_params={'name': 'junos', 'local': False}) diff --git a/lib/jnpr/junos/utils/scp.py b/lib/jnpr/junos/utils/scp.py index 8a150d6cb..cd7948d09 100644 --- a/lib/jnpr/junos/utils/scp.py +++ b/lib/jnpr/junos/utils/scp.py @@ -88,29 +88,26 @@ def open(self, **scpargs): # use junos._hostname since this will be correct if we are going # through a jumphost. - config = {} - kwargs = {} - ssh_config = getattr(junos, '_sshconf_path') + # Retrieve ProxyCommand and IdentityFile + sock = None + key_file = junos._ssh_private_key_file + ssh_config = junos._sshconf_path if ssh_config: config = paramiko.SSHConfig() config.parse(open(ssh_config)) config = config.lookup(junos._hostname) - sock = None - if config.get("proxycommand"): - sock = paramiko.proxy.ProxyCommand(config.get("proxycommand")) - - if self._junos._ssh_private_key_file is not None: - kwargs['key_filename']=self._junos._ssh_private_key_file + if config.get("proxycommand"): + sock = paramiko.proxy.ProxyCommand(config.get("proxycommand")) + key_file = key_file or config.get("identityfile") self._ssh.connect(hostname=junos._hostname, - port=( - 22, int( - junos._port))[ + port=(22, int(junos._port))[ junos._hostname == 'localhost'], username=junos._auth_user, password=junos._auth_password, - sock=sock, **kwargs - ) + key_filename=key_file, + allow_agent=junos._allow_agent, + sock=sock) return SCPClient(self._ssh.get_transport(), **scpargs) def close(self):