我对Paramiko的工作原理相当陌生,但我的主要目标是能够使用Python通过SSH运行自动命令。Python:Paramiko在尝试通过SSH执行命令时挂起
我有以下代码,我试图运行一个简单的ls
命令开始,但由于某种奇怪的原因代码似乎卡住,没有输出或错误消息产生。
import sys
import paramiko as pm
sys.stderr = sys.__stderr__
import os
class AllowAllKeys(pm.MissingHostKeyPolicy):
def missing_host_key(self, client, hostname, key):
return
HOST = '192.168.31.1'
USER = 'admin'
PASSWORD = 'admin'
client = pm.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(pm.AutoAddPolicy())
client.connect(HOST, username=USER, password=PASSWORD)
channel = client.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')
stdin.write('''
ls
exit
''')
print stdout.read()
stdout.close()
stdin.close()
任何帮助,将不胜感激:)
确定'stdin'和'stdout'不倒? – Elazar
@ mrpopo,更好的,你可以使用pexpect模块,而不是paramiko – abhi
我想最初使用pexpect,但我使用的是Windows机器:/ 无法在Windows上导入资源。不用担心,我只会虚拟一个debian客户端并学习pexpect。 – mrpopo