new.txt 为IP库
得到方法可以从下载最新QQWry.Dat文件,通过iplook工具转换成new.txt 文件,格式如下:
开始段 结束段 地域 公司
0.0.0.0 0.255.255.255 IANA CZ88.NET
1.0.0.0 1.255.255.255 IANA CZ88.NET
2.0.0.0 2.255.255.255 IANA CZ88.NET
3.0.0.0 3.255.255.255 美国 新泽西通用电气公司
ip.txt 为需要比对的IP文件,格式如下:
38.119.136.66
58.104.35.215
58.18.56.191
58.18.58.57
58.208.2.91
58.208.66.32
接下来需要完成的工作都是在LINUX环境下编写SHELL脚本来实现比对功能。
第一步,将ip地址格式转换成12位数字并去除分隔符“.”
trans_ip.sh
#!/bin/sh
dir=/root/compareIP
dir1=ip.txt
awk 'gsub(/\./," "){if($1<100&&$1>=10){print "0"$1} else if($1<10){print "00"$1} else {print $1}}' $dir/$dir1 > $dir/a
awk 'gsub(/\./," "){if($2<100&&$2>=10){print "0"$2} else if($2<10){print "00"$2} else {print $2}}' $dir/$dir1 > $dir/b
awk 'gsub(/\./," "){if($3<100&&$3>=10){print "0"$3} else if($3<10){print "00"$3} else {print $3}}' $dir/$dir1 > $dir/c
awk 'gsub(/\./," "){if($4<100&&$4>=10){print "0"$4} else if($4<10){print "00"$4} else {print $4}}' $dir/$dir1 > $dir/d
paste $dir/a $dir/b $dir/c $dir/d > $dir/j
sleep 5
awk '{print $1$2$3$4}' $dir/j >trans.txt
rm -fr $dir/a $dir/b $dir/c $dir/d $dir/j
第二步,将ip库段的开始和结束段ip转换同上格式。
trans_iplib.sh
#!/bin/sh
dir=/root/compareIP
awk 'gsub(/\./," "){if($1<100&&$1>=10){print "0"$1} else if($1<10){print "00"$1} else {print $1}}' $dir/new.txt >$dir/a
awk 'gsub(/\./," "){if($2<100&&$2>=10){print "0"$2} else if($2<10){print "00"$2} else {print $2}}' $dir/new.txt >$dir/b
awk 'gsub(/\./," "){if($3<100&&$3>=10){print "0"$3} else if($3<10){print "00"$3} else {print $3}}' $dir/new.txt >$dir/c
awk 'gsub(/\./," "){if($4<100&&$4>=10){print "0"$4" "} else if($4<10){print "00"$4" "} else {print $4" "}}' $dir/new.txt >$dir/d
awk 'gsub(/\./," "){if($5<100&&$5>=10){print "0"$5} else if($5<10){print "00"$5} else {print $5}}' $dir/new.txt >$dir/e
awk 'gsub(/\./," "){if($6<100&&$6>=10){print "0"$6} else if($6<10){print "00"$6} else {print $6}}' $dir/new.txt >$dir/f
awk 'gsub(/\./," "){if($7<100&&$7>=10){print "0"$7} else if($7<10){print "00"$7} else {print $7}}' $dir/new.txt >$dir/g
awk 'gsub(/\./," "){if($8<100&&$8>=10){print "0"$8" "} else if($8<10){print "00"$8" "} else {print $8" "}}' $dir/new.txt >$dir/h
awk 'gsub(/\./," "){print $9}' $dir/new.txt >$dir/i
paste $dir/a $dir/b $dir/c $dir/d $dir/e $dir/f $dir/g $dir/h $dir/i > $dir/j
awk '{print $1$2$3$4 " " $5$6$7$8 " " $9}' $dir/j > $dir/new1.txt
rm -fr $dir/a $dir/b $dir/c $dir/d $dir/e $dir/f $dir/g $dir/h $dir/i $dir/j
第三步,比对。
comip.sh
#!/bin/sh
for i in `cat trans.txt`
do
a=`awk '{if('$i'>=$1&&'$i'<=$2) {print $3;exit}}' new1.txt`
echo -e $i $a >> souce
done
souce文件就是我们所要的比对结果。
目前该方法速度还算可以,每1秒能比对5个IP左右。
阅读(843) | 评论(0) | 转发(0) |