【问题标题】:Paramiko hangs when running on 150+ servers while running simple commandsParamiko 在运行简单命令时在 150 多台服务器上运行时挂起
【发布时间】:2013-04-25 19:54:17
【问题描述】:

我正在尝试使用 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))

请指教,

谢谢!

【问题讨论】:

  • 启用 paramiko 调试日志以查看是否可以从 paramiko 模块中获取更多信息。 logging.getLogger("paramiko").setLevel(logging.DEBUG).

标签: python linux ssh paramiko


【解决方案1】:

可能是特定服务器的问题。尝试输出导致错误的服务器,看看如果仅在该特定服务器上运行脚本,是否可以重新创建问题。

【讨论】:

  • 感谢您的帮助!我尝试在失败的主机上运行命令,命令成功结束...