expect简单使用案例

举例:自动从另一个机器B中获取文件

shell脚本中调用expect脚本(传递所需要的机器B中的文件路径)

filename=$1
./t1.exp "/data/$filename"

给t1.exp赋权

chmod 755 t1.exp

expect 脚本

#!/usr/bin/expect
set passwd "test"
set src_ip "x.x.x.x"
set src_path [lindex $argv 0]
spawn scp root@$src_ip:$src_path /data
expect {
  "密码:"
        {
          send "$passwd
"
        }
   "pass"
        {
          send "$passwd
"
        }
   "yes/no"
        {
          sleep 2
          send_user "send yes"
          send "yes
"
        }
   eof
    {
        sleep 3
        send_user "eof
"
    }
}
set timeout 3000
send "exit
"
expect eof

备注:

expect脚本中接受shell脚本的参数(是从0开始的)

set src_path [lindex $argv 0]
原文地址:https://www.cnblogs.com/Nora-F/p/14298603.html