交互式转化批处理工具 expect
expect中相关命令
- spawn 启动新的进程
- expect 从进程接收字符串
- send 用于向进程发送字符串
- interact 允许用户交互
- exp_continue 匹配多个字符串在执行动作后加此命令
例一
#!/usr/bin/expect
spawn scp /tmp/123.txt [email protected]:/tmp
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "password\n"}
}
expect eof
[umpay@localhost ~]$ ./a.exp
例二
#!/usr/bin/expect
spawn ssh 1192.168.10.200
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "password\n"}
}
interact
例三 expect变量
#!/usr/bin/expect
set ip 1192.168.10.200
set user umpay
set password password
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "$password\n"}
}
interact
例四 expect位置参数
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "password\n"}
}
interact
[umpay@localhost ~]$ ./d.exp 1192.168.10.200 umpay password
例五 expect 执行多个命令
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "$password\n"}
}
expect "]#" {send "echo 'root1'\n"}
expect "]#" {send "echo 'root2'\n"}
send "exit\n"
expect eof
例六 shell脚本调用expect
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no" {send "yes\n";exp_continue}
"password" {send "$password\n"}
}
expect "]#" {send "echo 'ssh root1'\n"}
expect "]#" {send "echo 'ssh root2'\n"}
expect "]#" {send "exit\n"}
expect eof
EOF
标签:set,exp,批处理,send,expect,交互式,yes,password
From: https://www.cnblogs.com/baixisuozai/p/18148990