栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Paramiko一次创建多个SSH连接

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Paramiko一次创建多个SSH连接

您需要将调用放在单独的线程(或进程中,但这可能会过大),这反过来又要求代码位于函数中(无论如何,这是一个好主意:模块的顶部没有大量代码水平)。

例如:

import sys, os, string, threadingimport paramikocmd = "grep -h 'king' /opt/data/horror_20100810*"outlock = threading.Lock()def workon(host):    ssh = paramiko.SSHClient()    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())    ssh.connect(host, username='xy', password='xy')    stdin, stdout, stderr = ssh.exec_command(cmd)    stdin.write('xyn')    stdin.flush()    with outlock:        print stdout.readlines()def main():    hosts = ['10.10.3.10', '10.10.4.12', '10.10.2.15', ] # etc    threads = []    for h in hosts:        t = threading.Thread(target=workon, args=(h,))        t.start()        threads.append(t)    for t in threads:        t.join()main()

If you had many more than five hosts, I would recommend using instead a
“thread pool” architecture and a queue of work units. But, for just five, it’s
simpler to stick to the “dedicated thread” model (especially since there is no
thread pool in the standard library, so you’d need a third party package like
threadpool… or a lot of
subtle custom pre of your own of course;-).




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639540.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号