awk命令详解
- 使用方法 1、 前置指令 | awk 选项 (条件)指令 2、 awk 选项 (条件)指令 被处理的文档 - 选项 -F 修改分隔符 - 指令 print 输出 - 内置变量 $0 所有列 $1 第一列 $2 $3 NR 行号 NF 列号 - awk 一定注意使用单引号
awk '{print}' test # 找到带有H6e的行,输出第一列 [root@system1 opt]# awk '/H6e/{print $1}' test [root@system1 opt]# head -2 /etc/passwd > test [root@system1 opt]# cat test root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin [root@system1 opt]# awk -F: '{print $6}' test /root /bin [root@A opt]# awk '{print "asd"$1}' test
使用两个分隔符
[root@A opt]# cat test root:x:0:0:root:/root:/bin/bash [root@A opt]# awk -F[:/] '{print $9}' test bin
条件
1、正则 2、字符与数字 3、逻辑符号 4、运算
#!/bin/bash # descri u=`awk -F: '/bash$/{print $1}' /etc/passwd` for i in $u do grep ^$i: /etc/shadow | awk -F: '{print $1" --> "$2}' done
高级
BEGIN{}{}END{}
awk 流程控制
if(){}else if(){}else{}
[root@A opt]# awk -F: '{if($1 == "root"){print $3}}' /etc/passwd [root@A opt]# awk -F: '{if($3 >= 1000){x++}else{y++}}END{print x,y}' /etc/passwd [root@A opt]# awk -F: '{if(/bash$/){x++}else if(/nologin$/){y++}else{z++}}END{print x,y,z}' /etc/passwd 9 44 3
awk 数组
数组名[下标]=值
[root@A opt]# awk 'BEGIN{a[1]="abc";a["x"]="xxx";print a["x"],a[1]}'
awk实现统计效果
[root@system1 opt]# cat test aaa bbb aaa cccc sss aaa cccc asd [root@system1 opt]# awk '{a[$1]++}END{for(i in a){print i,a[i]}}' test cccc 2 aaa 3 bbb 1 asd 1 sss 1
统计网站访问数量
[root@system1 opt]# awk '{ip[$1]++}END{for(i in ip){print i,ip[i]}}' /var/log/httpd/access_log 192.168.10.1 75 192.168.10.131 5
排序 sort
-n 以数字升序排列 -nr 以数字降序排列 -k 2 以第2列为排序目标
模拟访问 ab httpd-tools
-c 1 1个人 -n 100 100次
[root@system1 opt]# ab -c 10 -n 100 http://192.168.10.131/
查看计算机各项性能指标
CPU负载 uptime 网卡流量 内存剩余量 磁盘剩余容量 计算机账户数量 当前登录账户数量 计算机当前开启的进程数量 本机已安装的软件包数量
[root@system1 opt]# uptime | awk '{print "最近15分钟CPU的负载:"$NF}' [root@system1 opt]# ifconfig eth0 | awk '/RX p/{print "主机eth0网卡的接收的流量是:"$5}' [root@system1 opt]# ifconfig eth0 | awk '/TX p/{print "主机eth0网卡的发出的流量是:"$5}' [root@system1 opt]# free -h | awk '/Mem:/{print "内存剩余流量:"$4}' [root@system1 opt]# df -h | awk '/\/$/{print "磁盘剩余容量:"$4}' [root@system1 opt]# cat /etc/passwd | wc -l [root@system1 opt]# wc -l /etc/passwd [root@system1 opt]# sed -n "$=" /etc/passwd [root@system1 opt]# awk 'END{print "服务器账户数量:"NR}' /etc/passwd [root@system1 opt]# u=`who | wc -l` [root@system1 opt]# echo "当前登录的账户数量:$u个" [root@system1 opt]# u=`ps aux | wc -l` [root@system1 opt]# echo "当前计算机开启的进程数量:$u个" [root@system1 opt]# u=`rpm -qa | wc -l` [root@system1 opt]# echo "计算机安装的软件包数量:$u个" clear
#!/bin/bash # 查看计算机各项性能指标 while : do uptime | awk '{print "最近15分钟内CPU的负载:"$NF}' ifconfig eth0 | awk '/RX p/{print "本机累计接收的流量"$5}' ifconfig eth0 | awk '/TX p/{print "本机累计发出的流量"$5}' free -h | awk '/Mem:/{print "内存剩余容量:"$4}' df -h | awk '/\/$/{print "硬盘剩余容量:"$4}' awk 'END{print "计算机账户数量:"NR}' /etc/passwd who | awk 'END{print "当前登录账户数量:"NR}' ps aux | awk 'END{print "计算机当前开启的进程数量:"NR}' rpm -qa | awk 'END{print "本机已安装的软件包数量:"NR}' sleep 3 clear done
编写安全检测脚本
[root@A ~]# awk '/Failed password for invalid user/{a[$13]++}END{for(i in a){print "账户名错误:"i,"尝试次数:"a[i]}}' /var/log/secure 账户名错误:192.168.4.1 尝试次数:1 [root@A ~]# awk '/Failed password for [^(invalid)]/{a[$11]++}END{for(i in a){print "密码错误:"i,"尝试次 数:"a[i]}}' /var/log/secure 密码错误:192.168.4.1 尝试次 数:2
密码错误超过三次
awk '/Failed password for [^(invalid)]/{a[$11]++}END{for(i in a){print i,a[i]}}' /var/log/secure | awk '$2 > 3'
账号名错误超过三次
awk '/Failed password for invalid user/{a[$13]++}END{for(i in a){print i,a[i]}}' /var/log/secure | awk '$2 > 3'
点赞(1)
评论留言