Linux Shell Script to reboot DSL or ADSL router

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:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 21 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
21 comments… add one
  • HKV Mar 31, 2007 @ 14:57

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

  • jjunior Apr 12, 2007 @ 13:43

    i found a err in the post.

    $ autoexepect

    $ autoexpect

  • Preshit May 16, 2007 @ 7:57

    # 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

  • 🐧 nixCraft May 16, 2007 @ 9:47


    Thanks for link and tutorial.

    Appreciate your post!

  • tkl Aug 29, 2007 @ 4:41

    How do I do a hardware reset

  • adam fowler Jan 16, 2008 @ 19:59

    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.

  • Federico Escobar Rodriguez Feb 8, 2008 @ 5:27

    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!

  • Pradeep Mar 28, 2008 @ 23:59

    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”

  • g Dec 31, 2008 @ 15:05

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

  • g Jan 7, 2009 @ 2:02

    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)

  • tomas May 3, 2009 @ 20:09

    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”

  • tomas May 3, 2009 @ 20:15

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


  • trinity May 27, 2009 @ 11:44

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

  • trinity May 27, 2009 @ 11:46

    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.

  • essbebe Jun 2, 2009 @ 11:17

    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.

  • Kevin Jul 4, 2010 @ 6:48

    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.

  • Kevin Jul 4, 2010 @ 7:09

    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

  • Kevin Jul 4, 2010 @ 7:46

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


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

  • MC Attema Jan 1, 2012 @ 22:28

    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”

  • Seedy Apr 25, 2012 @ 17:08

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

  • Sarthak Jun 22, 2013 @ 11:11

    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.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum