I'm trying to write a Python tool that can ping multiple hosts, using multiple threads. I have my code mainly working. Code below...
from threading import Thread
from subprocess import Popen, PIPE
class Ping:
def __init__(self, hosts):
self.hosts = hosts
self.results = dict()
def register_threads(self):
self.threads = list()
for host in self.hosts:
print('registering thread %s' % host)
thread = Thread(target=self.run_command, args=(host,))
self.threads.append(thread)
def start_threads(self):
for thread in self.threads:
thread.start()
def main(self):
self.register_threads()
self.start_threads()
def run_command(self, host):
process = Popen('ping {} -c 50'.format(host), stdout=PIPE, shell=True)
self.results[host] = list()
while True:
line = process.stdout.readline().rstrip()
print(line)
if not line:
print("not line")
break
self.results[host].append(str(line, 'utf-8'))
However, if the host is not reachable I do not get anything returned? i.e the Like so,
>>> from lib.ping import *
>>> ping = Ping(hosts=['1.2.2.2'])
>>> ping.main()
registering thread 1.2.2.2
>>> print(ping.results)
{'1.2.2.2': ['PING 1.2.2.2 (1.2.2.2) 56(84) bytes of data.']}
However, from a ping on another Liunx device I get,
rick@abc:~$ ping 1.2.2.2 -c 5
PING 1.2.2.2 (1.2.2.2) 56(84) bytes of data.
--- 1.2.2.2 ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4004ms
Any ideas of how to get the statistics line, and also know if the host is not reachable.
Thanks.