欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

shell 安全腳本的實現(xiàn)

 更新時間:2023年01月12日 09:01:31   作者:THE FOOL295  
本文主要介紹了shell 安全腳本的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

題目:

將密碼輸入錯誤超過4次的IP地址通過firewalld防火墻阻止訪問

1.初始配置

首先使用systemctl工具啟用firewalld服務(wù):

?[root@localhost ~]# systemctl enable firewalld

如果已經(jīng)啟用了,我們現(xiàn)在可以通過執(zhí)行以下命令啟動firewalld:

[root@localhost ~]# systemctl start firewalld

且可以通過運行以下命令驗證firewalld的狀態(tài)并且以下輸出確認了firewalld啟動了并且在運行:

[root@localhost ~]# systemctl status firewalld

在這里插入圖片描述

2.分析

1、需要知道ssh遠程訪問記錄在哪個文件中/var/log/secure
2、模擬遠程訪問輸錯密碼,查看日志文件:

cat /var/log/secure

在這里插入圖片描述

3、了解firewalld添加富規(guī)則的知識:

[root@localhost ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject'
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject

4、回到192.168.210.133上進行測試:

[root@localhost ~]# ssh root@192.168.210.128
ssh: connect to host 192.168.210.128 port 22: Connection refused

5、測試后將添加的富規(guī)則刪除:

[root@localhost ~]# firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.210.133/24" service name="ssh" reject'
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules:

3.編寫腳本

[root@localhost shell]# vim deny_ip.sh

#!/bin/bash
#*************************************************************
#Author:czc
#Date:  2023-01-09
#FileName: deny_ip.sh
#*************************************************************
serc_log=/avr/log/secure
ip_list=`awk '/Failed password/ {IP[$(NF-3)]++} END{for (k in IP){if (IP[k]>=4) {print k} }}' /var/log/secure`
for ip in `echo $ip_list`
do
        denyed_ip=`firewall-cmd --list-all | awk -F'[= ]' '/rule family/ && $NF="reject" && $(NF-1)~/ssh/ {print $6}' | awk -F'["/]' '{print $2}'`
        echo $denyed_ip | grep -q $ip
        [ $? -ne 0 ] && firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="$ip" service name="ssh" reject"
done

firewall-cmd --reload

[root@localhost shell]# chmod a+rx deny_ip.sh

4.測試

由于此時192.168.210.133只輸錯密碼3次,因此執(zhí)行腳本后,并未添加富規(guī)則。

在這里插入圖片描述

在這里插入圖片描述

此時再到192.168.210.133上對本機進行訪問,累計達到4次錯誤及以上:[root@localhost ~]# ssh

root@192.168.210.128
root@192.168.210.128's password: 
Permission denied, please try again.
root@192.168.210.128's password: 
Permission denied, please try again.
root@192.168.210.128's password: 

可以看到現(xiàn)在的輸入密碼錯誤次數(shù)累計達到5次:

在這里插入圖片描述

#此時在192.168.210.128上執(zhí)行腳本
[root@localhost shell]# ./deny_ip.sh
success
success
[root@localhost shell]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	rule family="ipv4" source address="192.168.210.133" service name="ssh" reject


#再回到192.168.210.133上進行測試

[root@localhost ~]# ssh root@192.168.210.128
ssh: connect to host 192.168.210.128 port 22: Connection refused

至此,腳本的編寫和測試就完成了。更多相關(guān)shell 安全腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何利用shell開發(fā)keepalived啟動腳本

    如何利用shell開發(fā)keepalived啟動腳本

    Keepalived軟件起初是專為LVS負載均衡軟件設(shè)計的,用來管理并監(jiān)控LVS集群系統(tǒng)中各個服務(wù)節(jié)點的狀態(tài),后來又加入了可以實現(xiàn)高可用的VRRP功能。這篇文章主要介紹了使用shell開發(fā)keepalived啟動腳本,需要的朋友可以參考下
    2020-03-03
  • Linux shell 實現(xiàn)用for循環(huán)100次的方法

    Linux shell 實現(xiàn)用for循環(huán)100次的方法

    今天小編就為大家分享一篇Linux shell 實現(xiàn)用for循環(huán)100次的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • linux?shell字符串截取的詳細總結(jié)(實用!)

    linux?shell字符串截取的詳細總結(jié)(實用!)

    在開發(fā)的時候經(jīng)常會自行寫一些小的腳本,其中就用到截取字符串的操作,這篇文章主要給大家介紹了關(guān)于linux?shell字符串截取的詳細方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • Linux命令ifconfig報錯command not found的解決方法

    Linux命令ifconfig報錯command not found的解決方法

    最近在安裝Vmware CentOS,輸入ifconfig查看VM的IP地址,提示command not found,發(fā)現(xiàn)沒安裝命令包,此篇文章記錄整個問題解決方法,有和小編遇到一樣的問題的小伙伴可以參考閱讀本文
    2023-08-08
  • 淺談shell的一些循環(huán)格式

    淺談shell的一些循環(huán)格式

    這篇文章主要介紹了淺談shell的一些循環(huán)格式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Linux shell腳本全面學(xué)習(xí)入門

    Linux shell腳本全面學(xué)習(xí)入門

    這篇文章主要為大家分享下Linux shell腳本相關(guān)的資料,對于linux系統(tǒng)中,shell腳本非常實用并強大
    2013-10-10
  • 在linux shell腳本中root切換到普通用戶執(zhí)行腳本或命令的方法

    在linux shell腳本中root切換到普通用戶執(zhí)行腳本或命令的方法

    今天小編就為大家分享一篇在linux shell腳本中root切換到普通用戶執(zhí)行腳本或命令的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 高級開發(fā)運維測試必須掌握的envsubst命令使用詳解

    高級開發(fā)運維測試必須掌握的envsubst命令使用詳解

    這篇文章主要為大家介紹了高級開發(fā)運維測試必須掌握的envsubst命令使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Shell中的for和while循環(huán)詳細總結(jié)

    Shell中的for和while循環(huán)詳細總結(jié)

    這篇文章主要介紹了Shell中的for和while循環(huán)詳細總結(jié),本文講解了for循環(huán)的數(shù)字段形式、詳細列出、對文件進行循環(huán),while循環(huán)的三種使用場合等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • linux下數(shù)據(jù)壓縮的幾種方法與查看方式(示例代碼)

    linux下數(shù)據(jù)壓縮的幾種方法與查看方式(示例代碼)

    這篇文章主要介紹了linux下數(shù)據(jù)壓縮的幾種方法與查看方式,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10

最新評論