-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping_test.py
105 lines (70 loc) · 1.98 KB
/
ping_test.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#shabang
#!/usr/bin/python3
"""
Kasey Kiggins
Date: 9/12/2023
"""
#add imports
import subprocess
import platform
import re
# create menu
def menu():
print( "Please select one of the following: \n 1.) Display the defualt gateway\n 2.) Test local connectivity\n 3.) Test remote connectivity \n 4.) Test DNS resolution\n 5.)Exit/end script")
#display the default gateway
def default():
result = subprocess.run(["ip", "r"], capture_output=True)
output = result.stdout.split()
ip_bytes = output[2]
ip_string = ip_bytes.decode('utf-8')
return ip_string
def ping(address):
result = subprocess.run(['ping', "-c", "4", address], capture_output=True)
if result.returncode == 0:
print(f"Ping to {address} was successful:\n")
else:
print(f"Ping to {address} failed with the following error message:\n")
print(result.stderr)
#test the local connectivity
def local():
#use default
address= default()
ping(address)
#ping default
#test remote connectivity
def remote():
#use 129.21.3.17
host = "129.21.3.17"
ping(host)
#test dns resolution
def dns():
ping("google.com")
#use 8.8.8.8
#making the program executable but not able to be edited
def exec():
subprocess.run(["chmod", "+", "rx" ])
subprocess.run(["chmod", "-", "w" ])
#Exit/quit the scripit
def main(): #clear
menu()
print("enter your selection: ")
val = input("")
if val== "1":
print("Your default gateway is: " , default())
elif val == "2":
local()
elif val == "3":
remote()
elif val == "4":
dns()
elif val == "5":
subprocess.run(["clear"])
exit()
else:
print("Please enter an option as listed")
if __name__ == "__main__":
exec()
subprocess.run(["clear"])
while True:
main()