文章目录

expect

一、 expect介绍

​ ​​expect​​​是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预。说白了,​​expect​​就是一套用来实现自动交互功能的软件。需要安装

yum install -y expect
1、expect基础

在使用​​expect​​时,基本上都是和以下四个命令打交道:

命令

作用

spawn

启动新的进程

expect

从进程接收字符串

send

用于向进程发送字符串

interact

允许用户交互

  • ​spawn​​​命令用来启动新的进程,​​spawn​​​后的​​expect​​​和​​send​​​命令都是和使用​​spawn​​启动的新进程进行交互。
  • ​expect​​​通常用来等待一个进程的反馈,我们根据进程的反馈,再使用​​send​​命令发送对应的交互命令。
  • ​send​​命令接收一个字符串参数,并将该参数发送到进程。
  • ​interact​​​命令用的其实不是很多,一般情况下使用​​spawn​​​、​​expect​​​和​​send​​​和命令就可以很好的完成我们的任务;但在一些特殊场合下还是需要使用​​interact​​​命令的,​​interact​​​命令主要用于退出自动化,进入人工交互。比如我们使用​​spawn​​​、​​send​​​和​​expect​​​命令完成了ftp登陆主机,执行下载文件任务,但是我们希望在文件下载结束以后,仍然可以停留在ftp命令行状态,以便手动的执行后续命令,此时使用​​interact​​命令就可以很好的完成这个任务。
2、总结expect自动应答的基本步骤
第一步: 运行一个程序或命令=>  spawn 命令信息
第二步: 识别产生信息关键字=> expect 捕获关键字 {send 应答信息}
第三步: 根据识别关键做处理=> send 应答信息

二 、expect实例

1、编写自动应答脚本
#!/usr/bin/expect

spawn ssh root@127.0.0.1 uptime

expect "yes/no"
send "yes\n"

expect "*assword"
send "123\n"

expect eof
===================================================================

# 解释:
#1、#!/usr/bin/expect -f:使用expect来解释该脚本

#2、spwan:
spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的。它主要的功能是给ssh运行进程加个壳,用来传递交互指令;

#3、expect:
expect "*assword":这里的expect也是expect的一个内部命令,这个命令的意思是判断上次输出结果里是否包含“password”的字符串,如果有则立即返回;否则就等待一段时间后返回,这里等待时长就是前面设置的30秒;

#4、send:
send "1\n":当匹配到对应的输出结果时,就发送密码到打开的ssh进程,执行交互动作;
2、为脚本设置超时时间
首次登陆之后,再次登陆,就不会出现yes/no的提示了,所以上述脚本再次运行会出现spawn 命令出现交互式提问的expect 匹配不上的情况,此时脚本会阻塞在原地,我们可以set timeout 3设置超时时间,单位为秒,默认情况下是10秒,以保障脚本超时则结束

#!/usr/bin/expect -f

spawn ssh root@192.168.15.61 uptime

set timeout 3 # 某一条expect语句在原地匹配,超过了3秒,无论是否匹配成功都会继续执行下一条指令

expect "yes/no"
send "yes\n"

expect "*assword"
send "123\n"

expect eof
3、规整脚本格式
设置超时时间的目的仅仅只是为了让脚本不要一直卡在原地,要真正解决上述问题,需要改写成下述形式
#!/usr/bin/expect -f

spawn ssh root@192.168.15.61 hostname

# 注意
# 1、{}一定要换行
# 2、下述语句就一个expect,代表匹配了一次,会匹配完一行就匹配下一行
expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "123\n"}
}

expect eof
4、练习

# cpu负载过载解决思路
# 1.top进程
# 2.lo进程
#3.黑客攻击
[root@web01 ~]# ps aux |grep "D+"
root 7708 0.0 0.2 112708 976 pts/0 R+ 20:32 0:00 grep --color=auto D+
[root@web01 ~]# cat /proc/sys/vm/swappiness
30
[root@web01 ~]# # 在 /etc/sysctl.conf 里面添加 vm.swappiness=0 重启计算机(^C
[root@web01 ~]# tail -f /var/log/s
sa/ secure spooler
[root@web01 ~]# tail -f /var/log/secure
# 尽量别用默认22端口
[root@web01 ~]# cat /etc/ssh/sshd_config |grep 22
#Port 22
[root@aliyun ~]# cat 1.sh 
#!/usr/bin/expect -f

spawn ssh rootn@127.0.0.1

set timeout -1 # 设置为-1代表永不超时,如果expect没有捕捉到就一直停在原地

expect {
"yes/no" {send "yes\n"}
}

expect {
"password" {send "123\n"}
}

expect "*mm*"
send "ls\n"

expect "\$"
send "pwd\n"

expect "\$"
send "exit\n" # 注意一定要输入exit结束信号

expect eof # 最后关闭匹配
5、interact交互

​interact​​:执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了。如果没有这一句登录完成后会退出,而不是留在远程终端上。

[root@egon ~]# cat test.sh 
#!/usr/bin/expect -f

spawn ssh root@192.168.15.61

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

interact
[root@egon ~]#
[root@egon ~]# ./test.sh
spawn ssh root@192.168.15.61
root@192.168.15.61's password:
Last login: Wed Aug 26 11:48:04 2021 from egon
+--------------------------------------------+
| |
| 你当前登录的是支付业务后台数据库服务 |
| 请不要删库 |
| |
+--------------------------------------------+
[root@egon ~]# pwd
/root
[root@egon ~]# echo "hello"
hello
[root@egon ~]# exit
登出
Connection to 192.168.15.61 closed.
[root@egon ~]#

三、 为expect脚本传参

shell脚本中的变量无法直接在expect中使用的,若expect需要使用变量,有两种方法可以选择:
方式一:自己定义
#!/usr/bin/expect -f

set timeout -1
set user "root"
set ip "192.168.12.20"
set cmd "hostname"
set pass "1"


spawn ssh $user@$ip $cmd

expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$pass\n"}
}

expect eof
方法二:可以通过下述方式引入shell变量,注意此时解释器换成#!/bin/bash
#!/bin/bash

user="root"
ip="192.168.12.20"
cmd="hostname"
pass="1"

expect << EOF
spawn ssh $user@$ip $cmd

expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$pass\n"}
}

expect eof
EOF

2、此外,expect脚本还可以从命令行获取参数

在expect中,​​$argc​​​表示参数个数,而参数值存放在​​$argv​​​中,比如取第一个参数就是​​[lindex $argv 0]​​,以此类推。

[root@egon ~]# cat test.sh 
#!/usr/bin/expect -f

if {$argc != 4} {
puts "Usage:./script.sh <ip> <username> <password> <cmd>"
exit 1
}

set ip [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set cmd [lindex $argv 3]
set timeout -1

spawn ssh $user@$ip $cmd

expect {
"yes/no" {send "yes\r";exp_continue}
"*assword" {send "$pass\n"}
}

expect eof
[root@egon ~]# ./test.sh
Usage:./script.sh <ip> <username> <password> <cmd>
[root@egon ~]# ./test.sh 192.168.12.20 root 1 hostname
spawn ssh root@192.168.12.20 hostname
root@192.168.12.20's password:
egon
[root@egon ~]#

能够在工作中熟练的使用Shell脚本就可以很大程度的提高工作效率,如果再搭配上expect,那么很多工作都可以自动化进行,对工作的展开如虎添翼。如果你会的话,你的视野将会更加开阔,那个时候你又会“嫌弃”expect了。