Linux Shell Script to reboot DSL or ADSL router

last updated in Categories Debian Linux, Howto, Linux, Linux desktop, Networking, RedHat/Fedora Linux, Shell scripting, Suse Linux, Tips

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 ""
# 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

Login: USER
Password: Password

Now type commands on the router:
$ ifconfig
$ exit

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

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:


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

21 comment

  1. # 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

  2. 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.

  3. 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!

  4. 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”

  5. 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)

  6. 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 “”
    set username “root”
    set password “???????”

    spawn telnet ${address}

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

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


  8. Hi’
    Your code is correct but small correction is there,
    Spawn telnet $address
    expect “login:”
    send “$usernemr”
    expect “password:”
    send “$passwordr”
    expect “~>”
    send “rebootr”
    expect “~>”

  9. 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.

  10. 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.

  11. 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.

  12. 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

  13. 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 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/” 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 “″

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

    # start telnet
    spawn telnet $routerip

    # send username & password
    expect “Username : ”
    send “$namer”
    expect “Password : ”
    send “$passr”
    expect -i “_{admin}=>”

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

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

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

  15. 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.

    Have a question? Post it on our forum!