wifi-keepalive/wifi-keepalive.py

33 lines
780 B
Python

from signal import SIGTERM, signal
from subprocess import CalledProcessError, check_output
from sys import exit
from time import sleep
PING_INTERVAL_IN_SECONDS = 80
signal(SIGTERM, lambda signum, frame: exit(0))
def getDefaultRoute():
routes = check_output(['route', '-n']).decode('utf-8').splitlines()
defaults = [r.split()[1] for r in routes if r.startswith('0.0.0.0')]
return defaults[0] if defaults else None
def keepalive(defaultRoute):
while True:
sleep(PING_INTERVAL_IN_SECONDS)
ping(defaultRoute)
def ping(address):
try:
check_output(['ping', '-c', '1', address])
except CalledProcessError as e:
pass
defaultRoute = getDefaultRoute()
keepalive(defaultRoute) if defaultRoute else print('No default route')