您的位置:首页 > 其它

expect 两种用法

2019-10-09 17:16 661 查看

yum install expect -y
#先安装expect

1.测试用法

#!/usr/bin/expect
#解释语言,这边运行要以./运行,bash运行会报错
spawn ssh root@192.168.0.14
#启动新的进程
expect "*password:"
#进程接收字符串,匹配
send "yxy7714707@\r"
#前面匹配到了就输入 “ ” 里的内容
expect "*#"
send "ifconfig>>123.txt\r"
send "exit\r"
interact

2.在sh脚本里调用

#!/bin/bash
ip=$1
#传递参数
user=$2
password=$3
expect <<EOF
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
#一个交互用一个expect{} 括起来,这个交互就是登陆的
expect "]#" { send "date>>123.txt\n" }
expect "]#" { send "exit\n" }
#退出
expect eof
EOF
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: