python - 在 150 多个服务器上运行简单命令时,Paramiko 会挂起

标签 python linux ssh paramiko

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

相关文章:

python - 从python通过SSH连接Gitlab部署公钥

Python 相对导入找不到包

python - Moviepy 缩放效果需要调整

Python多处理设计

python - 正则表达式匹配可能包含 '' ' '' '' ' 的单引号字符串

c++ - Linux gcc 选择尝试实例化函数应该在的模板

java - Jsch:有没有办法发送控制字符

java - 将 Java 中获取的 TGT 用于 SSH/其他应用程序

linux - 如何从第二行使用 awk '{print $1*Number}' 或告诉他忽略 NaN 值?

linux - 测试没有域名的nginx