expect传入参数和程序执行命令不成功问题

1.expect 关键字

  • send: 向进程发送字符串,用于模拟用户的输入。注意一定要加\r回车
  • expect: 从进程接收字符串
  • spawn: 启动进程(由spawn启动的进程的输出可以被expect所捕获)
  • interact: 用户交互

2.如何向expect脚本传入参数

set <变量名称> [lindex $argv <param index>]

注意下标从0开始,即0是第一个参数,不是脚本名称,这个和shell不同

#!/usr/bin/expect
set host [lindex $argv 0]  
set port [lindex $argv 1]  
set user [lindex $argv 2]  
set passwd [lindex $argv 3]  

3.程序中执行expect不成功

不要使用interact,如果在终端里执行expect,加上interact是没有问题的,但是在程序中执行命令,则不行;

spawn scp -r -P $port /root/dash/tool $user@$host:/tmp/
expect "*password:"
send "$passwd\r"
#interact
expect eof
exit

参考《Linux Expect 简介和使用实例》

猜你喜欢

转载自blog.csdn.net/my_live_123/article/details/88978820