Poniższy kod pozwala na połączenie ssh, automatyczne podanie użytkownika i hasła, wykonanie prostej komendy, przejście do katalogu /tmp i oddaje władzę w ręce w użytkownika skryptu. Taka mała wprawka do programowania w tcl/expect…
#!/usr/bin/expect
## usage if no options
if {[llength $argv] != 3} {
puts "usage: ssh_login "
exit 1
}
# setup variables
set timeout 20
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
# call ssh command
spawn ssh "$user\@$ip"
# give password
expect "password:"
send "$password\r";
# give commands when you see "]#" mean all welcome messages are showed
expect "]#"
send "touch ~/I_was_HERE2.txt\r"
send "cd /tmp\r"
# give control to user
interact