-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_connection_check.py
47 lines (39 loc) · 1.09 KB
/
net_connection_check.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
#!/usr/bin/python
import socket
import re
import subprocess
import os
remoteip = "8.8.8.8"
hostname = "www.yahoo.com"
connection = True
os.system('clear')
print('-' * 25)
print "***Beginning Test***\n1\n2\n3\n4\n5\n"
try:
ipr = subprocess.check_output('ip route | grep default', shell=True)
except:
connection = False
# get default gateway
if(connection):
searchobj = re.search(r'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})', ipr)
gateway = searchobj.group()
print("Your default gateway is " + gateway + "\n")
# ping the gateway
if(os.system('ping -c 1 ' + gateway + ' > /dev/null 2>/dev/null')):
print "Gateway Connection FAILED\n"
else:
print "Gateway Connection SUCCEEDED\n"
else:
print "No gateway to test\n"
# ping the remote address
if(os.system('ping -c 1 ' + remoteip + ' > /dev/null 2>/dev/null')):
print "Remote Connection FAILED\n"
else:
print "Remote Connection SUCCEEDED\n"
# ping the url to test DNS
if(os.system('ping -c 1 ' + hostname + ' > /dev/null 2>/dev/null')):
print "DNS Resolution FAILED\n"
else:
print "DNS Resolution SUCCEEDED\n"
print "***End Test***"
print('-' * 25)