理论+实验·Shell编程之免交互

理论+实验·Shell编程之免交互

Here Document概述

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

标准输入的一种替代品

语法格式

命令<<EOF	===>一般用EOF
...
...
EOF

Here Document使用注意事项

1:标记可以使用任意合法字符
2:结尾的标记一定要顶格写,前面不能有任何字符
3:结尾的标记后面也不能有任何字符(包括空格)
4:开头标记前后的空格会被省略掉

Here Document免交互

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

[root@localhost ~]# vim test.sh
#!/bin/bash
read i <<EOF
hello
EOF
echo $i
[root@localhost ~]# bash test.sh
hello

通过passwd给用户设置密码

[root@localhost ~]# vim test.sh
#!/bin/bash
useradd zhangsan
passwd zhangsan <<EOF
123123		===>passwd命令需要的密码和确认密码
123123
EOF
[root@localhost ~]# bash test.sh

Here Document变量设定

变量替换

[root@localhost ~]# vim test.sh
#!/bin/bash
doc_file="test.txt"
cat>$doc_file<<EOF
this is test
EOF
[root@localhost ~]# bash test.sh
[root@localhost ~]# cat test.sh
this is test
[root@localhost ~]# vim test.sh
#!/bin/bash
doc_file="test.txt"
i="first"
cat>$doc_file<<EOF
this is $i test
EOF
[root@localhost ~]# bash test.sh
[root@localhost ~]# cat test.sh
this is first test

变量设定

[root@localhost ~]# vim test.sh
#!/bin/bash
doc_file="test.txt"
testfile=$(<<EOF
this is test
$doc_file
EOF
)
echo $testfile
[root@localhost ~]# bash test.sh
this is first test test.txt

Here Document格式控制

关闭变量替换功能

[root@localhost ~]# vim test.sh
#!/bin/bash
cat<<'EOF'		===>单引号关闭变量替换
This is test.
$text
EOF
[root@localhost ~]# bash test.sh
This is test.
$text

去除每行之前的TAB字符 ===>’-'表示抑制行首的TAB作用

[root@localhost ~]# vim test.sh
#!/bin/bash
cat<<-'EOF'			===>'-'表示抑制行首的TAB作用
	This is test.
	$text
EOF
[root@localhost ~]# bash test.sh
This is test.
$text

Here Document多行注释

通过Here Document方式使Bash支持多行注释

语法格式

:<<EOF
第一行注释
第二行注释
...
EOF

示例

[root@localhost ~]# vim test.sh
#!/bin/bash
:<<EOF
This is test.
$text
EOF
echo "hello"
[root@localhost ~]# bash test.sh
hello

Expect概述

Expect

建立在tcl之上的一个工具 ===>yum -y install expect
用于进行自动化控制和测试
解决shell脚本中交互相关的问题

Expect安装

挂载光盘

制作本地YUM源

执行安装命令

[root@localhost ~]# yum -y install expect
[root@localhost ~]# rpm -qa|grep expect
expect-5.45-14.el7_1.x86_64
[root@localhost ~]# rpm -qa|grep tcl
tcl-8.5.13-8.el7.x86_64

基本命令

expect

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

send

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

spawn [email protected]	===>追踪指令---类似if条件
expect "connecting (yes/no)?"	===>捕捉会话---匹配字符串
send yes\r	===>自动发送指令---执行

spawn

启动进程,并追踪后续交互信息

结束符

expect eof ===>执行自动话任务通常使用expect eof
​ 等待执行结束
interact
​ 执行完成后保持交互状态,把控制权交给控制台

set ===>日志,信息展示

设置超时事件,国企则继续执行后续指令
单位是秒
timeout -1表示永不超时
默认情况下,timeout是10秒

exp_continue ===>不跳过继续匹配

允许expect继续向下执行指定

send_user

回显命令,相当于echo

接收参数

Expect脚本可以接受从bash传递的参数
可以使用[lindex $argv n]获得
n从0开始,分别表示第一个,第二个,第三个…参数
set 变量名(filename) [lindex $argv 0]

Expect语法

单一分支语法 ===>匹配的内容跟发送的内容需要加上双引号

expect "password:" {send "你的密码\r";}

多分支模式语法

expect "aaa" {send "AAA\r"}
expect "bbb" {send "BBB\r"}
expect "ccc" {send "CCC\r"}		===>send命令不具备回车换行功能,一般要加\r或\n
expect {
"aaa" {send "AAA\r"}
"bbb" {send "BBB\r"}
"ccc" {send "CCC\r"}		===>只要匹配了aaa或bbb或ccc中的任意一个,执行相应的send语句后退出该expecct语句
}
expect {
"aaa" {send "AAA\r";exp_continue}
"bbb" {send "BBB\r";exp_continue}
"ccc" {send "CCC\r"}		===>exp_continue表示继续后面的匹配,如果匹配了aaa,执行完send语句后还要继续向下匹配bbb
}

Expect执行方式

直接执行

[root@localhost ~]# more test.sh
#!/usr/bin/expect		===>Expect二进制文件的路径
	set timeout 60		===>超时事件60秒
	log file test.log		===>开启日志
	log_user 1		===>显示信息    0不显示
	set hostname [lindex $argv 0]		===>定义变量
	set password [lindex $argv 1]
	spawn ssh root@$hostname		===>追踪指令
	expect {		===>捕捉提示信息
		"(yes/no)"
		{send "yes\r"; exp_continue}
		"*password"
		{send "$password\r"}
}
interact		===>转交控制权
[root@localhost ~]# chmod a+x test.sh
[root@localhost ~]#./test.sh

嵌入执行

#!/bin/bash
hostname=$1
password=$2
/usr/bin/expect<<-EOF	===>Expect开始标志
spawn ssh root@${hostname}
expect {
"(yes/no)"
{send "yes\r";exp_continue}
"*password:"
{send "$password\r";}
}
expect "*]#"
send "exit\r"
expect eof
EOF		===>Expect结束标志,EOF前后不能由空格

猜你喜欢

转载自blog.csdn.net/weixin_47153668/article/details/107632266