-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping_devices.py
More file actions
58 lines (42 loc) · 1.85 KB
/
ping_devices.py
File metadata and controls
58 lines (42 loc) · 1.85 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# importing required libraries
import csv
import sys
import subprocess
from multiprocessing.dummy import Pool as ThreadPool
import subprocess
# open the device list file using the CSV reader for operations.
file = csv.reader(open('devices_list.csv'),delimiter = ',')
# defining an empty device list for updating it from the csv doc.
dev_list= []
# looping over the device list csv to fill the empty created list
for line in file :
dev = line[0]
ip = line[1]
dev = ['%s'%dev,'%s'%ip]
dev_list.append(dev)
# opening a new csv file to write output of the ICMP , writting header only
with open('cpe-status.csv', 'a') as f:
fieldnames = ['device', 'ip_address', 'status']
writer = csv.DictWriter(f, fieldnames=fieldnames, restval='')
writer.writeheader()
#defining the function ,opening the file , testing ICMP using a linux subprocess and writing the result in a row to file.
def get_prompt (dev):
with open('cpe-status.csv', 'a') as f:
fieldnames = ['device', 'ip_address', 'status']
writer = csv.DictWriter(f, fieldnames=fieldnames, restval='')
ping_reply = subprocess.call(['ping', '-c', '3', '-w', '3', '-q', '-n', dev[1]], stdout =subprocess.PIPE)
if ping_reply == 0:
status = "%s is Reachable" % dev[0]
print (status)
writer.writerow({'device': dev[0], 'ip_address': dev[1], 'status': status})
else:
status = "%s is not Reachable" % dev[0]
print (status)
writer.writerow({'device': dev[0], 'ip_address': dev[1], 'status': status})
#threading pools process defined here , using a thread pool of 6 , can be edited to accomodate with your host capabilities
num_threads_str = 6
num_threads = int(num_threads_str)
threads = ThreadPool (num_threads)
results = threads.map ( get_prompt , dev_list )
threads.close()
threads.join()