Friday, December 16, 2011

Restart your Router Automatically when Disconnected from Internet

Routers are known to act buggy and unstable and might disconnect you from the internet without any obvious reasons. Then, you obviously need to restart the router to re-gain internet access. But you can also set up your Linux box to ping a specific web address at regular intervals and restart the router if the ping is unsuccessful.

If you are the only user of that internet connection and are not that lazy to manually restart your router whenever it disconnects, you could probably just stick with the conventional method.

If you share that internet connection with room-mates, family, etc. or run some kind of server, you would probably want to never lose connectivity for longer periods, even when you yourself are away from the network, this post is for you.

So the idea basically is to create a 'ping' script that pings a specific web address and if unsuccessful, it executes another script to reset the router via 'telnet'. The 'ping' script is run at regular intervals by 'crontab'. The interval can be anything you like but for me, the ideal is 3 minutes.

Telnet Setup

From Wikipedia:

Telnet is a network protocol used on the Internet or local area networks to provide a bidirectional interactive text-oriented communications facility using a virtual terminal connection.

If your router doesn't support Telnet (however, almost all routers do support Telnet), you are out of luck here. To make sure that your router supports Telnet, log in to your router's web access page and look for 'local access' or something similar, probably under 'management' or 'connectivity'. Make sure 'Telnet' is there and also make sure that it is enabled.

Now from Linux Terminal, try to access your router via Telnet:


Where '' is the IP address of your router.

You should be presented with a login prompt, like in the screenshot above. Enter your router's username and password and log in.

Once logged in, you can execute the 'help' command to find out the commands supported by your router. Commonly, 'uptime', 'ifconfig', 'reboot' etc. are supported by almost all routers, still there might be some differences in the syntax.

You need to find out what exact command successfully restarts your router. It might be 'reboot', 'restart', 'reset', 'kill' or something else. You also need to find out if your router supports 'logout' or 'exit' for exiting. Once you found out those commands, it's time to create the script.

So, create a text file in your home directory and name it 'restart-router'. If you need to do this from the command line:

nano ~/restart-router

Copy this text into the newly created file:

#!/usr/bin/env expect

#replace 'user' with your username
set username user
#replace 'password' with your password
set pass password
#replace with your router's IP address, if it differs
set host

spawn telnet ${host}

expect -re "login:"
send "${username}\r"

expect "Password:"
send "${pass}\r"

expect -re "#"
#replace 'reboot' with whatever your router's specific command is
send "reboot\r"
#replace 'logout' with whatever your router's specific command is
send "logout\r"
expect eof

Now make the script executable by either right-clicking the newly created file in Nautilus, going to the 'Permissions' tab and ticking 'Allow executing file as program', or from the Terminal:

chmod +x ~/restart-router

That's it! Now you can test your script, but before that, you need to install one of the needed tools, i.e. 'expect'. For Debian, Ubuntu and Mint:

sudo apt-get install expect

Now test the script by:


If everything goes well, this command should restart your router. If so, you can proceed, otherwise try revising the above script and try to figure out any mistakes.

The Ping Script

So, now we need to create the 'ping' script which would ping the intended web address and restart the router if required. Again, create a new file in your home directory and name it 'ping' this time. Or from the Terminal:

nano ~/ping

Copy this text into the newly created file:

#replace '' with your preferred address to ping. This is a Google DNS server address which you should be able to ping successfully if you are connected to the internet.
if ! ping -c 1
/home/user/restart-router # replace 'user' with your username

Now make this script executable the same way from Nautilus as mentioned above. Or from the Terminal:

chmod +x ~/ping

Setting Up Cron

Cron is a time-based job-scheduler for Unix/Linux. If you want to know more about Cron, from Terminal:

man crontab

So, for executing your 'ping' script at regular intervals or times of the day, edit 'crontab' by running:

crontab -e

If you are running 'crontab' for the first time, it would offer you to choose your preferred Terminal-based text-editor. 'Nano' is the easiest of them.

Now create a new job by pasting this text into the newly created file:

*/3 * * * * /home/user/ping

Where '*/3' would execute the script at all the minutes of an hour that are divisible by 3. You can change the values to suit your needs. And obviously, replace 'user' with your username.

If using Nano, press 'Ctrl + X' to exit the file and answer 'y' when it asks to save the file. If there were any errors, it would let you know as soon as you exit the editor or if none, the Cronjob would be activated immediately.

That's it! Enjoy an almost un-interrupted internet!

Related Posts:

Network , Troubleshooting