Home > Expect/TCL, IT Architecture, Programming, SHELL > general expect tips

general expect tips

March 23rd, 2012

#!/bin/bash
PATH=$PATH:/opt/csw/bin/
export PATH
CMD="uname -a && finishthecoreadm"
#....ksh: finishthecoreadm: not found,......,......
USERNAME="root"
PASSWORD="test"
for i in `cat /home/liandy/servers_list`
do
echo ''>/root/.ssh/known_hosts
expect <<EOF
spawn ssh -l${USERNAME} -p22 $i $CMD
set timeout 20
expect "*yes*"
send "yes\r"
expect "assword:"
send "${PASSWORD}\r"
expect eof
EOF
done

================
spawn ssh 172.2.0.79 ifconfig
expect {
“*yes*” { send “yes\r” }
“password:” { send “yytt22\r” }
}
expect eof

#interact
spawn ssh 172.2.0.79
expect {
“*yes*” { send “yes\r” }
“password:” { send “yytt22\r” }
}
interact
expect “hi\n”
send “youyouyou”

expect “hi”
send “you typed $expect_out(buffer)”
send “but I only expected $expect_out(0,string)”

#spawn_id
spawn bc
set bc_id $spawn_id

spawn /bin/sh
set sh_id $spawn_id

set spawn_id $bc_id
send “scale=50\r”

#send -i & expect -i
set spawn_id $ftp_id
send “get $file1\r”;expect “220*ftp> ”
send -i $write_id “success get file” #another spawn_id
send “get $file2\r”;expect “220*ftp>” #return to current spawn_id

expect {
-i $ftp_id
“220*ftp> ” action1
“550*ftp ” action2
}

same as:

expect {
-i $ftp “220*ftp> ” action
“550*ftp ” action2 #return current spawn_id
}

#proc
proc login(id) { #spawn as parameter
set spawn $id
expect “login:”
send “$name\r”

expect “password:”
send “$password\r”

expect “$prompt”
}

#!/bin/bash
expect -c ”
set send_human {.1 .3 1 .05 2}

send -h \”I’m hungry. Let’s do lunch.\”

Good Luck!


Comments are closed.