各位好 請教各位Python使用多線程sshtunnel問題
先說明場景
20台設備需要先ssh到跳板機在ssh到設備做取資訊
並設定分批一次10台
利用多線程,使用sshtunnel連接跳板機再搭配netmiko連接設備
因為線程結束10台sshtunnel也會隨之關閉
換下一批10台在建立新的sshtunnel
代表20台設備跳板機會有20個session連接
想請教是否有保留第一次sshtunnel建立的10個通道
後續每批的線程可以直接使用這10個通道做後續netmiko連接設備的方法
以下附上簡化版程式碼
from netmiko import ConnectHandler     
from sshtunnel import open_tunnel                                  
import threading
def ssh_telnet():
    with open_tunnel( ##使用sshtunnel建立通道
                    ssh_address_or_host = (sship, sshport),
                    ssh_username = sshuser,
                    ssh_password = sshwd,
                    remote_bind_address=(ip, 22)
                   ) as tunnel:
                    try:
                        session = ConnectHandler()##這裡是使用netmiko連接設備
                        ##這裡執行連接設備的工作##
                        
                        
                                               
                        
if __name__ == '__main__':
    threads = []
    start_time = time.time()
    for i in range(10):
        thread = threading.Thread(target=ssh_telnet,)
        thread.start()
        threads.append(thread)
    
    for thread in threads:
        thread.join()
        
    print('完成')
    os.system("pause")
目前還未找到有相關資料,如有想法或者資訊
先謝謝各位的分享