shell编程之免交互(Here Document+Expect)

一、Here Document概述

  • 使用I/O重定向的方式将命令列表提供给交互式程序
  • 标准输入的一种替代品

语法格式

命令<<标记
...
...
标记

1.1 Here Document使用注意事项

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

1.2 Here Document免交互

  • 通过read命令接收输入并打印
[root@localhost ~]# vim hello.sh
#!/bin/bash
read i <<EOF
hello world
EOF
echo $i
[root@localhost ~]# chmod +x hello.sh 
[root@localhost ~]# ./hello.sh 
hello world
  • 通过passwd给用户设置密码
[root@localhost ~]# vim user.sh
#!/bin/bash
passwd lisi <<EOF
123abc    
123abc
EOF
[root@localhost ~]# ./passwd.sh 
更改用户 lisi 的密码 。
新的 密码:无效的密码: 密码少于 7 个字符
重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。

1.3 Here Document变量设定

  • 变量替换
[root@localhost ~]# vim user.sh 
#!/bin/bash
name="cllt"
filename="test.txt"
cat > $filename <<EOF
this is $name.com
EOF
[root@localhost ~]# cat test.txt 
this is cllt.com
  • 变量设定
[root@localhost ~]# vim user.sh 
#!/bin/bash
first="hello"
filename=$(cat <<EOF
this is cllt.com
my name is cllt
$first
EOF
)
echo $filename
[root@localhost ~]# ./user.sh 
this is cllt.com my name is cllt hello

1.4 Here Document格式控制

  • 关闭变量替换功能
[root@localhost ~]# vim user.sh 
#!/bin/bash
first="hello"
cat <<'EOF'            '单引号关闭变量替换'
this is cllt.com
$first
EOF
[root@localhost ~]# ./user.sh 
this is cllt.com
$first
  • 去除每行之前的tab字符
[root@localhost ~]# vim user.sh 
#!/bin/bash
first="hello"
cat <<-EOF           '-表示抑制行首的TAB作用'
        this is cllt.com
        $first
EOF
[root@localhost ~]# ./user.sh 
this is cllt.com
hello

1.5 Here Document多行注释

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

[root@localhost ~]# vim user.sh 
#!/bin/bash
first="hello"
: <<EOF                 '标记之间为注释内容'
this is cllt.com
$first
EOF
echo "lemon"
[root@localhost ~]# ./user.sh 
lemon

二、Expect概述

  • Expect是建立在tcl基础上的一个工具,Expect是用来进行自动化控制和测试的工具。主要解决shell脚本中不可交互的问题,对于大规模的linux运维很有帮助
  • 在Linux运维和开发中,我们经常需要远程登录服务器进行操作,在登录的过程是一个交互的过程,可能会需要输入yes/no password等信息。为了模拟这种输入,可以使用Expect脚本

在Linux系统中expect一般不自带,需要自己安装

yum install expect -y

2.1 基本命令

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

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

  • spawn
    启动进程,并跟踪后续交互信息

  • expect eof(结束符)
    等待执行结束(执行自动化任务通常使用expect eof)

  • interact(结束符)
    执行完成后保持交互状态,把控制权交给控制台

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

  • exp_contiune
    允许expec继续向下执行指令

  • send_user
    回显命令,相当于echo

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

2.2 Expect语法

  • 单一分支语法
expect "password: " {send "mypassword\r“;}
  • 多分支模式语法
expect "aaa" {send“AAA\r"}    'send命令不具备回车换行功能,一般要加\r或\n'
expect "bbb" {send“BBB\r"}
expect "ccc" {send“CCC\r"}
expect {    '只要匹配了aaa或bbb或ccc中的任何一个,执行相应的send语句后退出该expect语句'
"aaa"{send“AAA\r"}
"bbb" {send“BBB\r"}
"CCC" {send“CCC\r"}
}
expect {   'exp_ continue表示继续后面的匹配,如果匹配了aaa, 执行完send语句后还要继续向下匹配bbb'
"aaa" {send“AAA"; exp_ continue}
"bbb" {send“BBB"; exp_ continue}
"ccc" {send“CCC"}
}

2.3 Expect执行方式

  • 直接执行
[root@localhost ~]# vim demo01.sh 
#!/usr/bin/expect
#超时时间
set timeout 20
#开启日志
log_file test.log
#显示信息
log_user 1

#定义变量
set hostname [lindex $argv 0]
set password [lindex $argv 1]

#追踪指令
spawn ssh root@${hostname}

#捕捉提示信息
expect {
     "connecting (yes/no)"
     {send "yes\r";exp_continue}
     "*password:"
     {send "${password}\r";}
}
#转交控制权
interact
[root@localhost ~]# ./demo01.sh 192.168.179.144 123456
spawn ssh root@192.168.179.144
root@192.168.179.144's password: 
Last login: Tue Jul 28 09:34:58 2020 from 192.168.179.1
[root@server ~]# 
  • 嵌入执行
[root@localhost ~]# vim demo02.sh 
#!/bin/bash
hostname=$1
password=$2
#expect嵌入
/usr/bin/expect <<-EOF
spawn ssh root@${hostname}

#捕捉提示信息
expect {
     "connecting (yes/no)"
     {send "yes\r";exp_continue}
     "*password:"
     {send "${password}\r";}
}
expect "*]#"
send "exit\r"
expect eof
EOF
[root@localhost ~]# ./demo02.sh 192.168.179.144 123456
spawn ssh root@192.168.179.144
root@192.168.179.144's password: 
Last login: Tue Jul 28 09:35:17 2020 from 192.168.179.100
[root@server ~]# exit
登出
Connection to 192.168.179.144 closed.
[root@localhost ~]# 

三、实验案例

3.1 创建用户并设置密码

[root@localhost ~]# vim demo03.sh 
#!/bin/bash
users=$1
passwd=$2
useradd ${users}
#expect嵌入
/usr/bin/expect <<-EOF
spawn passwd ${users}
#捕捉提示信息
expect {
     "密码:"
     {send "$2\r";exp_continue}
     "密码:"
     {send "$2\r";}
}
#expect eof
EOF
[root@localhost ~]# ./demo03.sh wangwu 123456
spawn passwd wangwu
更改用户 wangwu 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

3.2 实现ssh自动登录

[root@localhost ~]# vim demo04.sh 

#!/usr/bin/expect
set timeout 5
set hostname [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh ${hostname}
expect {
     "Connection refused" exit
     "Name or service not known" exit
     "connecting (yes/no)"{send "yes\r";exp_continue}
     "*password:"{send "${password}\r"}
}
interact
exit
[root@localhost ~]# ./demo04.sh 192.168.179.144 123456
spawn ssh 192.168.179.144
root@192.168.179.144's password: 
Last login: Tue Jul 28 09:50:59 2020 from 192.168.179.100
[root@server ~]# exit
[root@localhost ~]# ./demo04.sh 
spawn ssh root@
ssh: Could not resolve hostname : Name or service not known
[root@localhost ~]# 

猜你喜欢

转载自blog.csdn.net/weixin_47153988/article/details/107627659