A blog where I talk about the stuff I make, do and more

Monday, November 30, 2015

[HOWTO] Raspi tricks: reboot your router every so often

4 comments
Okay let me immediately start by saying: this is absolutely not a thing that only the Raspberry Pi can do - but because it's such a small device and it uses relatively little power I think it's safe to assume that lots of people have this thing turned on day and night. And in that case, it's the ideal device to do some maintenance jobs over the network.

This little guide will show you how I've set up my Raspi to reset my router every day (it's a long story but one of my routers is very unstable in its current set up and I haven't figured out how to fix it yet).

UPDATE:

I forgot to mention which router I use this script for. It's a ASUS RT N66U.

As Jeroen pointed out in the comments it can be a bit more complicated if you use other routers. If you have a Fritz!box (xs4all tends to supply these as modems/routers) you are in luck:
https://github.com/jpluimers/bash-fritzclient

REQUIREMENTS

  • A raspberry pi
  • A router with telnet access
  • Some basic knowledge of linux, bash and cron jobs

LET'S GO!

First you probably need to install telnet:

#install telnet
sudo apt-get install telnet

Now you have telnet give it a try and log in with your router

#create a connection
telnet [ip-of-router]

You should be prompted for your username and password. Once you provided these, try the reboot command:

#reboot router
reboot

If this is all working we can move on to the next step: create a little bash script that runs a bunch of commands. It doesn't really matter where you put it but I like to have it in /home/pi/scripts/

#create directory
mkdir ~/scripts

#create file
nano ~/scripts/cronjob-router-reboot.sh

Paste the following script

#!/bin/sh
# replace cmd1 for the command to execute

host=192.168.0.179
port=23
user=YOURUSERNAME
pass=YOURPASSWORD
cmd1='reboot'

rm -f /home/pi/scripts/log-cronjob-router-reboot.txt

( echo open ${host}
sleep 2
echo ${user}
sleep 1
echo ${pass}
sleep 1
echo ${cmd1}
sleep 1

echo quit

sleep 2
 ) | /usr/bin/telnet > home/pi/scripts/log-cronjob-router-reboot.txt

You do need to edit the 'host', 'user' and 'pass' values at the top :). Press Ctrl+X, Y and then ENTER to save the file and exit.

Now make the file executable:

#make executable
chmod +x cronjob-router-reboot.sh

And try it out!

# run script
/home/pi/scripts/cronjob-router-reboot.sh

The last thing we need to do now is add it as a cron job. Cron, in case you don't know, is like Windows' Task Scheduler: it lets you run things at a given time and/or interval.

#open crontab
crontab -e

At the bottom, paste the following to start the job every day at 4:00 in the morning (see the text for a short description of that the first 5 characters do and how you can use them to schedule things)

0 4 * * * /home/pi/scripts/cronjob-router-reboot.sh

That's it! Save it and exit. Your raspberry should now reboot your router every day at 4:00 AM :D

4 comments :

  1. That's a tad more complex with a Fritz!Box router. So I have created a repository for that : https://github.com/jpluimers/bash-fritzclient

    ReplyDelete
    Replies
    1. Ah I forgot to mention which router I use :P Will edit the post asap with that info and add your link. Thanks!

      Delete
  2. Hello! I was trying to get this work, but no luck so far. It seems that the scrip does not press Enter after Username, Password and the defined command. so my screen when im running the script looks like this:
    pi@raspberrypi:~ $ sh telnet.sh
    telnet> Trying 192.168.0.1...
    Connected to 192.168.0.1.
    Escape character is '^]'.

    User Access Verification

    Username: myuser
    mypassword
    help system
    quit
    Connection closed by foreign host.

    any guess why it is like this? Im running raspbian.

    ReplyDelete
  3. I am very enjoyed for this blog. Its an informative topic.
    routerlogin
    netgear setup

    ReplyDelete