在Python 3.4.3中使用Paramiko运行多个SSH命令

时间:2016-03-21 19:08:06

标签: python ssh command paramiko

我正在尝试在一个盒子上运行多个命令,但第一个命令成功。这似乎与fork有关,但我无法弄清楚如何解决这个问题。非常感谢您的帮助或指导。

我的脚本

import paramiko
#Made in python 3.4.3

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('xxx.xx.xx.xx', port=22, username='domain\myusername', password='mypassword')

stdin, stdout, stderr = ssh.exec_command('vol status -f') #this being the first command works fine
output = stdout.readlines()
print ("\n".join(output))

stdin, stdout, stderr = ssh.exec_command('disk show -n')
output1 = stdout.readlines()
print ("\n".join(output1))
#stdin, stdout, stderr = ssh.exec_command('vol status -s')
#stdin, stdout, stderr = ssh.exec_command('df -Ag')

ssh.close()
input("Press <Enter> to exit ")

输出:

Traceback (most recent call last):
  File "C:\Users\myusername\Desktop\folder_copy\test.py", line 13, in <module>
    stdin, stdout, stderr = ssh.exec_command('disk show -n')
  File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\client.py", line 401, in exec_command
    chan = self._transport.open_session(timeout=timeout)
  File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\transport.py", line 702, in open_session
    timeout=timeout)
  File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\transport.py", line 823, in open_channel
    raise e
  File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\transport.py", line 1726, in run
    ptype, m = self.packetizer.read_message()
  File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\packet.py", line 386, in read_message
    header = self.read_all(self.__block_size_in, check_rekey=True)
  File "C:\Python34\lib\site-packages\paramiko-1.16.0-py3.4.egg\paramiko\packet.py", line 251, in read_all
    raise EOFError()
EOFError

1 个答案:

答案 0 :(得分:0)

如果您认为fork是问题,请尝试使用Transport类的atfork() method