Skip to content

Commit f19abcf

Browse files
authored
Merge pull request DhanushNehru#214 from ruchit-t/feat-auto-wifi-check-script
Auto WiFi Check Script
2 parents 6bc58c8 + 4153bd0 commit f19abcf

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

Auto WiFi Check/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<h1>WiFi Connection Checker and Restarter</h1>
2+
3+
<h2>Overview</h2>
4+
<p>This Python script is designed to check the status of your WiFi connection on a Windows system. If the WiFi network is inactive and not able to receive ping packets, the script will automatically turn off the WiFi, turn it on again, and check whether the connection is live again or not. The script utilizes the <code>schedule</code> package for task scheduling, <code>netsh</code> commands for controlling the WiFi interface, and the <code>netifaces</code> package to retrieve meaningful network interface names.</p>
5+
6+
<h2>Prerequisites</h2>
7+
<ul>
8+
<li>This script is specifically designed for Windows systems.</li>
9+
<li>The script requires administrative privileges to perform actions like turning off and on the WiFi interface.</li>
10+
</ul>
11+
12+
<h2>Dependencies</h2>
13+
<p>Install the <code>schedule</code> and <code>netifaces</code> packages using the following commands:</p>
14+
<pre>
15+
pip install schedule
16+
pip install netifaces
17+
</pre>
18+
19+
<h2>Usage</h2>
20+
<ol>
21+
<li>Run the script with administrative privileges to ensure it can control the WiFi interface.</li>
22+
<pre>python wifi_checker.py</pre>
23+
<li>The script will schedule a periodic check of the WiFi connection status.</li>
24+
<li>If the WiFi connection is inactive (unable to receive ping packets), the script will automatically turn off and on the WiFi interface.</li>
25+
<li>The script will log the status of the WiFi connection in the console.</li>
26+
</ol>
27+
28+
<h2>Important Notes</h2>
29+
<ul>
30+
<li>Ensure that the script is run with administrative privileges to avoid permission issues.</li>
31+
<li>The script uses the <code>netsh</code> command to control the WiFi interface. If there are any issues with the execution of <code>netsh</code>, the script may not work as expected.</li>
32+
</ul>
33+
34+
<h2>References</h2>
35+
<ul>
36+
<li><a href="https://schedule.readthedocs.io/en/stable/" target="_blank">Schedule Package Documentation</a></li>
37+
<li><a href="https://stackoverflow.com/questions/373335/how-do-i-get-a-cron-like-scheduler-in-python" target="_blank">Stack Overflow: How do I get a cron-like scheduler in Python?</a></li>
38+
<li><a href="https://stackoverflow.com/questions/44246527/turn-wifi-off-using-python-on-windows-10" target="_blank">Stack Overflow: Turn WiFi off using Python on Windows 10</a></li>
39+
<li><a href="https://stackoverflow.com/questions/29913516/how-to-get-meaningful-network-interface-names-instead-of-guids-with-netifaces-un" target="_blank">Stack Overflow: How to get meaningful network interface names instead of GUIDs with netifaces?</a></li>
40+
<li><a href="https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script" target="_blank">Stack Overflow: Request UAC elevation from within a Python script</a></li>
41+
</ul>
42+
43+
</body>
44+
</html>

Auto WiFi Check/wifi_checker.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import ctypes
2+
import subprocess
3+
import sys
4+
import time
5+
from datetime import datetime
6+
import schedule as sc
7+
8+
9+
def enable():
10+
subprocess.call("netsh interface set interface Wi-Fi enabled")
11+
print("Turning On the laptop WiFi")
12+
13+
def disable():
14+
subprocess.call("netsh interface set interface Wi-Fi disabled")
15+
print("Turning Off the laptop WiFi")
16+
17+
18+
19+
def job():
20+
if subprocess.call("netsh interface set interface Wi-Fi enabled") == 0:
21+
print("WiFi is enabled and connected to internet")
22+
hostname = "www.google.com"
23+
response = subprocess.call("ping -n 1 " + hostname)
24+
if response == 1:
25+
print("Your Connection is not working")
26+
disable()
27+
time.sleep(1)
28+
enable()
29+
30+
def is_admin():
31+
try:
32+
return ctypes.windll.shell32.IsUserAnAdmin()
33+
except:
34+
return False
35+
36+
if is_admin():
37+
# job()
38+
sc.every(50).seconds.do(job)
39+
else:
40+
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
41+
42+
43+
while True:
44+
sc.run_pending()
45+
time.sleep(1)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ More information on contributing and the general code of conduct for discussion
3838
| Arrange It | [Arrange It](https://github.com/DhanushNehru/Python-Scripts/tree/master/Arrange%20It) | A Python script which can automatically move files into corresponding folders based on their extensions. |
3939
| AutoCert | [AutoCert](https://github.com/DhanushNehru/Python-Scripts/tree/master/AutoCert) | A Python script to auto generate e-certificates in bulk. |
4040
| Automated Emails | [Automated Emails](https://github.com/DhanushNehru/Python-Scripts/tree/master/Automate%20Emails%20Daily) | A Python script to send out personalised emails by reading a CSV file. |
41+
| Auto WiFi Check | [Auto WiFi Check](https://github.com/DhanushNehru/Python-Scripts/tree/master/Auto%20WiFi%20Check) | A Python script to monitor if WiFi connection is active or not
4142
| Blackjack | [Blackjack](https://github.com/DhanushNehru/Python-Scripts/tree/master/Blackjack) | A game of Blackjack - let's get a 21. |
4243
| Chessboard | [Chessboard](https://github.com/DhanushNehru/Python-Scripts/tree/master/Chess%20Board) | Creates a chesboard using matplotlib. |
4344
| Compound Interest Calculator | [Compound Interest Calculator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Calculate%20Compound%20Interest) | A Python script to calculate compound interest. |
@@ -48,7 +49,7 @@ More information on contributing and the general code of conduct for discussion
4849
| Digital Clock | [Digital Clock](https://github.com/DhanushNehru/Python-Scripts/tree/master/Digital%20Clock) | A Python script to preview a digital clock in the terminal. |
4950
| Duplicate Finder | [Duplicate Finder](https://github.com/DhanushNehru/Python-Scripts/tree/master/Duplicate%Fnder) | The script identifies duplicate files by MD5 hash and allows deletion or relocation. |
5051
| Display Popup Window | [Display Popup Window](https://github.com/DhanushNehru/Python-Scripts/tree/master/Display%20Popup%20Window) | A Python script to preview a GUI interface to user. |
51-
| Emoji in PDF | [Emoji in PDF](https://github.com/DhanushNehru/Python-Scripts/tree/master/Expense%20Tracker) | A Python Script to view Emoji in PDF.
52+
| Emoji in PDF | [Emoji in PDF](https://github.com/DhanushNehru/Python-Scripts/tree/master/emoji_to_pdf) | A Python Script to view Emoji in PDF.
5253
| Expense Tracker | [Expense Tracker](https://github.com/DhanushNehru/Python-Scripts/tree/master/Expense%20Tracker) | A Python script which can track expenses. |
5354
| Face Reaction | [Face Reaction](https://github.com/DhanushNehru/Python-Scripts/tree/master/Face%20Reaction) | A script which attempts to detect facial expressions. |
5455
| Fake Profiles | [Fake Profiles](https://github.com/DhanushNehru/Python-Scripts/tree/master/Fake%20Profile) | Create fake profiles. |

0 commit comments

Comments
 (0)