批处理将IP地址转换为CIDR的Bash脚本



好的,问题来了。

我有一个明文列表,列出了我在服务器上屏蔽的IP地址,每天都变得越来越难处理(仅今天就增加了3000多个条目)。

它已经进行了重复排序,所以这不是问题。我想做的是编写一个脚本来遍历它,并更好地整合条目以进行大规模阻塞。

例如:

2.132.35.104
2.132.79.240
2.132.99.87
2.132.236.34
2.132.245.30

把它变成这样:

2.132.0.0/16

关于如何在bash脚本中编写代码,有什么建议吗?

更新:我已经制定了部分方法来做我需要的事情。将其转换为/24很容易,如下所示:

cat /usr/local/blocks/blocks.txt | while read line; do
oc1=`echo "$line" | cut -d '.' -f 1`
oc2=`echo "$line" | cut -d '.' -f 2`
oc3=`echo "$line" | cut -d '.' -f 3`
oc4=`echo "$line" | cut -d '.' -f 4`
echo "$oc1.$oc2.$oc3.0/24" >> twentyfour.srt
done
sort -u twentyfour.srt > twentyfour.txt
rm -f twentyfour.srt
ori=`cat /usr/local/blocks/blocks.txt | wc -l`
new=`cat twentyfour.txt | wc -l`
echo "$ori"
echo "$new"

这使它从4452个条目减少到4148个条目。

而不是:

109.86.9.93
109.86.26.77
109.86.55.225
109.86.70.224
109.86.87.199
109.86.89.202
109.86.95.248
109.86.100.19
109.86.110.43
109.86.145.216
109.86.152.86
109.86.155.238
109.86.156.54
109.86.187.91
109.86.228.86
109.86.234.51
109.86.239.61

我现在有:

109.86.100.0/24
109.86.110.0/24
109.86.145.0/24
109.86.152.0/24
109.86.155.0/24
109.86.156.0/24
109.86.187.0/24
109.86.228.0/24
109.86.234.0/24
109.86.239.0/24
109.86.26.0/24
109.86.55.0/24
109.86.70.0/24
109.86.87.0/24
109.86.89.0/24
109.86.9.0/24
109.86.95.0/24

一切都很好。但是,109.86../em>区域中有17个条目。在前2个八位字节在/24上匹配超过5个条目的情况下,我想将其减少到/16。

这就是我被卡住的地方。

更新2:

史蒂夫:这是今天的封锁名单。到目前为止的结果是这样的。显然,它并没有从十六岁的二十四岁中删除几乎重复的条目。

我希望我能告诉你这是一个简单的过滤器。但是,2.0.0.0/8版本的所有网络都已注册到RIPE NCC。被屏蔽的IP地址的范围太多了,只需缩小你想要的访问者的范围与不想要的访问者相比更容易。

你还可以使用各种工具来自动阻止攻击。

映射以识别哪个是哪个。https://www.iana.org/numbers这是我刚为你做的一个剧本。然后,您可以为每个主注册中心创建主要阻止列表。Afrinc、Lacnic、Apnic、Ripe和Arin。create_tables_by_registry.sh

只需运行此脚本。。。然后运行以下registry.sh文件。(E.g;熟.sh)

#!/bin/bash
# Author: Steve Kline
# Date: 03-04-2014
# Designed and tested to run on properly on CentOS 6.5
#Grab Updated IANA Address Space Assignments only if Newer Version
wget -N https://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.txt
assigned=ipv4-address-space.txt
arrayregistry=( afrinic apnic arin lacnic ripe )
for registry in "${arrayregistry[@]}"
do
#Clean up the ipv4-address-space.txt file and keep useable IPs
grep "$registry" $assigned | sed 's//8/.0.0.0/8/g'| colrm 15 > $registry-tmp1.txt
ip=($(cat $registry-tmp1.txt))
echo "#!/bin/bash" > $registry.sh
for ip in "${ip[@]}"
do
echo $ip | sed -e 's/"   "//g'  > $registry-tmp2.txt
#INSERT OR MODIFY YOUR COMPATIBLE FIREWALL RULES HERE
#This section creates the country to block.
echo "iptables -A INPUT -s $ip -j DROP" >> $registry.sh
chmod +x $registry.sh
done
rm $registry-tmp1.txt -f
rm $registry-tmp2.txt -f
done

好的!我回来了,这里有点疯狂,那里有点疯狂。。。我想我帮你弄清楚了。我相信你可以拼凑出一个修改方案来更好地满足你的需求。

#MODIFY FOR YOUR LIST OF IP ADDRESSES
BADIPS=block.ip
twentyfour=./twentyfour.ips #temp file for all IPs converted to twentyfour net ids
sixteen=./sixteen.ips   #temp file for sixteen bit
twentyfourlst1=./twentyfour1.txt    #temp file for 24 bit IDs
twentyfourlst2=./twentyfour2.txt    #temp file for 24 bit IDs filtered by 16 bit IDs that match
sixteenlst=./sixteen.txt    #temp file for parsed sixteenbit
#MODIFY FOR YOUR OUTPUT OF CIDR ADDRESSES
finalfile=./blockips.list   #Final file post-merge
cat $BADIPS | while read line; do
oc1=`echo "$line" | cut -d '.' -f 1`
oc2=`echo "$line" | cut -d '.' -f 2`
oc3=`echo "$line" | cut -d '.' -f 3`
oc4=`echo "$line" | cut -d '.' -f 4`
echo "$oc1.$oc2.$oc3.0/24" >> $twentyfour
echo "$oc1.$oc2.0.0/16" >> $sixteen
done
awk '{i=1;while(i <= NF){a[$(i++)]++}}END{for(i in a){if(a[i]>4){print i,a[i]}}}' $sixteen | sed 's/ [0-9]| [0-9][0-9]| [0-9][0-9][0-9]//g' > $sixteenlst
sort -u $twentyfour > twentyfour.txt
# THIS FINDS NEAR DUPLICATES MATCHING FIRST TWO OCTETS
cat $sixteenlst | while read line; do
oc1=`echo "$line" | cut -d '.' -f 1`
oc2=`echo "$line" | cut -d '.' -f 2`
oc3=`echo "$line" | cut -d '.' -f 3`
oc4=`echo "$line" | cut -d '.' -f 4`
grep "b$oc1.$oc2b" twentyfour.txt >> duplicates.txt    
done
#THIS REMOVES THE NEAR DUPLICATES FROM THE TWENTYFOUR FILE
fgrep -vw -f duplicates.txt twentyfour.txt > twentyfourfinal.txt
#THIS MERGES BOTH RESULTS
cat twentyfourfinal.txt $sixteenlst > $finalfile
sort -u $finalfile
ori=`cat $BADIPS | wc -l`
new=`cat $finalfile | wc -l`
echo "$ori"
echo "$new"
#LAST MIN CLEANUP
rm -f $twentyfour $twentyfourlst $sixteen $sixteenlst duplicates.txt twentyfourfinal.txt

返回修复:我注意到一个问题。。。最初没有成功。`grep"$oc1.$oc1"twentyfur.txt>重复.txt

例如:旧脚本对此测试IP范围的结果不好。。。上面的更新版本。。。完全按照它的意图去做。与八位字节完全匹配。。而不是类似的。

192.168.1.1
192.168.2.50
192.168.5.23
192.168.14.10
192.168.10.5
192.168.24.25
192.165.20.10
10.192.168.30
5.76.10.20
5.76.20.30
5.76.250.10
5.76.34.10
5.76.50.30
95.76.30.1    - Old script matched this to 5.76
20.20.5.5
20.20.10.10
20.20.16.50
20.20.205.20
20.20.60.20
205.20.16.20 - not a  problem
20.205.150.150 - Old script matched this to 20.20
220.20.16.0 - Also failed without adding -w parameter to the last grep to only match exact strings.

最新更新