Python Paramiko SSH run command

8k views Asked by At

I want to run simple command just for test ssh connection. There is an error about stdin/out/err. Code and error mesage is below.

Can someone help me ?

Code

import paramiko
import sys

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('x.x.x.x', port=2222, username='user', password='pass')
stdin, stdout, stderr = client.exec_command('exit')

print stdout
client.close()

Error message

Traceback (most recent call last):
File "C:/Users/EXT03D3912/PycharmProjects/paramiko_ssh1/paramiko_ssh1.py",           line 9, in <module>
stdin, stdout, stderr = client.exec_command('exit')
File "build\bdist.win32\egg\paramiko\client.py", line 343, in exec_command
File "build\bdist.win32\egg\paramiko\channel.py", line 212, in exec_command
File "build\bdist.win32\egg\paramiko\channel.py", line 1081, in     _wait_for_event
EOFError

thanks, haktan

1

There are 1 answers

2
Kavin Eswaramoorthy On

Since you are logged out from the terminal it is giving a EOFError AFAIK you can just close the connection it will automatically close your session there.

Consider the following code

import paramiko
import sys

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('x.x.x.x', port=2222, username='user', password='pass')
stdin, stdout, stderr = client.exec_command('ls')

print stdout
client.close()