How to use .ssh/config ProxyCommand in Python venv

Viewed 17

I'm trying to run a script which requires SSH ProxyCommand in my .ssh/config file, this works correctly when my venv isn't activated however when i activate the venv it doesn't seem to pickup the proxy.

Script run correctly without connecting the venv:

darylls@soldier:~/pyez/nornir$ python3 bgp.py 
GR-CR-0_re0 Upstream Providers:
Checking peer COGENT: 10.10.10.10
              COGENT: 10.10.10.10 Uptime: 9:36:43 Is Established

Checking peer COGENT: 10.10.10.10
              COGENT: 10.10.10.10 Uptime: 5w0d 20:40:25 Is Established

Same script run wihin the venv

(.venv) darylls@soldier:~/pyez/nornir$ python3 bgp.py 
Traceback (most recent call last):
  File "/home/darylls/pyez/nornir/.venv/lib/python3.8/site-packages/jnpr/junos/device.py", line 1347, in open
    self._conn = netconf_ssh.connect(
  File "/home/darylls/pyez/nornir/.venv/lib/python3.8/site-packages/ncclient/manager.py", line 168, in connect
    return connect_ssh(*args, **kwds)
  File "/home/darylls/pyez/nornir/.venv/lib/python3.8/site-packages/ncclient/manager.py", line 135, in connect_ssh
    session.connect(*args, **kwds)
  File "/home/darylls/pyez/nornir/.venv/lib/python3.8/site-packages/ncclient/transport/ssh.py", line 362, in connect
    self._auth(username, password, key_filenames, allow_agent, look_for_keys)
  File "/home/darylls/pyez/nornir/.venv/lib/python3.8/site-packages/ncclient/transport/ssh.py", line 464, in _auth
    raise AuthenticationError(repr(saved_exception))
ncclient.transport.errors.AuthenticationError: AuthenticationException('Authentication failed.')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "bgp.py", line 23, in <module>
    device.open()
  File "/home/darylls/pyez/nornir/.venv/lib/python3.8/site-packages/jnpr/junos/device.py", line 1366, in open
    raise EzErrors.ConnectAuthError(self)
jnpr.junos.exception.ConnectAuthError: ConnectAuthError(gr-cr-0)

The currrent Python script is using PyEZ Netconf to connect to a Juniper Router, which reads my .ssh/config file for the proxy command:

  Match host gr-cr-0
   Hostname 10.24.1.1
   ProxyCommand ssh -tA 8.8.8.8 -W%h:%p

I'm obviously not connecting via Google used as an example :)

I have small library of Pyhton PyEZ scripts which are used to run various tasks on Juniper devices, I'm now trying to convert these same PyEZ scripts to run Nornir, specifically the nornir_pyez plugins.

Nornir uses a .yml file similar to Ansible for host management, this looks like:

SRX-1:
    hostname: 172.17.3.201
    groups:
        - lab

SRX-2:
    hostname: 172.17.3.202
    groups:
        - lab

I've also tried various tweaks to this .yml file to read the .ssh/config file however still doesn't connect:

GR-CR-0:
    hostname: 10.24.1.1
    connection_options:
        optional_args:
            ssh_config_file: ~/.ssh/config

Am I missing something obvious here?

0 Answers
Related