I'm new to python and I am trying to accomplish a multithreaded ssh session with paramiko.
def worker:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username='root', password='password')
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.flush()
--> pid = stdout.readline()
def main:
f = open (options.host_file)
ips = f.read().splitlines()
for line in ips:
user,host = line.split("@")
my_thread = threading.Thread(target=worker ,args=(i,)
threads.append(my_thread)
my_thread.setDaemon(True)
my_thread.start()
my_thread.join()
is_running = my_thread.is_alive()
print "Value of IS RUNNING is: ",is_running,"\r"
What I have observed is that if I have pid = stdout.readline() in the worker, the thread runs the ssh and waits for each job before starting the next one, if I exclude this line all thread(s) run at the same time (which is what I would like) and the ssh exits.
I would like to set up a threaded ssh session to different hosts and monitor the threads until the program I am starting with ssh is finished. Since the threads will optimally run for different times, I'd like to restart the ones that end with new parameters.
Also isAlive always returns False for me.
Any assistance is greatly appreciated. Is this possible, or do I have to monitor the PID of the process started on the remote hosts?