'Using Python script to check my network devices connectivity
I have written below a Python script to check my network device's connectivity. it is working properly but still, I want to use it with the OOPS feature of python. Using OOPS failed to work. For your reference, I have pasted the code below.
import os
from termcolor import colored
import threading
import time
class Connectivity:
def __init__(self,aph, ip, count):
self.aph = aph
self.ip = ip
self.count = count
def pingPong(self):
rqst = os.popen(f"ping {self.ip} -n 4").read()
if 'Request timed out.' in rqst:
print("Testing Connectivity To AP ---->" + colored(self.aph, 'blue', attrs=['bold']) +
" & IP Address: " + colored([self.ip], 'cyan', attrs=['bold']) + " & This AP count is: "
+ colored(self.count, 'magenta', attrs=['bold']))
print(colored(rqst, 'red', attrs=['bold']))
opt = open("output.text", 'a')
opt.write(str(self.aph) + '\t' + str(self.ip) + '\t' + 'Down' + '\n')
opt.close()
elif 'Destination host unreachable.' in rqst:
print("Testing Connectivity To AP ---->" + colored(self.aph, 'blue', attrs=['bold']) +
" & IP Address: " + colored([self.ip], 'cyan', attrs=['bold']) + " & This AP count is: "
+ colored(self.count, 'magenta', attrs=['bold']))
print(colored(rqst, 'orange', attrs=['bold']))
opt = open("output.text", 'a')
opt.write(str(self.aph) + '\t' + str(self.ip) + '\t' + 'Down' + '\n')
opt.close()
else:
print("Testing Connectivity To AP ---->" + colored(self.aph, 'blue', attrs=['bold']) +
" & IP Address: " + colored([self.ip], 'cyan', attrs=['bold']) + " & This AP count is: "
+ colored(self.count, 'magenta', attrs=['bold']))
print(colored(rqst.upper(), 'green', attrs=['bold']))
opt = open("output.text", 'a')
opt.write(str(self.aph) + '\t' + str(self.ip) + '\t' + 'Up' + '\n')
opt.close()
if __name__ == '__main__':
pingap = Connectivity()
pingap.pingPong()
st = time.perf_counter()
count = 0
filnm = str(input("Please enter file name which content hostname and ip address: "))
with open(filnm, 'r') as rf:
nw = rf.read()
nw1 = nw.splitlines()
thread1 = list()
for ap in nw1:
aph = ap.split()[0]
ip = ap.split()[1]
count += 1
th = threading.Thread(target=pingap.pingPong(), args=(aph, ip, count))
thread1.append(th)
for th in thread1:
th.start()
for th in thread1:
th.join()
with open('output.text', 'r') as see:
wach = see.read()
udps = wach.splitlines()
print("\n")
print("*" * 50)
print("\nDevices are Online \n")
for ud in udps:
udvs = ud.split()[0]
dip = ud.split()[1]
up = ud.split()[2]
if "Up" in up:
print(colored(udvs, 'yellow') + " " + colored(dip, 'cyan') + ' is ' + colored(up, 'green',
attrs=['bold']))
print("\n")
print("*" * 50)
print("\nDevices are Offline \n")
for ud in udps:
ddvs = ud.split()[0]
dip = ud.split()[1]
dwn = ud.split()[2]
if "Down" in dwn:
print(colored(ddvs, 'yellow') + " " + colored(dip, 'cyan') + ' is ' + colored(dwn, 'red',
attrs=['bold']))
et = time.perf_counter()
print(f"Total time execution this script is {round(et - st, 2)} seconds(s)")
I have run this code using OOPS but it is repeating the same IP address, which should not be done. I hope you can understand my query. Here I am not expecting a full solution If possible give me short clue to fix this issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
