Expect

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

# cat expectscript.exp
#!/usr/bin/expect -f
set send_slow {1 .05}
set timeout -1
set address [lindex $argv 0]
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”
====
Executen met:

./expectscript.exp $ADDRESS $HOST $PROMPT $IO

-==================
===================
=====================================
=====================================
#!/usr/bin/expect -f
set force_conservative 1  ;# set to 1 to force conservative mode even if
;# script wasn’t run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s — $arg
}
}
if {$argc < 3} {
puts stderr “Usage: scripts.exp ip agent filename”
exit 0
} else {
    set ip [lindex $argv 0]
set agent [lindex $argv 1]
set filename [lindex $argv 2]
    set en_prompt “$agent# ”
set port 23
set delay 1
set INPUTFILE [open $filename “r”]
    set timeout -1
spawn telnet $ip $port
match_max 100000
expect “assword: ”
send — “passwordr”
expect “$hostname> ”
send — “enabler”
expect “assword: ”
send — “passwordr”
expect “$en_prompt”
    while {[gets $INPUTFILE line] != -1} {
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
=====================================
=====================================
#!/usr/bin/expect -f
set force_conservative 1  ;# set to 1 to force conservative mode even if
;# 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 hostnaam “hostname”
set ip “<IPaddress>”
set agent “$hostnaam”
set en_prompt “$agent# ”
set port 23
set delay 1
set timeout -1
spawn telnet $ip $port
match_max 100000
expect “assword: ”
send — “passwordr”
expect “$agent> ”
send — “exitr”
===================================
===================================

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.