Python并行SSH只获取命令输出

2024-10-01 11:40:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,我希望运行多个并行ssh连接和到设备的命令。我用psshlink来做它。 问题是设备在连接后会返回一些大标题,比如20-30行。 当我使用下面的代码时,打印出来的是命令的结果,但是在顶部还有一个大的标题,它是在登录之后打印出来的。在

hosts = ['XX.XXX.XX.XXX']

client = ParallelSSHClient(hosts, user='XXXX', password='XXXXX')
output = client.run_command('command')

for host in output:
    for line in output[host]['stdout']:
        print line

总之我能得到命令输出吗?在


Tags: in命令clienthost标题foroutputline
2条回答

我不太明白你的意思。 我也在使用pssh,似乎我在使用与您相同的方法来打印命令的输出,请参见以下内容:

client = pssh.ParallelSSHClient(nodes, pool_size=args.batch, timeout=10, num_retries=1)
output = client.run_command(command, sudo=True)
    for node in output:
        for line in output[node]['stdout']:
            print '[{0}]  {1}'.format(node, line)

你能再详细一点吗?也许可以提供一个你运行的命令和你得到的输出的例子?在

检查pssh。
此工具使用多线程,执行速度快。
你可以阅读更多关于它的here。在

相关问题 更多 >