我正在尝试使用 python 的 Paramiko 模块在一堆远程服务器上运行多个命令。
我尝试运行的命令是简单的命令,例如 cat、lspci(带有 grep)以及只有 1 行输出的小脚本。
问题是,如果我提供少量机器(~50),它就可以正常工作。 当我尝试在许多机器上运行脚本时,问题就出现了。
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username='root', password='pass')
transport = ssh.get_transport()
channel = transport.open_session()
stdin, stdout, stderr = ssh.exec_command(cmd)
for line in stdout.readlines():
line = line.strip()
sheet1.write(row_line,0,host,style_cell) # writing to xls file
sheet1.write(row_line,1,line,style_cell) # writing to xls file
while channel.recv_ready():
channel.recv(1024)
ssh.close()
expect:
print stdout
print stderr
这是我得到的标准输出、标准错误:
paramiko.ChannelFile from paramiko.Channel 2 (EOF received) (open) window=2097152
paramiko.Transport at 0xce44c9d0L (cipher aes128-ctr, 128 bits) (active; 2 open channel(s))
请多多指教
谢谢!
最佳答案
可能是特定服务器的问题。尝试输出导致错误的服务器,并查看如果仅在该特定服务器上运行脚本是否可以重现问题。
关于python - 在 150 多个服务器上运行简单命令时,Paramiko 会挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16213743/