首页 > 系统服务 > 详细

linux 的 expect 自动交互

时间:2019-10-29 12:37:51      阅读:78      评论:0      收藏:0      [点我收藏+]

https://www.jianshu.com/p/0194cbd70d39 

https://www.cnblogs.com/saneri/p/10819348.html  参考

expect是一个自动化交互套件,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信。

expect自动交互流程:

  spawn启动指定进程---expect获取指定关键字---send向指定程序发送指定字符---执行完成退出.

expect 最关键、常用的四个命令:

命令说明
send 用于向交互对象发送字符串
expect 从交互对象接收字符串
spawn 它主要的功能是给运行进程加个壳,用来传递交互指令
interact 执行完成后保持交互状态,把控制权交给控制台,这个时候便可以手工操作。如果没有该命令,命令完成后即退出。

   其他命令:

    exp_continue  :

    set timeout : 设置超时时间,计时单位是:秒,timeout -1 为永不超时。

    $argv 参数数组: 其中通过 [lindex $argv n] 可以获得第 n 个参数的值,调用脚本  ./xxx.sh  root   123456   192.168.199.123   分别传参数到 对应的变量

set username [lindex $argv 0] 
set password [lindex $argv 1] 
set hostname [lindex $argv 2] 
    

1. 登录远程host并且不退出

  执行用  expect  xxx.sh  或者  ./xxx.sh   ,不能用 sh xxx.sh

#!/usr/bin/expect

set timeout 5 # 跳板机1 set host "10.17.234.145" set username "dfs" set password "qwe200" set host1 "39.17.121.187" set username1 "scdd" set password1 "qwe100" spawn ssh $username@$host expect "*assword:*" {send "$password\n"} expect "*]*" {send "ssh -p 22222 $username1@$host1\n"} expect "*assword*" {send "$password1\n"} expect "*]*" {send "su\n"} expect "*:*" {send "$password1\n"} interact

  interact  会使得最后的 终端停留在 登录上去的 host上。

  expect 传入参数    ./xxx.sh  234

#!/usr/bin/expect
set timeout 5

set host [lindex $argv 0]

spawn ssh 192.168.199.$host

expect {
        "*yes/no*" {send "yes\r";exp_continue}
        "*assword*" {send "qwe123\r"}
}

interact  # 用这个不会退出
#expect eof  看到好多自动登录用的 这个,但是这个会 自动登出,不会停留在登录的host上。
exp_continue  附加于某个 expect 判断项之后,可以使该项被匹配后,还能继续匹配该 expect 判断语句内的其他项。我把这个理解为 类似 if,如果能匹配到前面的就 send ,不能就匹配下面的。

linux 的 expect 自动交互

原文:https://www.cnblogs.com/chenpython123/p/11757274.html

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