0

I am trying to perform SSH from one system to another using paramiko in python

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse', 
password='lol')

using this reference (http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different )

This is the case when we know the password of the system you want to log-in BUT if i want to login to a system where my public-key is copied and i dont know the password. Is there a way to do this

Thanks in advance

3
  • ssh.Client(host=xxxxxx,username='goutham ',key_filename='/root/.ssh/id_rsa') u mean to say that this will do the task?? Commented Aug 16, 2016 at 12:30
  • 2016-08-16 12:40:31.728 27806 INFO paramiko.transport [-] Connected (version 2.0, client OpenSSH_6.6.1p1) 2016-08-16 12:40:32.098 27806 INFO paramiko.transport [-] Authentication (publickey) failed. 2016-08-16 12:40:32.368 27806 INFO paramiko.transport [-] Authentication (publickey) failed. i am getting this error *** AuthenticationException: Authentication failed. Commented Aug 16, 2016 at 12:43
  • ssh.connect(self._get_host_for_server(server_id),username='root',key_filename='/root/.ssh/id_rsa') actually this is what i have tried .. and i got the above error Commented Aug 16, 2016 at 13:02

3 Answers 3

4

SSHClient.connect accepts a kwarg key_filename, which is a path to the local private key file (or files, if given a list of paths). See the docs.

key_filename (str) – the filename, or list of filenames, of optional private key(s) to try for authentication

Usage:

ssh.connect('<hostname>', username='<username>', key_filename='<path/to/openssh-private-key-file>')
Sign up to request clarification or add additional context in comments.

Comments

1

This code should work:

import paramiko

host = "<your-host>"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username='<your-username>', key_filename="/path/to/.ssh/id_rsa" , port=22)

# Just to test a command
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout.readlines():
    print line

client.close()

Here is the documentation of SSHClient.connect()

EDIT : /path/to/.ssh/id_rsa is your private key!

5 Comments

key_filename="/path/to/.ssh/id_rsa this refers to the path of public key in the system we are trying to log-in to am i right??
No, it refers to the path of your private key. Your public key just needs to be in ~/.ssh/authorized_keys on the server...
Ohkk thank you bro will reply back once i have any issue :) thanks alot
Yea B7 temporarily but can i ssh into one system after another using paramiko like A---B---C in a single program??
@user3398900 ok, so I think you can close this question! (and open a new topic for your question about recursive session, it is better to explain what you want to do with your SSH sessions and how is the architecture of network and your devices)
0

Adding the key to a configured SSH agent would make paramiko use it automatically with no changes to your code.

ssh-add <your private key>

Your code will work as is. Alternatively, the private key can be provided programmatically with

key = paramiko.RSAKey.from_private_key_file(<filename>)
SSHClient.connect(pkey=key)

2 Comments

Default named keys, for example id_rsa in ~/.ssh/ are used automatically without having to add them to an agent. Again your code will work as is.
Also, have a look at github.com/pkittenis/parallel-ssh which automates much of this and allows for parallel SSH execution as well.

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.