Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SSH Multi Threading
#1
Hi, guys. I'm currently having issue with multi threading specifically when i try to use SSH to multiple devices at a time. It works if i don't use multi threading. 

So i have list of devices defined and credentials in separate file

def open_ssh_conn(ip):
        username = 'admin'
        password = 'admin'
        try:
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(ip,username = username,password = password,timeout = 6)
            connection = ssh.invoke_shell()
            print ("Connecting to {} using SSH".format(ip))
            connection.send('show version\n')
            time.sleep(3)
            output = connection.recv(5000)
            if 'IOS XR' in output:
                modelseries = 'XR'
                print "host {} is running {}".format(ip,modelseries)
            if 'IOS XE' in output:
                modelseries = 'XE'
                print "host {} is running {}".format(ip,modelseries)
            ssh.close()
        except paramiko.SSHException:
            print "Authenticaion failed, please check credentials and try again"  
        except socket.error:
            print "SSH Connection to %s  failed " % ip



def create_threads():
    threads = []
    host = ['10.10.10.1', '10.10.10.2']
    for ip in host:
        th = threading.Thread(target = open_ssh_conn ,args = (ip,))
        th.start()
        threads.append(th)
    for thr in threads:
        thr.join()

if __name__ == "__main__":
        create_threads()
        print "Exiting the program"
What happens is i can see program connects via SSH at the same time to both devices in the list, but execution of commands within function that is being threaded (open_ssh_conn) is not working properly. it just hangs there. 
If i do join() for threads outside of the separate for loop, but like this

for ip in host:
        th = threading.Thread(target = open_ssh_conn ,args = (ip,))
        th.start()
        threads.append(th)
        th.join()
it works, but it is sequential. It waits for one thread to finish, before executing another one, which doesn't really help, since i can do this without threading. 

i know that ssh command takes take to be entered and then information to be compared (if statement), just don't know where exactly program is failing. 

any help will be appreciated.

Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,813 May-03-2023, 08:22 AM
Last Post: billykid999
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,119 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Need help multi-threading scraping spacedog 2 2,476 Apr-28-2021, 03:48 PM
Last Post: spacedog
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 1,716 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Multi-threading Evil_Patrick 2 24,054 Jul-15-2020, 09:55 AM
Last Post: snippsat
  object base multi threading maboobelahi93 0 1,429 Jan-29-2020, 11:21 AM
Last Post: maboobelahi93
  multi-threading error in minimal script Skaperen 2 4,663 Aug-03-2019, 07:58 PM
Last Post: Skaperen
  Problem with Python, MySQL and Multi-threading queries zagk 1 11,882 Jul-01-2017, 12:15 AM
Last Post: zagk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020