您的位置:首页 > 其它

expect 示例

2016-05-26 01:08 239 查看
/usr/bin/expect<<EOF
set timeout 50
spawn $SCP_LOG_TO_GATHER_SERVER
expect {
"*yes/no)?"
{
send "yes\n"
"*password:*" {send "GatherServerPassword\n"}
}
"*password:"
{
send "$GatherServerPassword\n"
}
}
expect "*password:"  { send "$GatherServerPassword\n" }
expect "100%"
expect eof

EOF

or
#!/usr/bin/expect

set timeout 1
spawn su root -c "/opt/1.sh"
expect "password: "
send "123456\r"
interact
exit

最新示例
#!/usr/bin/expect  -f   //这个expect的路径就是用which expect 查看的结果

spawn su - nginx       //切换用户
expect "password:"      //提示让输入密码
send "testr"       //输入nginx的密码
interact                //操作完成

#!/usr/bin/expect
set timeout 5
set server [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]

spawn ssh -l $user $server
expect {
"(yes/no)" { send "yesr"; exp_continue }
"password:" { send "$passwdr" }
}
expect "*Last login*" interact

#!/usr/bin/expect
set timeout 10
set host [lindex $argv 0]        //第1个参数,其它2,3,4参数类似
set username [lindex $argv 1]
set password [lindex $argv 2]
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn scp $src_file $username@$host:$dest_file
expect {
"(yes/no)?"
{
send "yesn"
expect "*assword:" { send "$passwordn"}
}
"*assword:"
{
send "$passwordn"
}
}
expect "100%"
expect eof
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  expect