Ostatnio aktywny 10 months ago

這個 Bash 腳本用於 建立並更新 ipset 規則,將台灣的 IP 網段加入名為 taiwan_ips 的集合,以便在 iptables 防火牆 中使用。它會從 GitHub 下載最新的 台灣 IP 清單 (tw.txt),然後解析其中的 IPv4 網段 並加入 ipset,最後可用於 限制 SSH 存取、流量過濾或強化網路安全,適用於 伺服器管理、防禦異地登入或阻擋非台灣 IP 連線。

Rewizja 2d6b5743c3a306ce4b741b1800ac784ef8221654

taiwan_ips_ipset_config.sh Surowy
1#!/bin/bash
2
3# Delete the existing taiwan_ips ipset collection if it exists
4ipset destroy taiwan_ips
5
6# Create a new taiwan_ips ipset collection
7ipset create taiwan_ips hash:net
8
9# Download the Taiwan IP range file using wget (commented out)
10wget https://github.com/Loyalsoldier/geoip/raw/release/text/tw.txt -O tw.txt
11
12# Add IPv4 ranges to the ipset collection
13while IFS= read -r line; do
14 # Check if the line contains an IPv4 address
15 if [[ "$line" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ]]; then
16 ipset add taiwan_ips "$line"
17 fi
18done < tw.txt
19
20# Delete the downloaded file (commented out)
21# rm tw.txt
22
23# View the contents of the taiwan_ips ipset collection
24# ipset list taiwan_ips