shell脚本之免交互expect

发布时间 2023-08-17 19:39:46作者: Candy独角兽

1. 多行重定向

cat <<EOF #打印在屏幕上

cat <<a.txt #不打印在屏幕上

[root@localhost ~]#cat <<EOF
> HELLO
> HI
> OK
> EOF
HELLO
HI
OK
[root@localhost ~]#cat <<EOF >test  #传给test
HELLO
HI
OK
EOF

[root@localhost ~]#cat test   #查看test
HELLO
HI
OK

2. Expect

  • 定义:是建立在tcl(tool command language)语言基础上的一个工具,常被用于进行自动化控制和测试,解决shell脚本中交互的相关问题

  • Expect概述(与ssh相互配合使用)

    • 建立在tcl之上的工具
    • 用于进行自动化控制和测试
    • 解决shell脚本中交互相关的问题
  • 基本命令

    • 判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回
    • 只能捕捉由spawn启动的进程的输出
    • 用于接收命令执行后的输出,然后和期望的字符串匹配
  • send

    • 向进程发送字符串,用于模拟用户的输入
    • 该命令不能自动回车换行,一般要加\r(回车)
  • set

    • 设置超时时间,过期则继续执行后续指令
    • 单位是秒
    • timeout -1表示永不超时
    • 默认情况下,timeout是10秒
  • exp_continue

    • 允许expect继续向下执行指令
  • send_user

    • 回显命令,相当于echo

基本命令:

#脚本解释器
#!/usr/bin/expect(不用.sh结尾)

#spawn 后面通常跟一个Linux执行命令,表示开启一个会话、进程,并跟踪后续交互信息

例:spawn ssh 192.168.8.8

#expect
判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回;只能捕捉有swpan启动的进程输出
用于接受命令执行后的输出,然后和期望的字符串匹配

#send
向进程发送字符串,用于模拟用户的输入:该命令不能自动回车换行,一般要加 \r (回车) 或者\n 

```bash
#!/usr/bin/expect

#设置超时时间
set timeout 5

#参数传入
set hostname  [lindex $argv 0]  
#hostname=$1

set password  [lindex $argv 1] 
#password=$2
[root@localhost ~]#rpm -q expect   #先查看是否安装expect,没有安装需要先安装(yum install expect -y)
expect-5.45-14.el7_1.x86_64

[root@localhost ~]#rpm -q tcl
tcl-8.5.13-8.el7.x86_64

[root@localhost ~]#vim test
#!/usr/bin/expect
spawn ssh 192.168.8.106    #启动命令

expect {       #捕捉spawn启动的进程的输出
    "yes/no" { send "yes\r"; exp_continue }  #捕捉yes或no,exp continue代表继续捕捉
    "password" { send "123123\r" }    #继续捕捉

}

#expect eof    #代表结束,结束返回之前终端
interact       #代表结束,留在远程终端

[root@localhost ~]#chmod +x test


[root@localhost ~]#./test     #执行脚本
spawn ssh 192.168.8.106
The authenticity of host '192.168.8.106 (192.168.8.106)' can't be established.
ECDSA key fingerprint is SHA256:e+J0VzcMDN/wqEMfgMwrXc+Gef7K/KjlHOIBeDyWFeI.
ECDSA key fingerprint is MD5:df:f7:b3:3f:f6:0e:64:6a:95:db:a7:09:a0:18:89:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.8.106' (ECDSA) to the list of known hosts.
root@192.168.8.106's password: 
Last login: Tue Aug 15 09:12:01 2023 from 192.168.8.1
[root@localhost ~]#
[root@localhost ~]#exit
登出
Connection to 192.168.8.106 closed.
[root@localhost ~]#

[root@localhost ~]#vim /data/test
#!/usr/bin/expect
set hostname [ lindex $argv 0 ]

spawn ssh $hostname

expect {
    "yes/no" { send "yes\r";exp_continue }
    "password" { send "123123\r" }
}

#expect eof
interact

[root@localhost ~]#chmod +x /data/test
[root@localhost ~]#/data/test 192.168.8.106
spawn ssh 192.168.8.106
The authenticity of host '192.168.8.106 (192.168.8.106)' can't be established.
ECDSA key fingerprint is SHA256:e+J0VzcMDN/wqEMfgMwrXc+Gef7K/KjlHOIBeDyWFeI.
ECDSA key fingerprint is MD5:df:f7:b3:3f:f6:0e:64:6a:95:db:a7:09:a0:18:89:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.8.106' (ECDSA) to the list of known hosts.
root@192.168.8.106's password: 
Last login: Thu Aug 17 17:50:25 2023
[root@localhost ~]#exit
登出
Connection to 192.168.8.106 closed.


[root@localhost ~]#vim test.sh
#!/bin/bash
hostname=$1
password=$2

/usr/bin/expect <<EOF

spawn ssh root@$hostname

expect {
        "(yes/no)"
        {send "yes\r";exp_continue}
        "*password"
        {send "$password\r"}
}
expect "*]#"   #只要出现以]#结尾的就新建用户zhangsan
send "useradd zhangsan\r"
expect eof
EOF

[root@localhost ~]#bash test.sh 192.168.8.106 123123
spawn ssh root@192.168.8.106
root@192.168.8.106's password: 
Last login: Thu Aug 17 18:51:50 2023 from 192.168.8.100
[root@localhost ~]#useradd zhangsan
[root@localhost ~]#


[root@localhost ~]#id zhangsan
uid=1001(zhangsan) gid=1001(zhangsan) 组=1001(zhangsan)

#!/bin/bash
password="123123"

hostlist="
192.168.8.106
192.168.8.101
"

for i in $hostlist
do
/usr/bin/expect <<EOF

spawn ssh root@$i

expect {
        "(yes/no)"
        {send "yes\r";exp_continue}
        "*password"
        {send "$password\r"}
}
expect "*]#"
send "useradd zhangsan\r"
expect eof
EOF
done

3. awk数组

[root@localhost ~]#awk 'BEGIN{student[1]="TZONG";student[2]="YZONG";student[3]="IZONG";for(i in student){print i"---->"student[i]}}'
1---->TZONG
2---->YZONG
3---->IZONG

[root@localhost ~]#awk 'BEGIN{student["a"]="TZONG";student["b"]="YZONG";student["c"]="IZONG";for(i in student){print i"---->"student[i]}}'
a---->TZONG
b---->YZONG
c---->IZONG
[root@localhost ~]#awk 'BEGIN{student["aa"]="TZONG";student["b"]="YZONG";student["c"]="IZONG";for(i in student){print i"---->"student[i]}}'    #顺序可以变化
b---->YZONG
c---->IZONG
aa---->TZONG

4. 去除重复行实例

[root@localhost ~]#vim test
[root@localhost ~]#cat test
abc
def
ghi
abc
ghi
def

afa
ghi
[root@localhost ~]#awk '!a[$0]++' test   #输出不重复的行
abc
def
ghi

afa