首页 > 其他 > 详细

expect 两种用法

时间:2019-10-09 18:05:39      阅读:136      评论:0      收藏:0      [点我收藏+]
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 

expect 两种用法

原文:https://blog.51cto.com/13620944/2440856

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!