首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >异步和paramiko用于并发ssh连接

异步和paramiko用于并发ssh连接
EN

Stack Overflow用户
提问于 2022-02-18 06:33:57
回答 2查看 2.3K关注 0票数 1

我正试图加速Paramiko SSH与几个网络设备的连接。我想为此目的使用异步,但我不确定我对它的实现是否正确,因为我没有看到执行时间有任何好处,如果我们不使用它,脚本每次执行大约6s。其想法是,第二个主机启动其SSH连接,而无需等待第一个主机的SSH连接的建立。

下面是我的当前代码,它运行但没有产生任何好处。任何建议,如何使它工作或改进,如果这是可能的,在这里。

代码语言:javascript
运行
复制
import paramiko
import time
import asyncio

async def sshTest(ipaddress,deviceUsername,devicePassword,sshPort): #finalDict
    try:
            print("Performing SSH Connection to the device")
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(ipaddress, username=deviceUsername, password=devicePassword, port=sshPort, look_for_keys=False, allow_agent=False)
            print("Channel established")         
    except Exception as e:
        print(e)       

async def main():
    print("Session 1 \n")
    await sshTest('192.168.255.11','admin','admin','22')
    print("Session 2 \n")
    await sshTest('192.168.254.11','admin','admin','22')

if __name__ == "__main__":
    start = time.time()
    asyncio.run(main())
    end = time.time()
    print("The time of execution of above program is :", end-start)
EN

回答 2

Stack Overflow用户

发布于 2022-03-17 08:18:17

如果要异步执行此任务,请创建任务列表并将每个异步任务添加到该列表中。使用await asyncio.gather(*tasks),您的函数使用I/O绑定异步执行任务。

编辑关于@satman的评论,我添加了一个asyncssh函数,而不是paramiko

代码语言:javascript
运行
复制
#import paramiko
import asyncssh
import time
import asyncio

async def sshTest(ipaddress,deviceUsername,devicePassword,sshPort): #finalDict
    try:
            print("Performing SSH Connection to the device")
            #client = paramiko.SSHClient()
            #client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            #client.connect(ipaddress, username=deviceUsername, password=devicePassword, port=sshPort, look_for_keys=False, allow_agent=False)
            async with  asyncssh.connect(ipaddress, username=deviceUsername, password=devicePassword, known_hosts=None) as conn:
              async with conn.start_sftp_client() as sftp:
                #Here you can enter the commands you want to execute, e.g. import data from a device
                await sftp.get("folder_device", "folder_local", preserve=True, recurse=True)


            print("Channel established")      
    except Exception as e:
        print(e)       


async def main():
    start = time.time()

    ip_list = ['192.168.254.11','192.168.255.11']
    tasks = []
    for ip in ip_list:
        tasks.append(asyncio.create_task(sshTest(ip,'admin','admin','22')))
    await asyncio.gather(*tasks)

    end = time.time()
    print("The time of execution of above program is :", end-start)


if __name__ == "__main__":
    asyncio.run(main())
票数 0
EN

Stack Overflow用户

发布于 2022-07-29 07:56:47

您能像上面建议的那样异步使用paramiko吗?只有一个线程,如果paramiko阻塞,它将不会将控制权转移到另一个任务,我认为。库也需要异步设计,不是吗?因此,如果您想要异步ssh连接,您必须使用异步ssh。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71169287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档