欢迎来到我的博客,这里是Linux板块,配合之前RHCSA进阶,希望我的博客可以帮助到您,感谢您的阅读,有什么问题还希望一起探讨交流,感谢各位的支持,感谢!
0、RHCSA——使用Linux搭建一个简单的论坛(这里即可跳转Linux基础内容)
https://blog.csdn.net/weixin_63172698/article/details/130977137?spm=1001.2014.3001.5501
1、RHCE——一、安装部署及例行性工作
https://blog.csdn.net/weixin_63172698/article/details/132378717?spm=1001.2014.3001.5501
2、RHCE——二、时间服务器
https://blog.csdn.net/weixin_63172698/article/details/132388581?spm=1001.2014.3001.5501
3、RHCE——三、远程连接服务器
https://blog.csdn.net/weixin_63172698/article/details/132391288?spm=1001.2014.3001.5501
4、RHCE——四、Web服务器(理论篇)
https://blog.csdn.net/weixin_63172698/article/details/132426298?spm=1001.2014.3001.5501
5、RHCE——五、Web服务器及内网穿透(实验篇)
https://blog.csdn.net/weixin_63172698/article/details/132425515?spm=1001.2014.3001.5501
6、RHCE——六、基于https协议的静态网站
https://blog.csdn.net/weixin_63172698/article/details/132445286?spm=1001.2014.3001.5501
7、RHCE——七、搭建云存储器
https://blog.csdn.net/weixin_63172698/article/details/132453897?spm=1001.2014.3001.5501
8、RHCE——八、DNS域名解析服务器
https://blog.csdn.net/weixin_63172698/article/details/132522246?spm=1001.2014.3001.5501
9、RHCE——九、SELinux
https://blog.csdn.net/weixin_63172698/article/details/132526882?spm=1001.2014.3001.5501
10、RHCE——十、防火墙、iptables、firewalld
https://blog.csdn.net/weixin_63172698/article/details/132527048?spm=1001.2014.3001.5501
11、RHCE——十一、NFS服务器
https://blog.csdn.net/weixin_63172698/article/details/132545080?spm=1001.2014.3001.5501
12、RHCE——十二、Mysql服务
https://blog.csdn.net/weixin_63172698/article/details/132559298?spm=1001.2014.3001.5501
13、RHCE——十三、Shell自动化运维编程基础
https://blog.csdn.net/weixin_63172698/article/details/132609269?spm=1001.2014.3001.5501
14、RHCE——十四、变量和引用
https://blog.csdn.net/weixin_63172698/article/details/132625329?spm=1001.2014.3001.5501
15、RHCE——十五、shell条件测试
https://blog.csdn.net/weixin_63172698/article/details/132625540?spm=1001.2014.3001.5501
16、RHCE——十六、流程控制之条件判断、循环
https://blog.csdn.net/weixin_63172698/article/details/132626491?spm=1001.2014.3001.5501
17、RHCE——十七、文本搜索工具-grep、正则表达式
https://blog.csdn.net/weixin_63172698/article/details/132636679?spm=1001.2014.3001.5501
18、RHCE——十八、shell编程之sed
https://blog.csdn.net/weixin_63172698/article/details/132675880?spm=1001.2014.3001.5501
19、RHCE——十九、shell编程之awk
https://blog.csdn.net/weixin_63172698/article/details/132712522?spm=1001.2014.3001.5501
20、RHCE——二十、Ansible及安装与配置
https://blog.csdn.net/weixin_63172698/article/details/132743283?spm=1001.2014.3001.5501
21、RHCE——二十一、Ansible模块
https://blog.csdn.net/weixin_63172698/article/details/132743390?spm=1001.2014.3001.5501
grep -参数 条件 文件名
-i 忽略大小写
-c 统计匹配的行数
-v 取反,不显示匹配的行
-w 匹配单词
-E 等价于egrep,即启用扩展正则表达式
-n 显示行号
-rl 将指定目录内的文件打印
-A数字 匹配行及以下n行
-B数字 匹配行及以上n行
-C数字 匹配行及上下n行
-q 静默模式,没有任何内容输出,使用$?来判断是否执行成功
-o 只显示匹配的内容

[root@server ~]# grep -n "root" /etc/passwd
[root@server ~]# grep -n "sshd" /etc/passwd
[root@server ~]# grep -n "/sbin/nologin" /etc/passwd
[root@server ~]# grep -c "/bin/bash" /etc/passwd
[root@server ~]# grep "/bin/bash" /etc/passwd | wc -l
[root@server ~]# grep -nv "/sbin/nologin" /etc/passwd
[root@server ~]# grep -ni "d" /etc/passwd
[root@server ~]# grep -nA2 "ftp" /etc/passwd
[root@server ~]# grep -nB3 "ftp" /etc/passwd
[root@server ~]# grep -nC3 "ftp" /etc/passwd


在进行程序设计的过程中,用户会不可避免地遇到处理某些文本的情况。有的时候,用户还需要查找符合某些比较复杂规则的字符串。对于这些情况,如果单纯依靠程序设计语言本身,则往往会使得用户通过复杂的代码来实现。但是,如果使用正则表达式,则会以非常简短的代码来完成
正则表达式是通过一些特殊字符的排列,用以查找、替换、删除一行或多行文字字符串,简单的说,正则表达式就是用在字符串的处理上面的一项表示式。由于正则表达式语法简练,功能强大,得到了许多程序设计语言的支持,包括Java、C++、Perl以及Shell等
| 元字符 | 说明 |
|---|---|
| ^ | 以某个字符开头 或 行首 |
| $ | 以某个字符结尾 或行尾 |
| . | 匹配任意单字符 |
| ***** | 对前一项进行0次或者多次重复匹配 |
| {m,n} | 将前一项字符重复m-n次,{m,},{,n},{m,n} |
| [] | 对方括号内的单字符进行匹配 |
| [^] | 不匹配方括号内的单字符 |
^[] | 匹配以某个字符开头的行 |
| () | 定义一个子表达式 |
| 词首【\<或\b】和词尾锚定【\>或者\b】 | 其后面的任意字符必须作为单词首部出现;其前面的任意字符必须作为单词尾部出现 |
| \ | 转义字符,让一些特殊符号失效 |
[:...:]的特殊元序列(meta sequence),他可以用于匹配特定的字符范围| 正则表达式 | 描述 | 示例 |
|---|---|---|
| [:alnum:] | 匹配任意一个字母或数字字符 | [[:alnum:]]+ |
| [:alpha:] | 匹配任意一个字母字符(包括大小写字母) | [[:alpha:]] |
| [:blank:] | 空格与制表符(横向和纵向) | [[:blank:]]* |
| [:digit:] | 匹配任意一个数字字符 | [[:digit:]]? |
| [:lower:] | 匹配小写字母 | [[:lower:]] |
| [:upper:] | 匹配大写字母 | ([[:upper:]]+)? |
| [:punct:] | 匹配标点符号 | [[:punct:]] |
| [:space:] | 匹配一个包括换行符、回车等在内的所有空白符 | [[:space:]]+ |
| [:graph:] | 匹配任何一个可以看得见的且可以打印的字符 | [[:graph:]] |
| [:xdigit:] | 任何一个十六进制数(即:0-9,a-f,A-F) | [[:xdigit:]]+ |
| [:cntrl:] | 任何一个控制字符(ASCII字符集中的前32个字符) | [[:cntrl:]] |
| [:print:] | 任何一个可以打印的字符 | [[:print:]] |
[root@server ~]# grep -n [dD] /etc/passwd
[root@server ~]# grep -n [oo] /etc/passwd


[root@server ~]# grep -n [^o] /etc/passwd
[root@server ~]# grep -n [^r]oot /etc/passwd


[root@server ~]# grep -nw [7-9] /etc/passwd
[root@server ~]# grep -n [A-Z] /etc/passwd
[root@server ~]# grep -n [A-G] /etc/passwd
[root@server ~]# grep -n [0-9A-Za-z] /etc/passwd
[root@server ~]# grep -n [[:alnum:]] /etc/sos/sos.conf




[root@server ~]# grep -n ^root /etc/passwd
[root@server ~]# grep -n ^# /etc/sos/sos.conf # 检索注释行
[root@server ~]# grep -n "^\[" /etc/sos/sos.conf
[root@server ~]# grep -n "[0-9]$" /etc/sos/sos.conf
[root@server ~]# grep -n "^$" /etc/sos/sos.conf
# 注意:
[root@server ~]# grep -n ^[root] /etc/passwd # 检索以r或o或t开头的不是root开头
[root@server ~]# grep -n ^root /etc/passwd # 整体对待
[root@server ~]# grep -n [^root] /etc/passwd # 检索不包含r或不包含o或不包含t,进行反向着色,注意:^[] 与 [^] 区别



[root@server ~]# grep -n r..t /etc/passwd
[root@server ~]# grep -n ooo* /etc/passwd
[root@server ~]# grep -n "\." /etc/passwd

[root@server ~]# grep -n "\<[0-9]\{2\}\>" /etc/passwd
# {m,n} 描述区间

| 元字符 | 说明 |
|---|---|
| ? | 将前一项字符进行0次或者1次的重复匹配 |
| + | 将前一项进行1次或者多次的重复匹配 |
| (|) | 匹配|符号左边或者右边的字符 |
[root@server ~]# grep -n "bash$" /etc/passwd
[root@server ~]# grep -n "\<[[:digit:]]\{3,4\}\>" /etc/passwd
[root@server ~]# grep -n "\<[0-9]\{3,4\}\>" /etc/passwd
[root@server ~]# egrep -n "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg
[root@server ~]# grep -nE "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg
[root@server ~]# grep -n "^[[:space:]]\+[^[:space:]]" /etc/grub2.cfg
# 注意:grep不支持扩展正则(? + (|)),要想生效,可以使用上述三种方法





# 将netstat -tan显示结果中以LISTEN开头后跟0个或多个空白字符结尾的行信息
[root@server ~]# netstat -tan | grep "LISTEN[[:space:]]*$"
# 检索fdisk -l 命令结果中,包含/dev/开头后跟上n的行信息
[root@server ~]# fdisk -l | grep "^/dev/n"
# 显示/etc/passwd中包含root、sshd、chrony的相关信息
[root@server ~]# egrep -n "(root|sshd|chrony)" /etc/passwd
[root@server ~]# grep -nE "(root|sshd|chrony)" /etc/passwd
# 检索ifconfig显示信息中结果为1-255间的整数
[root@server ~]# ifconfig | egrep -o "\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] )\>"
# 取出echo输出的绝对路径名中的文件名
[root@server ~]# echo /etc/yum.repos.d | egrep [^/]+/?$


编写脚本for1.sh,使用for循环创建20账户,账户名前缀由用户从键盘输入,账户初始密码由用广输入,例如: test1、test2、test3、…、 test10
[root@server ~]# vim for1.sh

[root@server ~]# bash for1.sh
请输入用户账户名前缀: test
请输入用户的密码: 123456
[root@server ~]# cat /etc/passwd | tail -20

编写脚本for2.sh,使用for循环,通过ping命令测试网段的主机连通性,IP前3段由用户输入,如: 输入192.168.48 ,则pin 192.168.48.125 - 192.168.48.135,将可以ping通的主机IP地址写入到 /tmp/host_up.txt文件中,不能ping通的主机IP地址写入到: /tmp/host_down.txt文件中
[root@server ~]# vim for2.sh

[root@server ~]# bash for2.sh
请输入网段:192.168.13
up ip:
192.168.13.134 is up
down ip :
192.168.13.125 is down
192.168.13.126 is down
192.168.13.127 is down
192.168.13.128 is down
192.168.13.129 is down
192.168.13.130 is down
192.168.13.131 is down
192.168.13.132 is down
192.168.13.133 is down
192.168.13.135 is down
[root@server ~]# cat /tmp/host_down.txt

使用for循环实现批量主机root密码的修改
步骤:

[root@server ~]# vim ip.txt




[root@server ~]# ssh-copy-id root@192.168.13.135
[root@server ~]# ssh-copy-id root@192.168.13.136

[root@server ~]# vim for3.sh


[root@server ~]# bash for3.sh
请输入密码:123
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
更改用户 root 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@server ~]#