Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save romeshniriella/7f7c1e7a48dfac16aee3dce4052a4293 to your computer and use it in GitHub Desktop.
Save romeshniriella/7f7c1e7a48dfac16aee3dce4052a4293 to your computer and use it in GitHub Desktop.
i have a really bad ADSL connection with really bad SNR. restarting the router helps most of the time. and it lasts for 15 mins tops. so automate the process.
import re
import getpass
import sys
import telnetlib
import time, threading
from subprocess import check_output
def routerTelnet():
print (time.ctime())
speedText = check_output(['C:\Program Files\cFosSpeed\spd.exe', 'speed']).decode('utf-8')
regex = re.compile(r'txspeed=(\d+|\d+\.\d+)[K|\s]\s+fixed=', re.MULTILINE)
matches = [m.groups() for m in regex.finditer(speedText)]
print (speedText)
print (matches)
upspeed = float(matches[0][0])
if upspeed > 1024:
upspeed /= 1024;
print ("upload speed = ", upspeed)
if upspeed < 20.0 and upspeed > 4.0:
print("speed dropped: ")
HOST = "192.168.1.1"
user = 'admin'
password = '?password?'
tn = telnetlib.Telnet()
tn.open(HOST, 23)
tn.read_until( 'Username: '.encode('ascii'))
tn.write((user + "\r\n").encode('ascii'))
tn.read_until( 'Password: '.encode('ascii'))
tn.write((password + "\r\n").encode('ascii') )
print("restarting the router")
tn.write( ('reboot' + '\r\n').encode('ascii'))
print(tn.read_all())
else:
print("speed is normal")
threading.Timer(1 * 60 * 15, routerTelnet).start()
routerTelnet()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment