Expect is a Tcl scripting language extension. Expect is often used for automation of interactive telecom applications. It is also widespread in the Telecom/Network testing. A lot of Telecom/Network test equipment (Spirent, IXIA, Agilent, JDSU) manufactures support TCL/Expect as test automation scripting language.
Using TCL/Expect you can easily automate some actions need to be executed many times during long period of time:
1. The script below connects to Linux Host with IP-address 192.168.10.10 and executes Linux commands "ifconfig eth1 down" and "ifconfig eth1 up" 99 times with 1 second delay between the commands:
# Set Timeout to wait response from Host
# Configuration parameters
set cfgHost "192.168.10.10"
set User "root"
set Password "admin123"
# Log in Linux Host
spawn ssh -l root $cfgHost
expect "login as:" { send "$User\r" }
expect "*assword:" { send "$Password\r" }
sleep 1;
# Disable/Enable Linux interface in cycle
for { set i 1 } { $i < 100 } { incr i } {
sleep 1;
expect "*~#" { send "ifconfig eth1 down\r" }
2. The script below connects to Cisco router and executes "sh interfaces status" command, all the Cisco console output will be sent to the predefined log file:
Using TCL/Expect you can easily automate some actions need to be executed many times during long period of time:
1. The script below connects to Linux Host with IP-address 192.168.10.10 and executes Linux commands "ifconfig eth1 down" and "ifconfig eth1 up" 99 times with 1 second delay between the commands:
# Set Timeout to wait response from Host
set timeout 30;
# Configuration parameters
set cfgHost "192.168.10.10"
set User "root"
set Password "admin123"
# Log in Linux Host
spawn ssh -l root $cfgHost
expect "login as:" { send "$User\r" }
expect "*assword:" { send "$Password\r" }
sleep 1;
# Disable/Enable Linux interface in cycle
for { set i 1 } { $i < 100 } { incr i } {
sleep 1;
expect "*~#" { send "ifconfig eth1 down\r" }
expect "*~#" { send "ifconfig eth1 up\r" }
}
expect eof;
exit;
}
expect eof;
exit;
2. The script below connects to Cisco router and executes "sh interfaces status" command, all the Cisco console output will be sent to the predefined log file:
# Connect Cisco router
spawn telent 10.10.10.10
expect "*ogin:" { send "admin" }
expect "*assword:" { send "123456\r" }
expect "*ogin:" { send "admin" }
expect "*assword:" { send "123456\r" }
# Open a logfile for the script output
log_file -noappend fulloutput.log; expect "*#" {send "sh interfaces status\r"}
# Finish script logging:
log_file
exit;
No comments:
Post a Comment