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\n"
        }
   "pass"
        {
          send "$passwd\n"
        }
   "yes/no"
        {
          sleep 2
          send_user "send yes"
          send "yes\n"
        }
   eof
    {
        sleep 3
        send_user "eof\n"
    }
}
set timeout 3000
send "exit\r"
expect eof

 

备注:

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

set src_path [lindex $argv 0]

 

posted on 2021-01-19 16:23  石墨方  阅读(85)  评论(0编辑  收藏  举报

导航