1

I am trying to ssh to a device using paramiko and run some commands using the following code in a virtual environment

import paramiko from getpass import getpass

if name == "main":

hostname = raw_input("Please enter your IP address: ")
username = raw_input("Please enter your username: ")
password = getpass()

s = paramiko.SSHClient()


s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.load_system_host_keys()
s.connect(hostname= hostname, username= username, password= password)
print s  
s.command = 'dir'
(stdin, stdout, stderr) = s.exec_command(command)
for line in stdout.readlines():
    print line 
s.close()

when i try running the above code i get the following error:

File "param.py", line 14, in s.connect(hostname= hostname, username= username, password= password) File "/users/myuser/myvirtualenv/lib/python2.7/site-packages/paramiko/client.py", line 394, in connect look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host) File "/users/myuser/myvirtualenv/lib/python2.7/site-packages/paramiko/client.py", line 649, in _auth raise saved_exception paramiko.ssh_exception.AuthenticationException: Authentication failed.

I can ssh to the device normally using putty. But when i try to do it in python using paramiko, I am getting authentication exception ..i dont really know why.

1
  • AuthenticationException means just that: the credentials you provided are not correct. Try printing hostname, username and password to make sure you are typing the correct ones. Commented Jun 12, 2017 at 13:16

1 Answer 1

2

I'm getting something similar with valid user/password input (as in, I can connect normally over sftp). It works for me when running s.connect(hostname= hostname, username= username, password= password, look_for_keys=False), but obviously it would be ideal if it would authenticate with both a password and an ssh key.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.