Expect is a tool to emulate human interaction with a system.
- expect <command-file>
- Execute the commands in the commandfiles
More research is needed
This script will put the output of a command on your terminal
set timeout 20 #If it all goes pear shaped the script will timeout after 20 seconds.
set name [lindex $argv 0] #First argument is assigned to the variable name
set user [lindex $argv 1] #Second argument is assigned to the variable user
set password [lindex $argv 2] #Third argument is assigned to the variable password
spawn telnet 10.0.0.1
expect “login:” #The script expects login
send “$user
” #The script sends the user variable
expect “Password:” #The script expects Password
send “$password
” #The script sends the password variable
expect — “>”
send — “ls – alr”
expect — “ls -alr”
expect — “(.*}/r”
More Examples
set timeout -1
set host [lindex $argv 1]
set prompt [lindex $argv 2]
set IO [lindex $argv 3]
spawn ssh user@$address
sleep 5
expect “Password: ”
send “passwordr”
expect “$prompt:~> ”
send “script.shr”
expect “Choice_>”
send “11r”
expect “Server address : ”
send “ThisScriptExpectedATarget for a scp command (user@host:/dir/dir/)”
expect “Password: ”
send “Apasswordr”
expect “$prompt:~> ”
send “exitr”
./expectscript.exp $ADDRESS $HOST $PROMPT $IO
=====================================
;# script wasn’t run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s — $arg
}
}
puts stderr “Usage: scripts.exp ip agent filename”
exit 0
} else {
set agent [lindex $argv 1]
set filename [lindex $argv 2]
set port 23
set delay 1
set INPUTFILE [open $filename “r”]
spawn telnet $ip $port
match_max 100000
expect “assword: ”
send — “passwordr”
expect “$hostname> ”
send — “enabler”
expect “assword: ”
send — “passwordr”
expect “$en_prompt”
send_user “n<$line>”
send — “notify sipd find-agent $liner”
expect {
“Could not find host with name” {
send_user “nERROR: <$line> COULD NOT BE FOUND”
continue
}
“Found host with name” {
continue
}
}
}
sleep 1
send — “exitr”
expect “$hostname> ”
send — “exitr”
}
close $INPUTFILE
=====================================
;# script wasn’t run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s — $arg
}
}
set ip “<IPaddress>”
set agent “$hostnaam”
set port 23
set delay 1
set timeout -1
spawn telnet $ip $port
match_max 100000
expect “assword: ”
send — “passwordr”
expect “$agent> ”
send — “exitr”
===================================