lofidevops
lofidevops

Reputation: 17022

Paramiko cannot open an ssh connection even with load_system_host_keys + WarningPolicy

I am connecting to a remote server with the following code:

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
ssh.connect(
    hostname=settings.HOSTNAME,
    port=settings.PORT,
    username=settings.USERNAME,
)

When I'm on local server A, I can ssh onto the remote from the command line, suggesting it is in known_hosts. And the code works as expected.

On local server B, I can also ssh onto the remote from the command line. But when I try to use the above code I get:

/opt/mysite/virtualenv/lib/python3.5/site-packages/paramiko/client.py:763: UserWarning: Unknown ssh host key for [hostname]:22: b'12345'
  key.get_fingerprint())))

...

  File "/opt/mysite/virtualenv/lib/python3.5/site-packages/paramiko/client.py", line 416, in connect
    look_for_keys, gss_auth, gss_kex, gss_deleg_creds, t.gss_host,
  File "/opt/mysite/virtualenv/lib/python3.5/site-packages/paramiko/client.py", line 702, in _auth
    raise SSHException('No authentication methods available')
paramiko.ssh_exception.SSHException: No authentication methods available

Unlike "SSH - Python with paramiko issue" I am using both load_system_host_keys and WarningPolicy, so I should not need to programatically add a password or key (and I don't need to on local server A).

Is there some system configuration step I've missed?

Upvotes: 1

Views: 5264

Answers (2)

Mauro Matsudo
Mauro Matsudo

Reputation: 25

Try to use the fabric (this is written based on invoke + paramiko) instead of the paramiko and set the following parameters:

con = fabric.Connection('username@hostname' ,connect_kwargs={'password': 'yourpassword', 'allow_agent': False}

If it's keep falling, try to check if your password is still valid and you're not required to change your password.

Upvotes: 2

lofidevops
lofidevops

Reputation: 17022

I tested with the wrong user on local server B. The user running the Python process did not have ssh permissions after all. (Command line ssh failed for that user.) Once I gave it permissions, the connection worked as expected.

Upvotes: 0

Related Questions