您的位置:首页 > 大数据 > 云计算

《云计算》shell脚本的基础应用(5)

2019-06-05 22:43 786 查看

4.expect预期交互
问题
本案例要求编写一个expect脚本,实现SSH登录的自动交互:
提前准备好目标主机,IP地址为192.168.4.5
用户名为mike、密码为1234567
执行脚本后自动登入,并且在目标主机建立测试文件 /tmp/mike.txt
方案
expect是基于TCL编写的自动交互式程序,可以为交互式过程(比如FTP、SSH等登录过程)自动输送预先准备的文本或指令,而无需人工干预。触发的依据是预期会出现的特征提示文本。
常见的expect指令:
定义环境变量:set 变量名 变量值
创建交互式进程:spawn 交互式命令行
触发预期交互:expect “预期会出现的文本关键词:” { send “发送的文本\r” }
在spawn建立的进程中允许交互指令:interact
步骤
实现此案例需要按照如下步骤进行。
步骤一:准备expect及SSH测试环境
1)安装expect工具
[root@svr5 ~]# yum -y install expect //安装expect
… …
Installed:
expect.x86_64 0:5.44.1.15-5.el6_4
Dependency Installed:
tcl.x86_64 1:8.5.7-6.el6

[root@svr5 ~]# which expect //确认expect路径
/usr/bin/expect
2)准备SSH测试环境
以本机192.168.4.5为例,只要启用sshd服务,并添加mike用户即可:
[root@svr5 ~]# service sshd restart
停止 sshd: [确定]
正在启动 sshd: [确定]

[root@svr5 ~]# useradd mike
[root@svr5 ~]# echo 1234567 | passwd --stdin mike
更改用户 mike 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
步骤二:编写expect_ssh脚本,实现免交互登录
1)任务需求及思路分析
在SSH登录过程中,如果是第一次连接到该目标主机,则首先会被要求接受密钥,然后才提示输入密码:
[root@svr5 ~]# ssh mike@192.168.4.5 //连接目标主机
The authenticity of host ‘192.168.4.5 (192.168.4.5)’ can’t be established.
RSA key fingerprint is 58:a0:d6:00:c7:f1:34:5d:6c:6d:70:ce:e0:20:f8:f3.
Are you sure you want to continue connecting (yes/no)? yes //接受密钥
Warning: Permanently added ‘192.168.4.5’ (RSA) to the list of known hosts.
mike@192.168.4.5’s password: //验证密码
Last login: Thu May 7 22:05:44 2015 from 192.168.4.5
[mike@svr5 ~]$ exit //返回客户端
logout
Connection to 192.168.4.5 closed.
在上述过程中,第一次交互发生在出现“… …(yes/no)?”的提示时,需要提供“yes”;而第二次交互发生在出现“… …password:”提示时,需要提供登录用户的密码;交互登录成功以后,需要在“[mike@svr5 ~]KaTeX parse error: Expected 'EOF', got '#' at position 77: …: [root@svr5 ~]#̲ ssh mike@192.1… exit //返回客户端
logout
Connection to 192.168.4.5 closed.
综上所述,应该预先知道SSH连接目标主机的正常交互过程。需要在脚本中来实现这些交互过程时,就可以利用expect工具,通过spawn指令来创建ssh登录进程,并设置expect指令来识别预期会出现的提示文本、提供相应的文本,甚至进一步发送相应的子进程交互指令。其中用户名、IP地址可以提前用set设好变量,这样更加方便。
2)根据实现思路编写脚本文件
脚本内容参考如下:
[root@svr5 ~]# vim expect_ssh.sh
#!/usr/bin/expect
set host 192.168.4.5 #//定义变量
set user mike
set password “1234567”
spawn ssh user@user@user@host #//创建交互式进程
expect “password:” { send “KaTeX parse error: Can't use function '\r' in math mode at position 9: password\̲r̲" } #//自动发送密…user@” { send “pwd > /tmp/$user.txt ; exit\r” }
#//发送交互式命令
interact #//允许交互式环境

[root@svr5 ~]# chmod +x expect_ssh.sh
3)验证、测试脚本
执行脚本前,目标主机上并没有/tmp/mike.txt文件:
[root@svr5 ~]# ls /tmp/mike.txt
ls: 无法访问/tmp/mike.txt: 没有那个文件或目录
执行expect_ssh.sh自动登录脚本:
[root@svr5 ~]# expect_ssh.sh
spawn ssh mike@192.168.4.5
mike@192.168.4.5’s password:
Last login: Mon May 11 12:08:47 2015 from 192.168.4.5
pwd > /tmp/mike.txt ; exit
[mike@svr5 ~]$ pwd > /tmp/mike.txt ; exit
logout
Connection to 192.168.4.5 closed.
再次检查目标主机,会看到已经建立了/tmp/mike.txt文件,说明expect自动登录并远程执行命令成功:
[root@svr5 ~]# ls -l /tmp/mike.txt
-rw-rw-r–. 1 mike mike 11 5月 11 12:17 /tmp/mike.txt
5.使用正则表达式
问题
本案例要求熟悉正则表达式的编写,完成以下任务:
利用egrep工具练习正则表达式的基本用法
提取出httpd.conf文件的有效配置行
编写正则表达式,分别匹配MAC地址、E-Mail邮箱地址、IP地址、主机名
方案
步骤
实现此案例需要按照如下步骤进行。
步骤一:正则表达式匹配练习
1)典型的应用场合:grep、egrep检索文本行
使用不带-E选项的grep命令时,支持基本正则匹配模式。比如“word”关键词检索、“^word”匹配以word开头的行、“wordKaTeX parse error: Expected 'EOF', got '#' at position 47: …: [root@svr5 ~]#̲ grep '^r' /etc…’ /etc/hosts
127.0.0.1 localhost.localdomain localhost
若希望在grep检索式同时组合多个条件,比如输出以“root”或者以“daemon”开头的行,这时候基本正则就不太方便了(“或者”必须转义为“|”):
[root@svr5 ~]# grep ‘root|daemon’ /etc/passwd //搜索无结果
[root@svr5 ~]#
[root@svr5 ~]# grep ‘root|daemon’ /etc/passwd //正确获得结果
root❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
而若若使用grep -E或egrep命令,可支持扩展正则匹配模式,能够自动识别 |、{ 等正则表达式中的特殊字符,用起来更加方便,比如:
[root@svr5 ~]# grep -E ‘root|daemon’ /etc/passwd
root❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
或者
[root@svr5 ~]# egrep ‘root|daemon’ /etc/passwd
root❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
使用grep -E 与 使用egrep命令完全等效,推荐使用后者,特别是涉及到复杂的正则表达式的时候。
2)grep、egrep命令的-q选项
选项 -q 表示 quiet(静默)的意思,结合此选项可以只做检索而并不输出,通常在脚本内用来识别查找的目标是否存在,通过返回状态 KaTeX parse error: Expected 'EOF', got '#' at position 110: …: [root@svr5 ~]#̲ grep '^192.168… —— 匹配行首、行尾
输出默认运行级别的配置记录(以id开头的行):
[root@svr5 ~]# egrep ‘^id’ /etc/inittab
id:3:initdefault:
输出主机名配置记录(以HOSTNAME开头的行):
[root@svr5 ~]# egrep ‘^HOSTNAME’ /etc/sysconfig/network
HOSTNAME=svr5.tarena.com
统计本地用户中登录Shell为“/sbin/nologin”的用户个数:
[root@svr5 ~]# egrep -m10 ‘/sbin/nologinKaTeX parse error: Expected 'EOF', got '#' at position 468: …n [root@svr5 ~]#̲ egrep -c '/sbi…’ /etc/passwd
32 //结合 -c 选项输出匹配的行数
使用 -c 选项可输出匹配行数,这与通过管道再 wc -l的效果是相同的,但是写法更简便。比如,统计使用“/bin/bash”作为登录Shell的正常用户个数,可执行:
[root@svr5 ~]# egrep -c ‘/bin/bashKaTeX parse error: Expected 'EOF', got '#' at position 34: …者 [root@svr5 ~]#̲ egrep '/bin/ba…’ /etc/passwd | wc -l
26
4)基本元字符 . —— 匹配任意单个字符
以/etc/rc.local文件为例,确认文本内容:
[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
输出/etc/rc.local文件内至少包括一个字符(\n换行符除外)的行,即非空行:
[root@svr5 1fff8 ~]# egrep ‘.’ /etc/rc.local
#!/bin/sh

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
输出/etc/rc.local文件内的空行(用 –v 选项将条件取反):
[root@svr5 ~]# egrep -v ‘.’ /etc/rc.local

[root@svr5 ~]#
上述取空行的操作与下列操作效果相同:
[root@svr5 ~]# egrep ‘^$’ /etc/rc.local

[root@svr5 ~]#
5)基本元字符 +、?、* —— 目标出现的次数
还以/etc/rc.local文件为例:
[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
输出包括 f、ff、ff、……的行,即“f”至少出现一次:
[root@svr5 ~]# egrep ‘f+’ /etc/rc.local

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

输出包括init、initial的行,即末尾的“ial”最多出现一次(可能没有):
[root@svr5 ~]# egrep ‘init(ial)?’ /etc/rc.local

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

输出包括stu、stuf、stuff、stufff、……的行,即末尾的“f”可出现任意多次,也可以没有。重复目标只有一个字符时,可以不使用括号:
[root@svr5 ~]# egrep ‘stuf*’ /etc/rc.local

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

输出所有行,单独的“.”可匹配任意行(包括空行):
[root@svr5 ~]# egrep '.’ /etc/rc.local
#!/bin/sh

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
输出/etc/passwd文件内“r”开头且以“nologin”结尾的用户记录,即中间可以是任意字符:
[root@svr5 ~]# egrep ‘^r.*nologin$’ /etc/passwd
rpc❌32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser❌29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
6)元字符 {} —— 限定出现的次数范围
创建一个练习用的测试文件:
[root@svr5 ~]# vim brace.txt
ab def ghi abdr
dedef abab ghighi
abcab CD-ROM
TARENA IT GROUP
cdcd ababab
Hello abababab World
输出包括ababab的行,即“ab”连续出现3次:
[root@svr5 ~]# egrep ‘(ab){3}’ brace.txt
cdcd ababab
Hello abababab World
输出包括abab、ababab、abababab的行,即“ab”连续出现2~4次:
[root@svr5 ~]# egrep ‘(ab){2,4}’ brace.txt
dedef abab ghighi
cdcd ababab
Hello abababab World
输出包括ababab、abababab、……的行,即“ab”最少连续出现3次:
[root@svr5 ~]# egrep ‘(ab){3,}’ brace.txt
cdcd ababab
Hello abababab World
7)元字符 [] —— 匹配范围内的单个字符
还以前面的测试文件bracet.txt为例:
[root@svr5 ~]# cat brace.txt
ab def ghi abdr
dedef abab ghighi
abcab CD-ROM
TARENA IT GROUP
cdcd ababab
Hello abababab World
输出包括abc、abd的行,即前两个字符为“ab”,第三个字符只要是c、d中的一个就符合条件:
[root@svr5 ~]# egrep ‘ab[cd]’ brace.txt
ab def ghi abdr
abcab CD-ROM
输出包括大写字母的行,使用[A-Z]匹配连续范围:
[root@svr5 ~]# egrep ‘[A-Z]’ brace.txt
abcab CD-ROM
TARENA IT GROUP
Hello abababab World
输出包括“非空格也非小写字母”的其他字符的行,本例中大写字母和 – 符合要求:
[root@svr5 ~]# egrep ‘[^ a-zA-Z]’ brace.txt
abcab CD-ROM
8)单词边界匹配
以文件/etc/rc.local为例:
[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh

This script will be executed after all the other init scripts.

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
输出包括单词“init”的行,文件中“initialization”不合要求:
[root@svr5 ~]# egrep ‘\binit\b’ /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

或者:
[root@svr5 ~]# egrep ‘<init>’ /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

输出包括以“ll”结尾的单词的行,使用 > 匹配单词右边界:
[root@svr5 ~]# egrep ‘ll>’ /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

或者:
[root@svr5 ~]# egrep ‘ll\b’ /etc/rc.local

This script will be executed after all the other init scripts.

want to do the full Sys V style init stuff.

9)多个条件的组合
通过dmesg启动日志查看与IDE接口、CDROM光盘相关的设备信息:
[root@svr5 ~]# egrep ‘<IDE>|<CDROM>’ /var/log/dmesg
Uniform Multi-Platform E-IDE driver Revision: 7.00alpha2
PIIX4: IDE controller at PCI slot 0000:00:07.1
Probing IDE interface ide0…
Probing IDE interface ide1…
hdc: VMware Virtual IDE CDROM Drive, ATAPI CD/DVD-ROM drive
Probing IDE interface ide0…
通过dmesg启动日志查看蓝牙设备、网卡设备相关的信息:
[root@svr5 ~]# egrep -i ‘eth|network|bluetooth’ /var/log/dmesg
Initalizing network drop monitor service
Bluetooth: Core ver 2.10
Bluetooth: HCI device and connection manager initialized
Bluetooth: HCI socket layer initialized
Bluetooth: HCI USB driver ver 2.9
Intel® PRO/1000 Network Driver - version 7.3.21-k4-3-NAPI
e1000: eth0: e1000_probe: Intel® PRO/1000 Network Connection
步骤二:利用正则表达式完成检索任务
1)提取出httpd.conf文件的有效配置行
以RHEL6自带的httpd软件包为例,默认的httpd.conf配置文件内提供了大量的注释信息(# 开头或空几个格再 #),以及一些分隔的空行:
[root@svr5 ~]# head /etc/httpd/conf/httpd.conf //确认文件内容

# This is the main Apache server configuration file. It contains the
# configuration directives that give the server its instructions.
# See URL:http://httpd.apache.org/docs/2.2/ for detailed information.
# In particular, see
# URL:http://httpd.apache.org/docs/2.2/mod/directives.html

# for a discussion of each configuration directive.

# Do NOT simply read the instructions in here without understanding
[root@svr5 ~]# egrep -c “.*” /etc/httpd/conf/httpd.conf

991 //总行数
[root@svr5 ~]# egrep -c “#” /etc/httpd/conf/httpd.conf
674 //含注释的行数
[root@svr5 ~]# egrep -c "^KaTeX parse error: Expected 'EOF', got '#' at position 134: …: [root@svr5 ~]#̲ egrep -c -v '#…’ /etc/httpd/conf/httpd.conf
222
结合 > 重定向操作,提取httpd.conf的有效配置,将其保存到文件 httpd.conf.min,相关操作如下:
[root@svr5 ~]# egrep -v ‘#|^$’ /etc/httpd/conf/httpd.conf > httpd.conf.min
[root@svr5 ~]# head httpd.conf.min //确认有效配置的前10行
ServerTokens OS
ServerRoot “/etc/httpd”
PidFile run/httpd.pid
Timeout 120
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:838 errors:0 dropped:0 overruns:0 frame:0
TX packets:838 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:93855 (91.6 KiB) TX bytes:93855 (91.6 KiB)
[root@svr5 ~]# ifconfig | egrep ‘<[0-9]{1,3}(.[0-9]{1,3}){3}>’
inet addr:192.168.4.4 Bcast:192.168.4.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: