1

I am trying to connect from one server to another server using paramiko. Here is the code:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username='abc', password='password')
stdin, stdout, stderr = ssh.exec_command('ssh xyz@host1 ls')

I am unable to run the above code because of the special character "@". Could someone suggest me how to resolve it.

I am able to run ssh xyz@host1 ls command in putty successfully. But through python code it is not working.

2

The username and host are specified in your ssh.connect so there's no need to do ssh xyz@host1. You can just do:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username='abc', password='password')
stdin, stdout, stderr = ssh.exec_command('ls')

ssh.connect is establishing the connection so you don't need to execute ssh again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.