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
If i do join() for threads outside of the separate for loop, but like this
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.
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)