命令
expect
描述
programmed dialogue with interactive programs, Version 5
交互式编程工具
用法
1
| expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
|
选项
1 2 3 4 5
| Command: send 用于向进程发送字符串 expect 从进程接收字符串 spawn 启动新进程 interact 执行交互命令
|
注意
expect
命令需要安装expect软件包
参考
https://www.linuxprobe.com/linux-expect-auto.html
https://blog.csdn.net/lijingjingchn/article/details/97129561
https://www.cnblogs.com/kevingrace/p/5900303.html
https://blog.csdn.net/u010820857/article/details/89925274
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| $ yum install expect
$ vim ssh.exp
set timeout 10 set host 192.168.80.12 set username root set password "redhat"
spawn ssh $username@$host expect { "yes/no" { send "yes\r";exp_continue } "password:" { send "$password\r" } } expect "]*" {send "echo a >> /tmp/a\r"}
interact $ chmod 755 ssh.exp $ ./ssh.exp
$ vim sftp.sh
host=192.168.80.12 username=root password="redhat"
/usr/bin/expect << EOF set timeout 5 spawn sftp -o stricthostkeychecking=no -P 22 $username@$host expect { "yes/no" { send "yes\r";exp_continue } "password" { send "$password\n"} } expect "sftp>" send "ls\n" send "bye\r" expect eof EOF $ sh sftp.sh
|