Shell编程之免交互(Here Document免交互、Here Document常规用法、Expect基本命令、Expect执行方式)

Here Document免交互

什么是Here Document

Here document (here-document, here-text, heredoc, hereis, here-string, here-script)是一个文件文字或输入流文字:它是源代码文件的一部分,被视为它是一个单独的文件。该术语还用于使用类似语法,保留换行符和文本中的其他空格(包括缩进)的多行字符串文字形式。

Here Document 概述

使用I/O重定向的方式将命令列表提供给交互程序(I是in/O是out)

标准输入的一种替代品

语法格式

命令 <<标记  #自定义
...
...
标记  #自定义

Here Document 使用注意事项

标记可以使用任意合法字符

结尾的标记一定要顶格写,前面不能有任何字符

结尾的标记后面也不能有任何字符(包括空格)

开头标记前后的空格会被省略掉

Here Document常规用法

Here Document免交互

通过read命令接收输入并打印

[root@promote ~]# read a <<EOF   #EOF可以自定义
> this is test
> QWQ
> EOF	#收尾对应
[root@promote ~]# echo $a		#每一行只对应一次交互
this is test

使用免交互给用户设置密码

[root@shell ~]# vim 222.sh
#!/bin/bash
useradd zhangsan
passwd zhangsan <<EOF
abc123
abc123
EOF
[root@shell ~]# chmod +x 222.sh 
[root@shell ~]# ./222.sh 
更改用户 zhangsan 的密码 。
新的 密码:无效的密码: 密码少于 7 个字符
重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。

Here Document变量设定

变量替换

#!/bin/bash
a="test.txt"
b=world
cat > $a <<AA
hello $b
AA
~   
[root@promote test]# chmod +x test1.sh
[root@promote test]# ./test1.sh
[root@promote test]# cat test.txt 
hello world

变量设定

[root@promote test]# vim test1.sh 
#!/bin/bash
a="100"
b=$(cat  <<AA
aaa
bbb
$a
AA
)
echo $b

[root@promote test]# sh test1.sh 
aaa bbb 100

Here Document格式控制

关闭变量替换功能
只需给标记加上单引号

#!/bin/bash
a="100"
b=$(cat  <<'AA'
aaa
bbb
$a
AA
)
echo $b

[root@promote test]# sh test1.sh 
aaa bbb $a

去除每行前的TAB字符
只需在标记前面加上“-”

b=$(cat  <<-'AA'
        aaa
        bbb
        $a
AA
)

多行注释
只需把“:”加在<<前面

:<<AA
注释1
注释2
...
AA

Expect概述

建立在tcl之上的一个工具

用于进行自动化控制和测试

解决shell脚本中交互相关的问题

Expect基本命令

需要yum安装

yum -y install expect

expect

判断上次输出结果是否包含指定的字符串,如果用则立即返回,否则就等待超时时间返回

只能捕捉由spawn启动的进程的输出

用于接收命令执行后的输出,然后和期望的字符串匹配

send

向进程发送字符串,用于模拟用户的输入

该命令不能自动回车换行,一般要加\r模拟用户回车

spawn

启动进程,兵跟踪后续交互信息

结束符

expect eof
等待执行结束

intreact
执行完成后保持交货状态,把控制权交给控制台

set

设置超时时间,过期则继续后续指令(单位只秒)

timeout -1表示永不超时

默认情况下,timeout是10秒

exp_continue

允许expect继续向下执行指令

send_user

回显命令,相当于echo

免交互三个重点

spawn 	追踪输入的指令
expect  捕捉输入指令后出现的会话
send	根据捕捉不同的会话来发送对应的指令

Expect语法

单分支语法

expect "password:"{send "123456\r";}

多分支语法

expect"aaa"{send"AAA\r"}
expect"bbb"{send"BBB\r"}
expect"ccc"{send"CCC\r"}
expect{
	"aaa"{send"AAA\r"}
	"bbb"{send"BBB\r"}
	"ccc"{send"CCC\r"}
}
只要匹配到了其中任何一个,执行对应的send语句后就退出该expect语句
expect{
	"aaa"{send"AAA\r";exp_continue}
	"bbb"{send"BBB\r";exp_continue}
	"ccc"{send"CCC\r"}
}
exp_continue表示继续后面的匹配,
如果匹配到了aaa,执行完send语句后还要继续向下匹配bbb

Expect执行方式

直接执行
远程链接免交互实例

#!/usr/bin/expect
#超时
set timeout 20
#开启日志
log_file test.log
#显示信息
log_user 1
#定义变量
set hostname [lindex $a 0]
set password [lindex $b 1]
#追踪指令
spawn ssh root@${hostname}
#捕捉提示信息
expect {
        "connecting(yes/no)"
        {send "yes\r";exp_continue}
        "*password:"
        {send "${password}\r";}
}
#转交控制权
interact

[root@promote opt]# ./test2.sh 192.168.110.4 11925
第一个位置变量是IP192.168~  第二个位置变量是密码11925

嵌入执行

#!/bin/bash
a=$1
b=$2
#expect嵌入
/usr/bin/expect <<-EOF
spawn ssh root@${a}
#捕捉提示信息
expect {
        "connecting(yes/no)"
        {send "yes\r";exp_continue}
        "*password:"
        {send "${b}\r";}
}
expect "*]#"
send "exif\r"
expect eof
EOF

创建用户,并设置密码

#!bin/bash
a=$1
b=$2
useradd $a
/usr/bin/expect <<-EOF
spawn passwd ${a}
expect {
        "密码:"
        {send "$b\r";exp_continue}
        "密码:"
        {send "$b\r"}
}
EOF

[root@promote opt]# ./test2.sh aa 112233
更改用户 aa 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

猜你喜欢

转载自blog.csdn.net/CN_PanHao/article/details/107627788