Skip to content

Commit bb5ce56

Browse files
committed
Added Internet Connectivity Monitor
1 parent 4d61073 commit bb5ce56

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Internet Connectivity Monitor
2+
3+
4+
## Overview
5+
6+
Hi, I'm Prince Khunt. I have developed this Python script, which periodically check internet connectivity and diagnose network issues. It automates the process of diagnosing and potentially resolving connectivity problems by performing various network tests and actions.
7+
8+
## Features
9+
10+
- Checks internet connectivity by pinging multiple websites.
11+
- Diagnoses network issues such as DNS resolution problems, DNS hijacking, proxy blocking, and firewall issues.
12+
- Automatically restarts Wi-Fi connections, if connectivity problems persist.
13+
14+
## Usage
15+
16+
1. Clone or download the script to your local machine.
17+
2. Ensure you have Python installed on your system.
18+
3. Run the script using the command `python monitor.py`.
19+
4. The script will periodically check internet connectivity and diagnose any issues encountered.
20+
21+
## Requirements
22+
23+
- Python 3.x
24+
- Requests library (install via `pip install requests`)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import requests
2+
import socket
3+
import platform
4+
import subprocess
5+
import time
6+
7+
# List of websites
8+
websites = ['http://google.com', 'http://facebook.com', 'http://twitter.com']
9+
10+
# Check internet connectivity
11+
def check_internet():
12+
for website in websites:
13+
try:
14+
response = requests.get(website, timeout=10)
15+
if response.status_code == 200:
16+
print("\033[92mConnected to {}\033[0m".format(website))
17+
return True
18+
except requests.ConnectionError as e:
19+
print("\033[91mFailed to connect to {}: {}\033[0m".format(website, e))
20+
break # Stop further attempts if one website fails
21+
return False
22+
23+
# Diagnose network issues
24+
def diagnose_issue():
25+
# Flush DNS cache
26+
try:
27+
if platform.system() == 'Windows':
28+
subprocess.run(["ipconfig", "/flushdns"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
29+
print("\033[92mDNS cache flushed.\033[0m")
30+
elif platform.system() in ['Darwin']:
31+
subprocess.run(["sudo", "killall", "-HUP", "mDNSResponder"], check=True)
32+
subprocess.run(["sudo", "dscacheutil", "-flushcache"], check=True)
33+
print("\033[92mDNS cache flushed.\033[0m")
34+
elif platform.system() in ['Linux']:
35+
subprocess.run(["sudo", "systemctl", "restart", "networking"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
36+
print("\033[92mDNS cache flushed.\033[0m")
37+
else:
38+
print("\033[91mUnsupported platform for DNS cache flushing.\033[0m")
39+
except subprocess.CalledProcessError as e:
40+
print("\033[91mFailed to flush DNS cache: {}\033[0m".format(e))
41+
42+
# Check DNS resolution
43+
try:
44+
socket.gethostbyname('google.com')
45+
print("\033[92mDNS resolution successful.\033[0m")
46+
except socket.gaierror:
47+
print("\033[91mDNS resolution failed. Check DNS settings.\033[0m")
48+
49+
# Check DNS hijacking
50+
try:
51+
dns_response = socket.gethostbyname('example.com')
52+
if dns_response != '93.184.216.34':
53+
print("\033[93mDNS hijacking detected.\033[0m")
54+
except socket.gaierror:
55+
print("\033[91mDNS resolution failed. Check DNS settings.\033[0m")
56+
57+
# Check if proxy is blocking connections
58+
try:
59+
response = requests.get("http://example.com", timeout=10)
60+
if response.status_code == 200:
61+
print("\033[92mProxy is not blocking connections.\033[0m")
62+
except requests.ConnectionError:
63+
print("\033[91mConnection Error Occurred, Proxy could be blocking connection. \033[0m")
64+
65+
# Check general network connectivity
66+
try:
67+
socket.create_connection(("google.com", 80), timeout=10)
68+
print("\033[92mIPv4 network connectivity is fine.\033[0m")
69+
except OSError:
70+
print("\033[91mIPv4 network connectivity issue. Check network settings or firewall.\033[0m")
71+
72+
# Check ipv6 ping
73+
if platform.system() != 'Windows': # Windows does not support IPv6 ping easily
74+
try:
75+
subprocess.run(["ping", "-c", "1", "-6", "ipv6.google.com"], timeout=10, check=True)
76+
print("\033[92mIPv6 network connectivity is fine.\033[0m")
77+
except subprocess.CalledProcessError:
78+
print("\033[91mIPv6 network connectivity issue. Check network settings or firewall.\033[0m")
79+
except subprocess.TimeoutExpired:
80+
print("\033[91mIPv6 ping timeout.\033[0m")
81+
82+
# Check if ping is working
83+
try:
84+
if platform.system() == 'Windows':
85+
subprocess.run(["ping", "-n", "1", "8.8.8.8"], timeout=10, check=True)
86+
else:
87+
subprocess.run(["ping", "-c", "1", "8.8.8.8"], timeout=10, check=True)
88+
print("\033[92mPing is up.\033[0m")
89+
except subprocess.CalledProcessError:
90+
print("\033[91mUnable to ping. Probably Internet is not working, Check firewall settings if any.\033[0m")
91+
except subprocess.TimeoutExpired:
92+
print("\033[91mUnable to ping. Internet is not working.\033[0m")
93+
94+
# Check Captive portals
95+
try:
96+
response = requests.get("http://clients3.google.com/generate_204", timeout=10)
97+
if response.status_code == 204:
98+
print("\033[92mNo captive portal detected.\033[0m")
99+
else:
100+
print("\033[93mCaptive portal detected.\033[0m")
101+
except requests.ConnectionError:
102+
print("\033[91mFailed to check for captive portal.\033[0m")
103+
104+
# Check certificate
105+
try:
106+
response = requests.get("https://google.com", timeout=10)
107+
print("\033[92mSSL certificate check successful.\033[0m")
108+
except requests.exceptions.SSLError:
109+
print("\033[91mSSL certificate check failed. Check SSL certificates.\033[0m")
110+
except requests.ConnectionError:
111+
print("\033[91mFailed to check SSL certificate.\033[0m")
112+
113+
#Restart Wi-Fi connection
114+
def restart_wifi():
115+
system = platform.system()
116+
if system == 'Windows':
117+
try:
118+
subprocess.run(["netsh", "interface", "set", "interface", "Wi-Fi", "disabled"], check=True)
119+
time.sleep(5)
120+
subprocess.run(["netsh", "interface", "set", "interface", "Wi-Fi", "enabled"], check=True)
121+
except subprocess.CalledProcessError as e:
122+
print("\033[91mFailed to restart Wi-Fi on Windows: {}\033[0m".format(e))
123+
elif system == 'Linux':
124+
try:
125+
subprocess.run(["sudo", "systemctl", "restart", "network-manager"], check=True)
126+
except subprocess.CalledProcessError as e:
127+
print("\033[91mFailed to restart Wi-Fi on Linux: {}\033[0m".format(e))
128+
elif system == 'Darwin': # macOS
129+
try:
130+
subprocess.run(["networksetup", "-setairportpower", "en0", "off"], check=True)
131+
time.sleep(5)
132+
subprocess.run(["networksetup", "-setairportpower", "en0", "on"], check=True)
133+
except subprocess.CalledProcessError as e:
134+
print("\033[91mFailed to restart Wi-Fi on macOS: {}\033[0m".format(e))
135+
else:
136+
print("\033[91mUnsupported platform.\033[0m")
137+
138+
#Check internet connectivity every 10 seconds
139+
while True:
140+
if not check_internet():
141+
print("\033[91mInternet is down. Diagnosing the issue...\033[0m")
142+
diagnose_issue()
143+
print("\033[93mAttempting to restart Wi-Fi...\033[0m")
144+
restart_wifi()
145+
time.sleep(10) # Allow time for Wi-Fi to reconnect
146+
if check_internet():
147+
print("\033[92mWi-Fi restarted successfully.\033[0m")
148+
else:
149+
print("\033[91mFailed to restart Wi-Fi or connect to the internet.\033[0m")
150+
else:
151+
print("\033[92mInternet is up and running.\033[0m")
152+
153+
time.sleep(10) # Wait for 10 seconds before checking again
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests==2.28.2

0 commit comments

Comments
 (0)