Previous details
I need to ssh on a switch to ping different hosts. Earlier I started a thread for each host but turned out to exceed the maximum ssh connection number easily so I created a interactive shell session according to this. But when I ran in parallel it kept hanging in there after sending first command. I have no idea how to fix this.
Simplified code listed below:
import paramiko
import time
from paramiko import SSHClient
from multiprocessing.dummy import Pool
def rping(src, user, passwd, dst):
def command(des):
chan.send('ping -s 64 -t 1500 %s\r\n' % des)
time.sleep(3)
resp = chan.recv(9999)
print resp
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(src, 22, user, passwd, timeout=3)
chan = ssh.invoke_shell()
pool = Pool()
pool.map(command, dst)
pool.close()
pool.join()
if __name__ == '__main__':
print time.ctime()
src = '10.130.1.121'
user = 'user'
passwd = 'password'
dst = ['10.130.1.122', '10.130.1.123', 10.130.1.124'']
rping(src, user, passwd, dst)
print time.ctime()