-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping_test.py
More file actions
66 lines (57 loc) · 1.97 KB
/
ping_test.py
File metadata and controls
66 lines (57 loc) · 1.97 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
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
#Dylan Aguirre, 9/5/25
import subprocess
import os
import time
#Extra module needed. pip install netifaces
import netifaces
def user_Input(): #Basic input checking
try:
userInput = input("> ")
userInput = int(userInput)
return userInput
except(KeyboardInterrupt):
raise KeyboardInterrupt
except:
print("Error! Invalid command!")
raise SyntaxError
def retrieve_Gateway():
return str(netifaces.gateways()["default"][2][0])
# output = subprocess.run("ip r",capture_output=True).stdout.decode().split()
# return output[2]
def ping_out(ip:str): #Checks for operating system type and prints returncode result
if(os.name == "nt"): #Windows
output = subprocess.run("ping "+str(ip),capture_output=True)
else: #Linux/Mac
output = subprocess.run(["ping","-c","4",str(ip)],capture_output=True)
if(output.returncode == 0):
print("Successful ping!")
else:
print("Ping failure!")
def main():
#Clear terminal
#Source: https://stackoverflow.com/questions/2084508/clear-the-terminal-in-python
os.system('cls' if os.name == 'nt' else 'clear')
while True:
print("1. Display the default gateway\n\
2. Test Local Connectivity\n\
3. Test Remote Connectivity\n\
4. Test DNS Resolution\n\
5. Exit/quit the script")
#Input checking
userInput = user_Input()
if userInput == 1: #Display the default gateway
print(retrieve_Gateway())
elif userInput == 2: #Test local connectivity
ping_out(retrieve_Gateway())
elif userInput == 3: #Test remote connectivity
ping_out("129.21.3.17")
elif userInput == 4: #Test DNS resolution
ping_out("www.google.com")
elif userInput == 5: #Quit
print("Quitting...")
break
else: #Check for bad command
print("Command not recognized!")
input("\nPress enter to continue... ")
main()