Python Paramiko SSH 运行命令 [英] Python Paramiko SSH run command

查看:121
本文介绍了Python Paramiko SSH 运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行简单的命令来测试 ssh 连接.有关于 stdin/out/err 的错误.代码和错误信息如下.

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.

有人可以帮我吗?

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()

错误信息

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

谢谢,客丹

推荐答案

由于您从终端注销,它会给出 EOFError AFAIK,您只需关闭连接,它就会自动关闭您的会话.

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.

考虑下面的代码

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()

这篇关于Python Paramiko SSH 运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆