Skip to content

Instantly share code, notes, and snippets.

@a37h
Last active May 26, 2021 05:15
Show Gist options
  • Save a37h/5c1d208cc377681118f64553254e5edc to your computer and use it in GitHub Desktop.
Save a37h/5c1d208cc377681118f64553254e5edc to your computer and use it in GitHub Desktop.
Reconnect WIFI every 60 seconds
#!/usr/bin/env python
import subprocess
import time
import datetime
import re
import sys
def sendNotification(message):
subprocess.Popen(['notify-send', f'-u', 'low', message])
return
def keepReconnectingTo(interface_name):
"""
Reconnects to the specified interface every 60 seconds
"""
bash_command = f"nmcli d disconnect {interface_name} && nmcli d connect {interface_name}"
def timePrint(text):
print(f'# {datetime.datetime.now().time()}', f'{text}', sep='\n')
try:
while True:
timePrint('disconnecting...')
sendNotification('disconnecting')
output = subprocess.check_output(['bash', '-c', bash_command])
timePrint('connected!')
sendNotification('connected!')
for i in range(6):
time.sleep(10)
print(f'{(i+1)*10}s/{6*10}... ', end='', flush=True)
print('\n')
except KeyboardInterrupt:
print('\n')
if __name__ == "__main__":
"""
Reconnect to the current active wifi interface every 90 seconds
"""
bash_command = "cat /proc/net/wireless"
output1 = subprocess.check_output(['bash', '-c', bash_command]).decode('utf-8')
interface_name = output1.split('\n')[2].split(':')[0].strip()
if interface_name != '':
print("Current active interface:", interface_name)
else:
print("Found no active interfaces, try connecting to wifi first")
exit()
keepReconnectingTo(interface_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment