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 "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:
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- Hacking Beetel 220x ADSL router (Broadcom BCM6338)
- How to configure cisco router using linux for (COM port) configuration
- HP-UX: How Do I configure routing or add route?
- Testing Connectivity with fping and send mail if any hosts are unreachable
- Simple Linux and UNIX system monitoring with ping command and scripts
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: adsl, beetel, Cisco, expect_command, linksys router, Linux, nat router, reboot, reboot_router, router linux, router reboot, router_firewall, router_reboot_automation, shell script, shell_script, telnet_interface, UNIX, web_interface



er. Is ther any simple way to do it under windows ?
i found a err in the post.
err:
$ autoexepect
correct:
$ autoexpect
yes, there is. I have written a tutorial on my blog.
Here’s the link
Let me know if you have problems
Preshit,
Thanks for link and tutorial.
Appreciate your post!
How do I do a hardware reset
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.
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!
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”