Below is a complete script I am trying to automate the process of pinging multiple routers and to do that every 2 hrs but I also want the ability to terminate it at any given time.
def start():
for file_name in file_list:
unrechable = []
rechable = []
print("Processing:"+file_name,end="\n")
open_file(file_name, rechable, unrechable)
if len(unrechable) > 0:
print("These IP from " + file_name + " are Unrechable:")
for i in unrechable:
print(i,end="\n")
print("")
else:
print("All IP's are Rechable from " + file_name)
return
'''
'''
def open_file(file_name, rechable, unrechable):
df = pd.read_excel("D:/Network/"+file_name+".xlsx")
col_IP = df.loc[:, "IP"].tolist()
col_name = df.loc[:, "Location"].tolist()
check(col_IP, col_name, rechable, unrechable)
return
'''
'''
def check(col_IP, col_name, rechable, unrechable):
for ip in range(len(col_IP)):
response = os.popen(f"ping {col_IP[ip]} ").read()
if("Request timed out." or "unreachable") in response:
print(response)
unrechable.append(str(col_IP[ip] + " at " + col_name[ip]))
else:
print(response)
rechable.append(str(col_IP[ip] + " at " + col_name[ip]))
return
'''
'''
def main1():
while(True):
start()
print("Goint to Sleep for 2Hrs")
time.sleep(60)
def qu():
while(True):
if(keyboard.is_pressed('q')):
print("exit")
os._exit
'''
'''
file_list = ["ISP" , "NVR"]
if __name__ == '__main__':
p2 = Thread(target=main1)
p3 = Thread(target=qu)
p2.start()
p3.start()
I have created 2 threads here one runs the main script the other looks for keyboard interrupt. But once q is pressed only one of the threads terminates. I later found out it is impossible to terminate both threads at one and I am completely lost at this point
I added a queue to a thread that listens to any keyboard interrupts and once the condition is met adds it to the queue.
def qu1():
while(True):
if keyboard.is_pressed("q"):
qu_main.put("q")
print("added q")
break
else:
pass
return
Next I put my sleep timer in a loop. I wanted my script to execute in 2hr intervals therefore I added a 1s timer and looped it 7200 times.
while(x < 7200):
if(not qu_main.empty()):
if(qu_main.get() == 'q'):
print("ending the Cycle")
return
else:
print("sleep")
x += 1
time.sleep(1)
else:
print("sleep")
x += 1
time.sleep(1)