Shell免交互应用
Here Document
使用I/O重定向的方式将命令列表提供给交互程序
标准输入的一种代替品
语法格式:
命令 <<标记
...
...
标记
注意事项:
- 标记可以使用任意合法字符
- 结尾的标记一定要定格写,前面不能有任何字符
- 结尾的标记后面也不能有任何字符(包括空格)
- 开头标记前后的空格会被省略掉
Here Document免交互
通过read命令接收输入并打印
[root@server2 ~]# read a
abc
[root@server2 ~]# echo $a
abc
免交互脚本
#!/bin/bash
# read免交互
read z <<e
abc
e
echo $z
通过passwd给用户设置密码
[root@server2 ~]# useradd zhangsan 先创建用户,为设置密码
[root@server2 ~]# vim passwd.sh
#!/bin/bash
# 给用户设置密码
passwd zhangsan <<e
123456
123456
e
[root@server2 ~]# chmod +x passwd.sh
[root@server2 ~]# ./passwd.sh
Here Document变量设定
变量替换
[root@server2 ~]# vim file.sh
#!/bin/bash
# 查看内容重定向给一个文件
a=/root/a.txt
b=abc 变量可见
cat > $a << EOF
hello $b
EOF
[root@server2 ~]# chmod +x file.sh
[root@server2 ~]# ./file.sh
[root@server2 ~]# cat a.txt
变量设定
[root@server2 ~]# vim 123.sh
#!/bin/bash
# 多行内容赋值给变量
123=abc
b=$(cat << EOF 多行内容整合为一个整体
This is a file.
This is a text.
$123
EOF
)
echo $b
[root@server2 ~]# chmod +x 123.sh
[root@server2 ~]# ./123.sh
Here Document格式控制
关闭变量替换功能
#!/bin/bash
# 多行内容赋值给变量
123=abc
b=$(cat << 'EOF' 单引号关闭变量替换
This is a file.
This is a text.
$123
EOF
)
echo $b
去除每行之前的TAB字符
#!/bin/bash
# 多行内容赋值给变量
123=abc
b=$(cat << -'EOF' - 表示抑制行首Tab
This is a file.
This is a text.
$123
EOF
)
echo $b
Here Document多行注释
通过Here Document方式使Bash支持多行注释
语法格式:
:<<DO-NOTHING
第一行注释
第二行注释
....
DO-NOTHING
示例
#!/bin/bash
# 注释
:<<EOF
hello
hi 注释,内容不显示 等同于配置文件中的" # "号
good morning
EOF
echo abc
Expect
建立在tcl上的一个工具;
用于进行自动化控制和测试;
解决shell脚本中交互相关的问题。
Spawn的使用
启动进程,并跟踪后续交互信息
Expect的使用
1、判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回;
2、只能捕捉由spawn启动的进程的输出;
3、用于接收命令执行后的输出,然后和期望的字符串匹配。
send的使用
1、向进程发送字符串,用于模拟用户的输入;
2、该命令不能自动回车换行,一般要加\r(回车)或\n(换行)
结束符
1、expect eof:等待执行结束;
2、interact:执行完成后保持交互状态,把控制权交给控制台。
set的使用
1、设置超时时间,过期则继续执行后续指令;
2、单位是秒;
3、timeout -1 表示永不超时;
4、默认情况下,timeout是10秒。
exp_continue的使用
允许expect继续向下执行指令
如果有一条语句错误,不加exp_continue,就会到此为止,退出;加上exp_continue会继续执行后续语句,不退出。
send_user的使用
回显命令,相当于echo
接收参数
Expect脚本可以接受从bash传递的参数;
也可以使用[lindex $argv n]获得;
n从0开始,分别表示第一个,第二个,第三个…参数。
[lindex $argv 0]相当于/bin/bash的$1
单分支和多分支语法
单一分支语法(send最后要加分号)
expect "password:" {send "123456\r";}
多分支模式语法
第一种
expect "aaa" {send "AAA\r"}
expect "aaa" {send "AAA\r"}
expect "aaa" {send "AAA\r"}
第二种
expect {
"aaa" {send "AAA\r"}
"bbb" {send "BBB\r"}
"ccc" {send "CCC\r"}
}
以上两种只要匹配了aaa 或bbb或ccc中的任何一个,执行相应的send语句后就会退出该expect语句。
下一种加入了exp_continue表示继续后面的匹配,如果匹配了aaa,执行完send语句后还会继续向下匹配bbb
捕捉内容要用双引号引起来。
expect {
"aaa" {send "AAA\r";exp_continue}
"bbb" {send "BBB\r";exp_continue}
"ccc" {send "CCC\r"}
}
执行方式
直接执行
[$argv 0],代表位置变量$1
[$argv 1],代表位置变量$2
#!/usr/bin/expect 是Expect二进制文件的路径
[root@localhost~]cat test.sh
#!/usr/bin/expect
set timeout 60 ##设置超时时间
log file test.log
log_user 1 ##展示日志信息,展示写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 ##将权限转交控制台
注意:只能用 ./ 来执行,因为不是bash环境了
嵌入执行
[root@localhost~]cat b.sh
#!/bin/bash
hostname=$1
password=$2
expect<<-EOF
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前后不能有空格'
免密登录示例
#!/bin/bash
yum -y install expect
ip=$1
passwd=$2
/usr/bin/expect<<EOF
spawn ssh-keygen -t rsa
expect {
"(/root/.ssh/id_rsa)"
{send "\r";exp_continue}
"(empty for no passphrase)"
{send "\r";exp_continue}
"passphrase again"
{send "\r"}
}
expect eof
EOF
/usr/bin/expect<<EOF
spawn ssh-copy-id $1
expect {
"(yes/no)"
{send "yes\r";exp_continue}
"password"
{send "$2\r"}
}
expect eof
EOF
ssh root@$1