-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.py
executable file
·41 lines (28 loc) · 963 Bytes
/
ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
"""Perform a ping for a set of hosts."""
import argparse
import subprocess as cmds
from multiprocessing import cpu_count
from multiprocessing.dummy import Pool as ThreadPool
import six
def find(host):
"""Perform a ping for a given host."""
cmd = "ping -w 1 -q " + host
(status, text) = cmds.getstatusoutput(cmd)
return status, host, text
def ping(hosts):
"""Perform a ping for a set of hosts."""
infile = open(hosts, "r")
hosts = [x.strip() for x in infile.readlines()]
pool = ThreadPool(cpu_count() * 4)
results = pool.map(find, hosts)
for result in results:
if result[0] != 0:
six.print_(result[1])
pool.close()
pool.join()
if __name__ == "__main__":
PARSER = argparse.ArgumentParser(description="ping a set of hosts")
PARSER.add_argument("-f", "--file", help="hosts file", required=True)
ARGS = vars(PARSER.parse_args())
ping(ARGS["file"])