Jun
29
2009
29
2009
Automate Telnet Commands
An article by Bharat Balegere 1 Comment Last Modified on June 29, 2009
Telnet can be used to perform operations like configuring a router.These operations cannot be used in batch files to automate the process.
Ts10 can be used to automate telnet commands in Windows.
Expect can be used to automate telnet commands in Linux.
Example Windows
Restart a Huwaei ADSL2+ router automatically using a batch file
1.Create a text file restart.txt in folder c:\router
192.168.1.1
WAIT "Login: "
SEND "admin\m"
WAIT "Password: "
SEND "admin\m"
WAIT "--> "
SEND "system restart\m"
WAIT "Login: "
SEND "admin\m"
WAIT "Password: "
SEND "admin\m"
WAIT "--> "
SEND "system restart\m"
2.Download Ts10 to c:\router
3.Create a batch file restart.bat in c:\router
chdir C:\router
c:
TST10 /r:restart.txt /o:last_log.txt
c:
TST10 /r:restart.txt /o:last_log.txt
Just execute the Restart.bat file to restart your Router.
Example Linux
1.Install Expect : sudo apt-get install expect for ubuntu
2.Create a file restart.sh
#!/usr/bin/expect -f
set timeout 20
set echo off
set name admin
set routercmd "system restart\r"
# router password
set pass admin
# your router IP address
set routerip 192.168.1.1
spawn telnet $routerip
# send username & password
expect "Login: "
send "$name\r"
expect "Password: "
send "$pass\r"
expect -i "--> "
send $routercmd
exit
set timeout 20
set echo off
set name admin
set routercmd "system restart\r"
# router password
set pass admin
# your router IP address
set routerip 192.168.1.1
spawn telnet $routerip
# send username & password
expect "Login: "
send "$name\r"
expect "Password: "
send "$pass\r"
expect -i "--> "
send $routercmd
exit
3.Make Restart.sh executable
chmod u+x restart.sh
4.Execute the restart.sh script to restart your router.
Fix Low Volume Issues in Ubuntu »
You are a life saver. Thanks. I was using expect/autoexpect on Ubuntu but that conked out and had to fall back on Windows 7. The Telnet Scripting Tool works beautifully, I had problems with it earlier because I missed out on the space after “Login: ” and “Password: “.