<button id="g80sy"></button>
  • <abbr id="g80sy"></abbr>
  • <button id="g80sy"></button>
    <rt id="g80sy"><delect id="g80sy"></delect></rt>
  • Linux服務器 屏蔽國外IP訪問親測可用

    程序猿 2023-02-09 15:24:50 9468瀏覽 加載中

    通過ssh遠程登錄服務器內,運行如下命令語句獲取國內IP網段,會保存為/root/china_ssr.txt

    wget -q --timeout=60 -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /root/china_ssr.txt

    將下面腳本保存為/root/allcn.sh ,并設置可執行權限( 命令: chmod +x  allcn.sh)

    mmode=$1
    
    CNIP="/root/china_ssr.txt"
    
    gen_iplist() {
            cat <<-EOF
                    $(cat ${CNIP:=/dev/null} 2>/dev/null)
    EOF
    }
    
    flush_r() {
    iptables  -F ALLCNRULE 2>/dev/null
    iptables -D INPUT -p tcp -j ALLCNRULE 2>/dev/null
    iptables  -X ALLCNRULE 2>/dev/null
    ipset -X allcn 2>/dev/null
    }
    
    mstart() {
    ipset create allcn hash:net 2>/dev/null
    ipset -! -R <<-EOF 
    $(gen_iplist | sed -e "s/^/add allcn /")
    EOF
    
    iptables -N ALLCNRULE 
    iptables -I INPUT -p tcp -j ALLCNRULE 
    iptables -A ALLCNRULE -s 127.0.0.0/8 -j RETURN
    iptables -A ALLCNRULE -s 169.254.0.0/16 -j RETURN
    iptables -A ALLCNRULE -s 224.0.0.0/4 -j RETURN
    iptables -A ALLCNRULE -s 255.255.255.255 -j RETURN
    iptables -A ALLCNRULE -m set --match-set allcn  src -j RETURN 
    iptables -A ALLCNRULE -p tcp -j DROP 
    }
    if [ "$mmode" == "stop" ] ;then
    flush_r
    exit 0
    fi
    flush_r
    sleep 1
    mstart

    執行如下命令將開始攔截

    /root/allcn.sh

    執行如下命令即可停止攔截

    /root/allcn.sh stop

    標簽:
    最后修改:2025-03-12 09:25:35

    非特殊說明,本博所有文章均為博主原創。

    主站蜘蛛池模板: 黑龙江省| 石狮市| 即墨市| 汉中市| 中方县| 红安县| 松阳县| 广汉市| 遵义县| 德钦县| 东丰县| 洛阳市| 安溪县| 大洼县| 永州市| 乳山市| 怀仁县| 醴陵市| 岫岩| 平遥县| 涪陵区| 小金县| 沂源县| 佛山市| 乌拉特中旗| 色达县| 遵义县| 闵行区| 措勤县| 东兰县| 尚义县| 从江县| 陕西省| 泽州县| 紫云| 西藏| 洛宁县| 永城市| 高阳县| 都江堰市| 扶绥县|