expect

交互式转化批处理工具 expect

expect 是由Don Libes基于Tcl( Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助 expect 处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

expect 语法:

expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]

常见选项:

-c:从命令行执行expect脚本,默认expect是交互地执行的

-d:可以输出输出调试信息

示例:

expect -c 'expect "/n" {send "pressed enter/n"}'
expect  -d ssh.exp

expect中相关命令

  • spawn 启动新的进程
  • expect 从进程接收字符串
  • send 用于向进程发送字符串
  • interact 允许用户交互
  • exp_continue 匹配多个字符串在执行动作后加此命令

expect最常用的语法(tcl语言:模式-动作)

单一分支模式语法:

[root@centos8 test]#expect
expect1.1> expect "hi" {send "You said hi/n"}
hahahixixi
You said hi

匹配到hi后,会输出“you said hi”,并换行

多分支模式语法:

[root@centos8 test]#expect
expect1.1> expect "hi" { send "You said hi/n" } "hehe" { send "Hehe yourself/n" } "bye" { send "Good bye/n" }
hehe
Hehe yourself
expect1.2> expect "hi" { send "You said hi/n" } "hehe" { send "Hehe yourself/n" } "bye" { send "Good bye/n" }
bye
Good bye
expect1.3> expect "hi" { send "You said hi/n" } "hehe" { send "Hehe yourself/n" } "bye" { send "Good bye/n" }
hi
You said hi
expect1.4>

匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:

expect {
    "hi" { send "You said hi/n"}
    "hehe" { send "Hehe yourself/n"}
    "bye" { send  " Good bye/n"}
}

范例1:

#!/usr/bin/expect
spawn scp /etc/fstab 10.0.0.7:/data
expect {
        "yes/no" { send "yes/n";exp_continue }
        "password" { send  "magedu/n" }
}
expect eof

范例2:

#!/usr/bin/expect
spawn ssh 10.0.0.7
expect {
        "yes/no" { send "yes/n";exp_continue }
        "password" { send  "magedu/n" }
}
interact

范例3:expect 变量

#!/usr/bin/expect
set ip 10.0.0.7
set user root
set password magedu
set timeout 10
spawn ssh user@ip
expect {
        "yes/no" { send "yes/n";exp_continue }
        "password" { send "$password/n" }
}
interact

范例4:expect 位置参数

#!/usr/bin/expect
set ip [lindex argv 0] 
set user [lindexargv 1]
set password [lindex argv 2]
spawn sshuser@ip
expect {
        "yes/no" { send "yes/n";exp_continue }
        "password" { send "password/n" }
}
interact
#./ssh3.exp 192.168.8.10 root magedu

范例5:expect 执行多个命令

#!/usr/bin/expect
set ip [lindex argv 0] 
set user [lindexargv 1]
set password [lindex argv 2]
set timeout 10
spawn sshuser@ip
expect {
        "yes/no" { send "yes/n";exp_continue }
        "password" { send "password/n" }
}
expect "]#" { send "useradd haha/n" }
expect "]#" { send "echo magedu |passwd --stdin haha/n" }
send "exit/n"
expect eof
#./ssh4.exp 10.0.0.7 root magedu

范例6:shell脚本调用expect

#!/bin/bash
ip=1 
user=2
password=3
expect <<EOF
set timeout 20
spawn sshuser@ip
expect {
        "yes/no" { send "yes/n";exp_continue }
        "password" { send "password/n" }
}
expect "]#" { send "useradd hehe/n" }
expect "]#" { send "echo magedu |passwd --stdin hehe/n" }
expect "]#" { send "exit/n" }
expect eof
EOF 

#./ssh5.sh 192.168.8.10 root magedu

范例7: shell脚本利用循环调用expect在CentOS和Ubuntu上批量创建用户

#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#QQ:            29308620
#Date:          2020-01-06
#FileName:      expect6.sh
#URL:           http://www.magedu.com
#Description:       The test script
#Copyright (C):     2020 All rights reserved
#********************************************************************
NET=10.0.0
user=root
password=magedu

for ID in 6 7 111;do
ip=NET.ID

expect <<EOF
set timeout 20
spawn ssh user@ip
expect {
        "yes/no" { send "yes/n";exp_continue }
        "password" { send "$password/n" }
}
expect "#" { send "useradd test/n" }
expect "#" { send "exit/n" }
expect eof
EOF
done

本文链接:http://www.yunweipai.com/34357.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52556.html

(0)
上一篇 2021年8月6日
下一篇 2021年8月6日

相关推荐

发表回复

登录后才能评论