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.
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
Login: USER Password: Password
Now type commands on the router:
You are done, type exit to stop autoexepct command:
autoexpect done, file is script.exp
Just type ./script.exp to run ifconfig command:
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!
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop