awk的用法4例

1斤蔓、編寫腳本selinux.sh瑞佩,實現(xiàn)開啟或禁用SELinux功能

-------------------------------------------------------------------------------
[root@localhost data]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
#思想:實現(xiàn)禁用selinux懂版,只需要修改配置文件兜叨,SELINUX=disabled潭千,重啟即可衅码。
------------------------------------------------------------------------------
#首先創(chuàng)建遠程主機列表
vim hostlist.txt
192.168.44.10
192.168.44.129

#創(chuàng)建代碼,expect實現(xiàn)登陸主機脊岳,修改配置文件退出
#!/bin/bash
#
while read ip;do
user=root
password=123456
#事先要確認所有主機密碼一致
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect "]#" { send "sed -i 's/SELINUX=enforcing/SELIN
UX=disabled/' /etc/selinux/config\n" }
expect eof
EOF
done < hostlist.txt

#執(zhí)行腳本測試
[root@localhost scripts]# bash selinux.sh  
spawn ssh root@192.168.44.129
root@192.168.44.129's password: 
Last login: Mon Mar 30 02:03:19 2020 from 192.168.44.10
[root@centos6 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@centos6 ~]# spawn ssh root@192.168.44.10
The authenticity of host '192.168.44.10 (192.168.44.10)' can't be established.
ECDSA key fingerprint is SHA256:09hv5Rkix/WMuMfJA17altW1BT11IvVcKFiAifZGUFM.
ECDSA key fingerprint is MD5:c9:d4:55:65:5a:f0:49:53:4c:0f:a0:5d:3e:2c:32:d7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.44.10' (ECDSA) to the list of known hosts.
root@192.168.44.10's password: 
Last login: Wed Apr 22 08:37:14 2020 from 192.168.44.1
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost ~]# spawn ssh root@
ssh: Could not resolve hostname : Name or service not known
expect: spawn id exp6 not open
    while executing
"expect "]#" { send "sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config\n" }"

#查看另一主機配置文件是否修改
[root@centos6 ~]# cat /etc/selinux/config 
#成功修改逝段,即已經實現(xiàn)

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

2、統(tǒng)計/etc/fstab文件中每個文件系統(tǒng)類型出現(xiàn)的次數(shù)

[root@localhost scripts]# cat /etc/fstab -n
     1
     2  #
     3  # /etc/fstab
     4  # Created by anaconda on Tue Mar  3 08:08:26 2020
     5  #
     6  # Accessible filesystems, by reference, are maintained under '/dev/disk'
     7  # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8  #
     9  /dev/mapper/centos-root /                       xfs     defaults        0 0
    10  UUID=ca08ca12-f105-4b64-903c-8635d7700c83 /boot                   xfs     defaults        0 0
    11  /dev/mapper/centos-data /data                   xfs     defaults        0 0
    12  /dev/mapper/centos-swap swap                    swap    defaults        0 0
#方法1 第8行以后取出來割捅,取第三個域奶躯,并統(tǒng)計    
[root@localhost scripts]# awk 'NR>8{print $3}' /etc/fstab |uniq -c                           
      3 xfs
      1 swap
#方法2 匹配/dev/開頭和UUID開頭的行,取第3列 
[root@localhost scripts]# awk '/^\/dev|^UUID/{print $3}' /etc/fstab |uniq -c
      3 xfs
      1 swap
#方法3 匹配不是以#開頭的行亿驾,取第3列    
[root@localhost ~]# cat /etc/fstab |awk '/^[^#]/{print $3}'|uniq -c
      3 xfs
      1 swap
#方法4 awk關聯(lián)數(shù)組的用法
[root@localhost ~]# awk '/^[^#]/{count[$3]++}END{for (i in count) print count[i],i}' /etc/fstab     
1 swap
3 xfs

3嘹黔、提取出字符串Yd$C@M05MB%9&Bdh7dq+YVixp3vpw中的所有數(shù)字

#方法1
[root@localhost ~]# echo "Yd$C@M05MB%9&Bdh7dq+YVixp3vpw"|tr -dc [:digit:]
#除了數(shù)字其它字符刪除
05973

#方法2 awk gsub函數(shù)
[root@localhost ~]# echo 'Yd$C@M05MB%9&Bdh7dq+YVixp3vpw'|awk 'gsub(/[^0-9]/,"",$0)'
#全局替換非數(shù)字字符為空
05973

4、解決DOS攻擊生產案例:根據(jù)web日志或者或者網絡連接數(shù)莫瞬,監(jiān)控當某個IP 并發(fā)連接數(shù)或者短時內PV達到100儡蔓,即調用防火墻命令封掉對應的IP,監(jiān)控頻率每隔5分鐘疼邀。防火墻命令為:iptables -A INPUT -s IP -j REJECT

#1. 查看web日志有ip 統(tǒng)計ip和ip連接的次數(shù)
[root@localhost scripts]# less access_log 
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "POST /webnoauth/model.cgi HTTP/1.1" 404 293 "http://172.18.0.1/webnoauth/model.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.110 - - [20/May/2018:08:15:46 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.110 - - [20/May/2018:08:15:46 +0800] "POST /webnoauth/model.cgi HTTP/1.1" 404 293 "http://172.18.0.1/webnoauth/model.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.110 - - [20/May/2018:08:15:46 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.110 - - [20/May/2018:08:15:46 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.110 - - [20/May/2018:08:15:46 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
#2. 使用awk進行統(tǒng)計并存入iplist.txt文本中
[root@localhost scripts]# awk '{ip[$1]++}END{for(i in ip){print i,ip[i]}}' access_log >iplist.txt
[root@localhost scripts]# awk '{ip[$1]++}END{for(i in ip){print i,ip[i]}}' access_log >iplist.txt
[root@localhost scripts]# less iplist.txt 
172.20.0.200 1482
172.20.21.121 2
172.20.30.91 29
172.16.102.29 864
172.20.0.76 1565
172.20.9.9 15
172.20.1.125 463
172.20.61.11 2
172.20.73.73 198
172.20.107.222 3
172.20.0.222 2834
172.20.111.240 4
172.16.102.48 166
172.20.110.245 23
172.20.22.141 9
172.20.109.144 21
172.20.111.243 795
172.20.0.227 2267
172.20.99.66 30
172.20.116.174 641
172.20.109.148 25
172.20.111.248 23
172.18.118.91 92
172.20.116.176 3
172.20.100.201 45
172.16.101.150 90
172.16.101.138 10
172.20.116.178 3
172.20.116.191 800
172.16.101.139 48
172.20.116.179 2262
172.20.116.192 3
172.18.118.95 5
172.16.101.153 34
172.20.65.65 2259
172.18.118.96 66
172.20.116.194 468
172.20.101.140 14
172.20.19.88 30
#3. ip次數(shù)判斷是否大于100次喂江,大于100次就設置拒絕
[root@localhost scripts]# vim iptest.sh 
  
#!/bin/bash
#
while read ip number;do
if [ $number -gt 100 ];then
        iptables -A INPUT -s $ip -j REJECT
         echo from $ip rejected|tee -a /tmp/reject.txt
fi
done <iplist.txt
[root@localhost scripts]# bash iptest.sh 
from 172.20.0.200 rejected
from 172.16.102.29 rejected
。旁振。获询。。拐袜。
[root@localhost scripts]# cat /tmp/reject.txt 
from 172.20.0.200 rejected
from 172.16.102.29 rejected
吉嚣。。蹬铺。尝哆。。
[root@localhost scripts]# iptables -nL|grep 172
#驗證甜攀,規(guī)則是否生效
REJECT     all  --  172.20.118.110       0.0.0.0/0            reject-with icmp-port-unreachable
秋泄。。赴邻。印衔。啡捶。

#合并腳本
#!/bin/bash
#
#腳本中引入文件注意最好絕對路徑
awk '{ip[$1]++}END{num=0;for(i in ip){print i,ip[i]}}' /data/scripts/access_log >/tmp/iplist.txt
#生產iplist.txt后延遲1s再分析
sleep 1s
while read ip number;do
if [ $number -gt 100 ];then
        iptables -A INPUT -s $ip -j REJECT
         #echo from $ip rejected|tee -a /tmp/reject.txt 
         #注釋掉就不顯示姥敛,也不需要提示靜默執(zhí)行就好
fi
done </tmp/iplist.txt

#測試時將其注釋打開
[root@localhost scripts]# bash iptest1.sh 
from 172.20.0.200 rejected
from 172.16.102.29 rejected
from 172.20.0.76 rejected
from 172.20.1.125 rejected
from 172.20.73.73 rejected
from 172.20.0.222 rejected
from 172.16.102.48 rejected
from 172.20.111.243 rejected
from 172.20.0.227 rejected
from 172.20.116.174 rejected
from 172.20.116.191 rejected
from 172.20.116.179 rejected
from 172.20.65.65 rejected
from 172.20.116.194 rejected
from 172.20.116.195 rejected
from 172.20.116.215 rejected
from 172.18.118.102 rejected
from 172.20.116.230 rejected
from 172.18.118.104 rejected
from 172.18.118.120 rejected
from 172.20.101.149 rejected
from 172.18.118.123 rejected

#編寫crontab每5分鐘執(zhí)行此腳本
[root@localhost scripts]# crontab -l
*/5 * * * * /data/scripts/iptest1.sh
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市瞎暑,隨后出現(xiàn)的幾起案子彤敛,更是在濱河造成了極大的恐慌与帆,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,194評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件墨榄,死亡現(xiàn)場離奇詭異玄糟,居然都是意外死亡,警方通過查閱死者的電腦和手機袄秩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評論 2 385
  • 文/潘曉璐 我一進店門阵翎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人之剧,你說我怎么就攤上這事郭卫。” “怎么了背稼?”我有些...
    開封第一講書人閱讀 156,780評論 0 346
  • 文/不壞的土叔 我叫張陵贰军,是天一觀的道長。 經常有香客問我蟹肘,道長词疼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,388評論 1 283
  • 正文 為了忘掉前任帘腹,我火速辦了婚禮贰盗,結果婚禮上,老公的妹妹穿的比我還像新娘阳欲。我一直安慰自己童太,他們只是感情好,可當我...
    茶點故事閱讀 65,430評論 5 384
  • 文/花漫 我一把揭開白布胸完。 她就那樣靜靜地躺著书释,像睡著了一般。 火紅的嫁衣襯著肌膚如雪赊窥。 梳的紋絲不亂的頭發(fā)上爆惧,一...
    開封第一講書人閱讀 49,764評論 1 290
  • 那天,我揣著相機與錄音锨能,去河邊找鬼扯再。 笑死,一個胖子當著我的面吹牛址遇,可吹牛的內容都是我干的熄阻。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼倔约,長吁一口氣:“原來是場噩夢啊……” “哼秃殉!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,679評論 0 266
  • 序言:老撾萬榮一對情侶失蹤钾军,失蹤者是張志新(化名)和其女友劉穎鳄袍,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吏恭,經...
    沈念sama閱讀 44,122評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡拗小,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,459評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了樱哼。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哀九。...
    茶點故事閱讀 38,605評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖搅幅,靈堂內的尸體忽然破棺而出勾栗,到底是詐尸還是另有隱情,我是刑警寧澤盏筐,帶...
    沈念sama閱讀 34,270評論 4 329
  • 正文 年R本政府宣布围俘,位于F島的核電站,受9級特大地震影響琢融,放射性物質發(fā)生泄漏界牡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,867評論 3 312
  • 文/蒙蒙 一漾抬、第九天 我趴在偏房一處隱蔽的房頂上張望宿亡。 院中可真熱鬧,春花似錦纳令、人聲如沸挽荠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,734評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽圈匆。三九已至,卻和暖如春捏雌,著一層夾襖步出監(jiān)牢的瞬間跃赚,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,961評論 1 265
  • 我被黑心中介騙來泰國打工性湿, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留纬傲,地道東北人。 一個月前我還...
    沈念sama閱讀 46,297評論 2 360
  • 正文 我出身青樓肤频,卻偏偏與公主長得像叹括,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子宵荒,可洞房花燭夜當晚...
    茶點故事閱讀 43,472評論 2 348

推薦閱讀更多精彩內容

  • 1汁雷、編寫腳本selinux.sh净嘀,實現(xiàn)開啟或禁用SELinux功能 1.1 提示輸入?yún)?shù)方式 [root@cent...
    一心1977閱讀 246評論 0 0
  • 一 ,編寫腳本selinux.sh摔竿,實現(xiàn)開啟或禁用SELINUX功能 測試 [root@centos7 ~]# b...
    L星Y閱讀 157評論 0 0
  • feisky云計算、虛擬化與Linux技術筆記posts - 1014, comments - 298, trac...
    不排版閱讀 3,827評論 0 5
  • 1少孝、編寫腳本selinux.sh继低,實現(xiàn)開啟或禁用SELinux功能 echo "Change Selinux st...
    小羅很忙閱讀 154評論 0 0
  • https://blog.csdn.net/u013332124/article/details/94182021...
    瘋狂的哈丘閱讀 1,745評論 1 2