优化IP段合并程序,更新IP段合并结果 (#5)

* Update ipdb.txt

* Create main_2.py

优化IP段合并程序

* Update ipdb.txt

去除非中国所属IP地址

* Update ipdb.txt

新增镇江联通1IP段
This commit is contained in:
LeterTW 2024-08-03 09:39:57 +08:00 committed by GitHub
parent 98bda2e584
commit 28704de846
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 40 deletions

View File

@ -1,46 +1,9 @@
# IP段清单
104.247.192.0/24
107.149.204.0/24
107.165.206.0/24
111.121.27.0/24
113.231.202.0/24
116.179.152.0/24
118.81.184.0/23
118.81.185.0/24
122.195.22.0/24
124.132.156.0/24
124.163.207.0/24
124.163.208.0/24
124.163.220.0/24
153.101.51.0/24
153.101.64.0/24
153.101.65.0/24
173.245.76.0/24
175.42.154.0/24
175.42.155.0/24
175.44.72.0/24
175.44.73.0/24
183.185.14.0/24
183.224.221.0/24
211.90.146.0/24
211.90.147.0/24
211.93.170.0/24
220.248.203.0/24
221.204.0.0/15
221.205.0.0/15
221.205.168.0/23
221.205.169.0/24
221.6.171.0/24
221.7.251.0/24
221.90.0.0/15
222.189.163.0/24
27.221.70.0/24
36.5.81.0/24
36.35.38.0/24
36.151.55.0/24
36.249.150.0/24
36.35.38.0/24
36.5.81.0/24
39.74.239.0/24
47.76.99.0/24
58.220.40.0/24
60.190.128.0/24
60.220.182.0/24
@ -51,4 +14,24 @@
61.160.233.0/24
61.179.15.0/24
61.241.177.0/24
84.247.148.0/24
111.121.27.0/24
113.231.202.0/24
116.179.152.0/24
118.81.184.0/23
122.195.22.0/24
124.132.156.0/24
124.163.207.0/23
124.163.220.0/24
153.101.51.0/24
153.101.64.0/23
153.101.141.0/24
175.42.154.0/23
175.44.72.0/23
183.185.14.0/24
183.224.221.0/24
211.90.146.0/23
211.93.170.0/24
220.248.203.0/24
221.6.171.0/24
221.7.251.0/24
222.189.163.0/24

49
main_2.py Normal file
View File

@ -0,0 +1,49 @@
import ipaddress
import sys
def cidr_to_range(cidr):
network = ipaddress.ip_network(cidr)
return (network.network_address, network.broadcast_address)
def range_to_cidr(start, end):
return [str(cidr) for cidr in ipaddress.summarize_address_range(start, end)]
def merge_cidrs(cidrs):
ranges = sorted(cidr_to_range(cidr) for cidr in cidrs)
merged = []
for start, end in ranges:
if not merged or start > merged[-1][1] + 1:
merged.append([start, end])
else:
merged[-1][1] = max(merged[-1][1], end)
result = []
for start, end in merged:
result.extend(range_to_cidr(start, end))
return result
def main():
print("请输入IP段列表每行一个CIDR格式的IP段:")
print("输入完成后请按Ctrl+D (Unix/Linux/Mac) 或 Ctrl+Z 然后按Enter (Windows) 来结束输入")
input_cidrs = []
for line in sys.stdin:
cidr = line.strip()
try:
ipaddress.ip_network(cidr)
input_cidrs.append(cidr)
except ValueError:
print(f"无效的CIDR格式: {cidr},已忽略", file=sys.stderr)
if not input_cidrs:
print("未输入有效的IP段", file=sys.stderr)
return
merged_cidrs = merge_cidrs(input_cidrs)
print("\n合并后的IP段:")
for cidr in merged_cidrs:
print(cidr)
if __name__ == "__main__":
main()