qwe3223678qwe 发表于 2018-8-21 06:04:04

Linux自动化运维之Shell脚本(登堂入室)

  基本正则表达式 (使用一些特殊符号来表达)
  ^       开始
  $       结尾
  [ ]   集合中任意单个符号
  [^ ]      对集合取反
  .       任意单个符号


[*]匹配前一个字符出现任意次(0次或多次)  .*      匹配所以
  {n,m} 匹配前一个字符出现了n到m次
  {n,}匹配前一个字符出现了n次或n次以上
  {n}       匹配前一个字符出现n次

  扩展正则(优化基本,添加新的)
  {n,m}
  {n,}
  {n}
  将*拆成了+ ?


[*]  匹配 1 次或多次
  ?       匹配 0 或 1 次
  ()      整体
  |       或者
  \b      匹配单词边界
  \B      匹配非单词边界
  \<      匹配指定单词开头
  \>      匹配指定单词结尾
  # grep &quot;test&quot; test.txt
  this is a test file
  testisgood
  goodtest haha
  # grep &quot;\btest\b&quot; test.txt
  this is a test file
  # grep &quot;\btest&quot; test.txt
  this is a test file
  testisgood
  # grep &quot;test\b&quot; test.txt
  this is a test file
  goodtest haha

  基本正则:兼容性强,书写麻烦
  扩展正则:兼容性差,书写简单
  egrep可以使用扩展的正则表达式
  grep -E 表示使用扩展的正则表达式
  extended register
  扩充寄存器
  grep {1,4}
  等同于
  grep -E {1,4}
  -c 返回匹配的行数
  -o 显示一行中与指定模式匹配的部分
  grep -Ec 条件         #显示匹配行数
  grep -Eo 条件 | wc -l #显示可得到匹配的实际数目
  sed 非交互文本编译器(流处理器)
  sed [选项] '条件指令' 文件
  选项:
  -n 屏蔽sed默认输出
  -r 开启扩展正则
  -i 修改源文件
  条件:
  1.行号
  

    # sed -n '1p' /etc/passwd   #打印第一行  root:x:0:0:root:/root:/bin/bash
  # sed -n '1,3p' /etc/passwd   #打印 1,2,3 行
  root:x:0:0:root:/root:/bin/bash
  bin:x:1:1:bin:/bin:/sbin/nologin
  daemon:x:2:2:daemon:/sbin:/sbin/nologin
  

  2.正则表达式
  

# sed -nr '/^test:/p' /etc/passwd#/../之中输入正则 p 为打印  
test:x:3468:3468::/home/test:/bin/bash
  

  指令:(增 删 改 查)
  p   打印
  d   删除
  s   替换 s/旧/新/
  s # 新 # 旧 #   #可变
  注意事项:替换符号可以是任意其他符号
  a   append 追加   之后加一行
  i   insert 插入   之前加一行
  c   替换行change
  r   导入
  w   另存为
  H/h 复制
  G/g 粘贴
  () 保留,复制
  \粘贴
  例:
  

# sed 'd' /etc/passwd  
# sed '/bash$/d' /etc/passwd#显示删除后的
  

  
# sed's/2012/666/' a.txt    #默认替换每行的第一个
  
666 2011 2012
  
2018 666
  
2013
  
666 2012 2012
  
# sed's/2012/666/g' a.txt   #替换全部
  
666 2011 666
  
2018 666
  
2013
  
666 666 666
  
# sed's/2012/666/2' a.txt   #替换第二个
  
2012 2011 666
  
2018 2012
  
2013
  
2012 666 2012
  
# cat 1.txt
  
98969 9899 9869 98969
  
# sed 's9\98\9\99\96\99' 1.txt
  
98969 969 9869 98969
  

  
# vim test.txt
  
# cat test.txt
  
ni hao nb
  
welcome to beijing
  
1.把第一个字符和最后一个字符对调
  
# sed -r 's/^(.)(.*)(.)$/\3\2\1/' test.txt
  
bi hao nn
  
gelcome to beijinw
  
2.把第二个字符和倒数第二个字符对调
  
^(.)(.)(.*)(.)(.)$\1\4\3\2\5
  

  
# sed 'a666 1' test.txt
  
12
  
666 1
  
# sed 'i666 1' test.txt
  
666 1
  
12
  
# sed 'c666 1' test.txt
  
666 1
  

  
# sed 'r /etc/hosts' a.txt
  a.txt的每一行下导入/etc/hosts 的内容
  
# sed '2r /etc/hosts' a.txt
  a.txt的第二行下导入/etc/hosts 的内容
  
# sed-i 'w /b.txt' a.txt      #全文另存为
  
# sed-i '3w /c.txt' a.txt   #仅第三行另存为
  
# cat /b.txt
  
2012 2011 2012
  
2018 2012
  
2013
  
2012 2012 2012
  
# cat /c.txt
  
2013
  
# sed '2H;3G' a.txt
  
2012 2011 2012
  
2018 2012
  
2013                #G 为追加粘贴
  

  
2018 2012
  
2012 2012 2012
  

  练习:
  1)把/etc/paswd/中能登录的用户找出来
  #!/bin/bash
  sed -n '/bash$/p' /etc/passwd > tmp.txt
  for i in cat tmp.txt
  do
  echo ${i%%:*}
  done
  

#!/bin/bash  
sed -n '/bash$/s/:.*//p' /etc/passwd
  

  2)把/etc/shadow中的密码显示
  

#!/bin/bash  
A=`sed -n '/bash$/s/:.*//p' /etc/passwd` &> /dev/null
  
for i in $A
  
do
  p1=`grep "$i" /etc/shadow`
  p2=${p1#*:}
  p3=${p2%%:*}
  echo "$i:$p3"
  
done
  

  awk 数据过滤,统计(行,列)
  逐行处理器
  awk [选项] '条件{指令}' 文件
  命令 | awk [选项] '条件{指令}'
  

# free | awk '/Mem/{print $4}'    #查看内存  
# ifconfig eth0 | awk '/RX p/{print $5}' #查看网卡流量
  
# tailf /var/log/secure   #监控 远程登录 日志文件
  

  选项:
  -F指定分隔符   #默认分隔符为空格和(TAB)
  # awk -F: '{print $1}' /etc/passwd
  awk 内置变量
  $1 $2 $3..#某一列
  NF      #当前行有多少列
  NR      #当前行号
  

# awk -F: '{print NR}' /etc/passwd  
# awk -F: '{print NF}' /etc/passwd
  
# awk -F: '{print $NF}' /etc/passwd   #打印最后一列
  
# awk '{print "nh","nb","nm"}' /etc/passwd#打印常量(必须用双引号)
  
# awk -F: '{print "用户名为:",$1}'/etc/passwd
  
用户名为: root
  
用户名为: bin
  
...
  

  awk [选项] 'BEGIN{} 条件{} END{}' 文件
  原则:所有的指令必须放在{}
  BEGIN{}:指令在读取文件内容前执行1次
  条件{}: 指令在读取文件后中执行n次
  END{}: 指令在读取文件后执行1次
  

# awk 'BEGIN{print "nihao"}' a.txt  
nihao
  
# awk '{print "nihao"}' a.txt
  
nihao
  
nihao
  
nihao
  
nihao
  
# awk 'END{print "nihao"}' a.txt
  
nihao
  

  awk 条件:
  1.正则(模糊)
  

在一整行中包含root即可  
# grep 'root' /etc/passwd
  
root:x:0:0:root:/root:/bin/bash
  
operator:x:11:0:operator:/root:/sbin/nologin
  
#在一整行包含root即可(模糊查找)
  
# awk -F: '/root/{print $3}' /etc/passwd
  
0
  
11
  
# awk -F: '/^root/{print $3}' /etc/passwd
  
0
  
#在某一列中包含root即可(模糊查找) $1 为第一列
  
# awk -F: '$1~/root/{print $3}' /etc/passwd
  
0
  
#$6 为第六列
  
# awk -F: '$6~/root/{print $3}' /etc/passwd
  
0
  
11
  

  2.数字和字符比较
  == !=>   >=<   /dev/null
  cd nginx-1.8.0
  echo -e &quot;\e\e[0m&quot;
  echo -n '正在安装依赖包...'
  yum -y install gcc pcre-devel openssl-devel &>/dev/null
  echo -e &quot;\e\e[0m&quot;
  echo -n './configure 配置...'
  ./configure &>/dev/null
  echo -e &quot;\e\e[0m&quot;
  echo -n 'make编译...'
  make &>/dev/null
  echo -e &quot;\e\e[0m&quot;
  echo -n 'make install 安装...'
  make install &>/dev/null
  echo -e &quot;\e\e[0m&quot;
  

a=`ls /usr/local/nginx/ | wc -l`  
if [ $a -eq 0 ];then
  echo -n '安装失败'
  echo -e "\e\e[0m"
  
else
  echo -n '安装成功'
  echo -e "\e\e[0m"
  
fi
  

  2.nginx启动服务的脚本
  #!/bin/bash
  case $1 in
  start)
  /usr/local/nginx/sbin/nginx;;
  stop)
  /usr/local/nginx/sbin/nginx -s stop;;
  reastart)
  /usr/local/nginx/sbin/nginx -s stop
  /usr/local/nginx/sbin/nginx;;
  status)
  netstat -ntulp | grep nginx &> /dev/null
  if [ $? -eq 0 ];then
  echo -e &quot;Active: active \033[32m(running)\033[0m&quot;
  else
  echo -e &quot;Active: inactive \033[31m(dead)\033[0m&quot;
  fi;;
  *)
  echo Error;;
  esac
  4.检查/var/log/secure看看有没有人尝试破解密码
  #!/bin/bash
  while :
  do
  rm -rf ip.txt
  #这里我使用的是访问失败12次就被标记
  awk '/Failed/{print $11}' /var/log/secure | awk '{ip[$1]++} END{for(i in ip){if(ip>=12){print i}}}' >> ip.txt
  for i in cat ip.txt
  do
  #这里暂时将标记的ip放入block区中
  firewall-cmd --zone=block --add-source=$i &> /dev/null
  done
  sleep 10
  done
  3.监控脚本(awk过滤):
  #!/bin/bash
  CPU=uptime | awk'{print$10}'
  echo &quot;当前CPU负载为:${CPU}&quot;
  #这里RX1和RX2是相等的RX1是为了方便计算,RX2是为了使用户查看方便
  RX1=ifconfig eth0 | awk'/RX p/{print $5}'
  RX2=ifconfig eth0 | awk -F\('/RX p/{print $2}' | sed's/[)]//'
  echo &quot;当前网卡接受的数据流量:$RX2&quot;
  #这里TX1和TX2是相等的RX1是为了方便计算,RX2是为了使用户查看方便
  TX1=ifconfig eth0 | awk'/TX p/{print $5}'
  TX2=ifconfig eth0 | awk -F\('/TX p/{print $2}' | sed's/[)]//'
  echo &quot;当前网卡接受的数据流量:$TX2&quot;
  FREE=free | awk '/Mem/{print $4}'
  echo &quot;当前内存剩余:$FREE&quot;
  ROOTFREE=df | awk '/\/$/{print $4}'
  echo &quot;当前根份区的剩余容量:$ROOTFREE&quot;
  USERS=cat /etc/passwd | wc -l
  echo &quot;当前计算机的账户数量:$USER&quot;
  USERNOW=who | awk 'END{print NR}'
  echo &quot;当前登陆了几个人:$USERNOW&quot;
  NUM=ps aux | wc -l
  echo &quot;当前开启的进程数量: $NUM&quot;
  SNUM=rpm -qa | wc -l
  echo &quot;已经安装了多少软件:$SNUM&quot;


页: [1]
查看完整版本: Linux自动化运维之Shell脚本(登堂入室)