Linux Shell Script to reboot DSL or ADSL router

by on February 14, 2006 · 21 comments· LAST UPDATED April 23, 2008

in , ,

If you need to reboot the router then you need to use web interface or telnet interface. Both methods take time, especially if you are playing with ACL, NAT or router firewall or you just wanna reboot the router from your Linux desktop. I have created simple script using expect tool to reboot router. Make sure you have expect command installed. Use rpm or apt-get command to install expect tool.

Shell script

Create a script as follows (tested on Beetel ADSL 220x router):

#!/usr/bin/expect -f
 
set timeout 20
 
# router user name
set name "admin"
 
# router password
set pass "PASSWORD"
 
# router IP address
set routerip "192.168.1.254"
 
# Read command as arg to this script
set routercmd [lindex $argv 0]
 
# start telnet
spawn telnet $routerip
 
# send username & password
expect "Login:"
send -- "$name\r"
expect "Password:"
send -- "$pass\r"
 
# get out of ISP's  Stupid menu program, go to shell
expect " -> "
send --  "sh\r"
 
# execute command
expect "# "
send -- "$routercmd\r"
# exit
send -- "^D"

Save script and setup executable permission on it:
$ chmod +x router.exp

How do I run this script?

You need to pass command to script to execute on a router. For example to display router uptime, interface information and to reboot router you need to type command as follows:
$ ./router.exp uptime
$ ./router.exp ifconfig
$ ./router.exp reboot

Since my ISP router offers menu as soon as login above script may not work on generic router such as Cisco or linksys router. Therefore, you may need to modify above script to work with your router. If you are a new to expect then use autoexpect command to generate script. It watches you interacting with another program and creates an Expect script that reproduces your interactions For straightline scripts, autoexpect saves substantial time over writing scripts by hand. Even if you are an Expect expert, you will find it convenient to use autoexpect to automate the more mindless parts of interactions. It is much easier to cut/paste hunks of autoexpect scripts together than to write them from scratch. Moreover, if you are a beginner, you may be able to get away with learning nothing more about Expect than how to call autoexpect. Just type autoexecpt:
$ autoexpectautoexpect started, file is script.exp

Next type telnet command (telnet to the router):
$ telnet 192.168.1.254
Output:

Login: USER
Password: Password

Now type commands on the router:
$ ifconfig
$ exit

You are done, type exit to stop autoexepct command:
$ exit
Output:

autoexpect done, file is script.exp

Just type ./script.exp to run ifconfig command:
$ ./script.exp
You can now modify script.exp to reboot or to run other commands. It is a real lifesaver.

See also:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 21 comments… read them below or add one }

1 HKV March 31, 2007 at 2:57 pm

er. Is ther any simple way to do it under windows ?

Reply

2 jjunior April 12, 2007 at 1:43 pm

i found a err in the post.

err:
$ autoexepect

correct:
$ autoexpect

Reply

3 Preshit May 16, 2007 at 7:57 am

# HKV Says:
March 31st, 2007 at 2:57 pm

er. Is ther any simple way to do it under windows ?

yes, there is. I have written a tutorial on my blog.

Here’s the link

Let me know if you have problems

Reply

4 nixCraft May 16, 2007 at 9:47 am

Preshit,

Thanks for link and tutorial.

Appreciate your post!

Reply

5 tkl August 29, 2007 at 4:41 am

How do I do a hardware reset

Reply

6 adam fowler January 16, 2008 at 7:59 pm

I am using Dlink DSL G624T router with Apple Mac OSX 10.3.9 and a Firefox browser. If I change my router config and click on ‘save and reboot’ nothing happens. I have to access the router from a Windows PC to get the ‘save and reboot’ option to function. If I telnet to my router what is the command to ‘save and reboot’ please.

Reply

7 Federico Escobar Rodriguez February 8, 2008 at 5:27 am

Help!!
I have used the autoexpect command to create a script to reboot my router. Well, it created a long text file with a lot of information and at the end the script needed wich works almost perfect.
The problem is that it reboot the router but then it keeps waiting in telnet (>) expecting for something but doesn’t respond to any command until i hit ctrl+c and stop the process by hand.
I’ve seen you used “exit” to logout from telnet but in my case i need to use “logout” because “exit” just doesn’t exists as a telnet command.
Please let me know how to send a signal to stop the process from the script, i tried “send — “^D”” but it doesn’t work.
Another think is that at the end of the script.exp file generated there is the command “expect eof”, what should i do? please help me!

Reply

8 Pradeep March 28, 2008 at 11:59 pm

Hello sir I am trying the same script to spwan ssh. Prioir to that i installed expect rpm and i modified the code but expect command througing error “Floating point error”

It’s veru important for please help”

Reply

9 g December 31, 2008 at 3:05 pm

Since you are so good at hacking, how can I make Siemens CL-110 ADSL router work as USB in Linux/Mandrake?

Reply

10 g January 7, 2009 at 2:02 am

well i’m closing this down than to have to check back – I tried all kinds of crap – to talk to USB devices with drivers (javax-usb and all that is at least incomplete work), virtual machines (impossible to install unless you wann fiddle with the parameters all day), and so on and so on – no solution – only an etherner adapter(card)

Reply

11 tomas May 3, 2009 at 8:09 pm

I have a problem with the script. The command is send to the prompt, but not executed just displayed. What could be the reason that script not executing the command?

$ ./teln_reboot.exp
#!/usr/bin/expect -f

set address “xxx.xxx.xxx.xxx”
set username “root”
set password “???????”

spawn telnet ${address}

expect “login:”
send — “${username}\r”
expect “Password:”
send — “${password}\r”
expect “~> ”
send — “reboot”
expect “~> ”
send — “exit\r”

Reply

12 tomas May 3, 2009 at 8:15 pm

And I forget to mention, it’s not DSL router, it’s dreambox satellite reciever but it have telnet server and busybox linux system.

Greetz.

Reply

13 trinity May 27, 2009 at 11:44 am

Hi’
Your code is correct but small correction is there,
Spawn telnet $address
expect “login:”
send “$usernem\r”
expect “password:”
send “$password\r”
expect “~>”
send “reboot\r”
expect “~>”

Reply

14 trinity May 27, 2009 at 11:46 am

hi,
how t owrite code for stop the autoboot process.please anyone help me.
How hit enter command during the send command execution please give me any idea.

Reply

15 essbebe June 2, 2009 at 11:17 am

Under windows it can be easily done with “Imran’s Broadband Helper Utility”.
With many more functionality “Imran’s Broadband Helper Utility” is a freeware.

Reply

16 Kevin July 4, 2010 at 6:48 am

The autoexpect worked well but I am also have the same problem as Federico Escobar Rodriguez wrote on February 8, 2008.

I have a perl program calling the script to reboot the router at specific times but it hangs after the exp script is executed. I don’t know what to do.

Reply

17 Kevin July 4, 2010 at 7:09 am

I found the problem described in the above comment.

The script never stops running. Typing ps -fu shows the pid for the script. The following command will correct the problem but some people might need it automated. kill

Reply

18 Kevin July 4, 2010 at 7:46 am

This works well. I also had to allow local telnet in the router Advanced / Local Administration settings.

http://broadbandforum.in/mtnl-broadband/11733-telnet-scripting-tool-linux-ubuntu/

#!/bin/sh
host=192.168.1.1
port=23
login=
passwd=
cmd=reboot

(echo open ${host} ${port}
sleep 1
echo ${login}
sleep 1
echo ${passwd}
sleep 1
echo ${cmd}
sleep 1
echo exit) | telnet

Reply

19 MC Attema January 1, 2012 at 10:28 pm

BBox2 Sagem Blocked – NAT out failed First packet in connection is not a SYN packet

hello just wont to let every body know that there is a way to automatic restart the firewall from the using ubuntu with a cronjob end installing under ubuntu the following program in a terminal $ sudo apt-get install expect after that copy the script and save it as restart.sh in /var/etc you have to create the etc folder and than you need to attribute it to 755

after that you open crontab -e and add “*/59 * * * * /var/etc/restart.sh” every 59 minutes

#!/usr/bin/expect -f
set timeout 5
set echo off

# router user name
set name admin

# router password
set pass 123456789

# router IP address
set routerip “192.168.1.1″

# Read command as arg to this script
set routercmd “fw_restart”

# start telnet
spawn telnet $routerip

# send username & password
expect “Username : ”
send “$name\r”
expect “Password : ”
send “$pass\r”
expect -i “_{admin}=>”

# get out of ISP’s Stupid menu program, go to shell
expect ” -> ”
send — “fw_restart\r”

# execute command
expect “# ”
send — “$routercmd”
# exit
send — “^D”

Reply

20 Seedy April 25, 2012 at 5:08 pm

Can this same script be alerted to change some firewall settings, ie: allow/deny certain IP’s on the local network?
Thanks

Reply

21 Sarthak June 22, 2013 at 11:11 am

Hi,
I wish to renew my ip address without restarting the device. Is it possible..?? One more thing I know its been pretty old post but can you help me getting shell access to beetel 450TC1 router given by airtel. As such I can’t find means to get busybox on it.
I wish to work freely and stop getting blocked by download sites for downloading.
Plz help.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , ,

Previous post:

Next post: