It all started with the fact that one day I became the owner of the D-Link 2640U ADSL router. Since I also distribute the Internet to my friends, it is turned on for me around the clock, and then one annoying trouble was revealed - he periodically lost connection with the provider and did not want to connect before rebooting. Searches and firmware updates did not solve the problem, and since I sometimes don’t have a home, and I need to overload the modem, one day a script was written that does it automatically.
The choice fell on Python purely by chance - began to learn the language and decided to practice. Here's what came of it:
import os
import re
import time
import sys
import telnetlib
from threading import thread
class testit ( Thread ) :
def __init__ ( self , ip ) :
Thread. __init__ ( self )
self . ip = ip
self . status = - 1
def run ( self ) :
pingaling = os . popen ( "ping -q -c2" + self . ip , "r" )
while 1 :
line = pingaling. readline ( )
if not line: break
igot = re . findall ( testit. lifeline , line )
if igot:
self . status = int ( igot [ 0 ] )
if self . status == 0 :
tn = telnetlib . Telnet ( HOST )
tn. read_until ( "BCM96338 ADSL Router" )
tn. read_until ( "Login:" )
tn. write ( user + " \ n " )
tn. read_until ( "Password:" )
tn. write ( password + " \ n " )
time . sleep ( 5 )
# tn.write ("ifconfig ppp_0_1_32_1 \ n")
tn. write ( "reboot \ n " )
time . sleep ( 5 )
tn. write ( "logout \ n " )
time . sleep ( 60 )
')
testit. lifeline = re . compile ( r "( \ d ) received" )
ip = "213.180.204.8"
HOST = "192.168.168.1"
user = 'user'
password = 'password'
current = testit ( ip )
current. start ( )
The script pings the specified ip address and, if it is unavailable, connects via telnet to the modem and gives the command to reboot.
It costs me to run on the crown every 3 minutes (chosen by experience - the best option)
ip-address to be checked.
HOST - the modem address.
user, password - the username and password of the user on the modem, respectively.
The BCM96338 ADSL Router line gives me a modem when it comes in via telnet, you can change it to the one that matches your modem.
That's all proper. Corrections and additions are welcome.