一个处理IP的脚本
时间:2010-11-11 来源:expert1
\(^o^)/~,恐怕是写的最复杂的一个了。
#!/bin/awk -f # by expert1 # date :2010-10-12 ############################################################################
# #
# Warnings:don't touch it unless you know how it works!!! #
# #
# Any question please contact me at xxxxxx #
# #
############################################################################ ############ change ip/maksk to iprange ######## ########### e.g 192.168.0.0/24 ->192.168.0.0-192.168.0.255 ####
function int2binary(x,i){
while(x>0){
i=(x%2)""i;
x=int(x/2);
}
return i;
}
function ip2binary(ip,i,s){
split(a[1],b,".");
for(i=1;i<=4;i++)
s=s""padding(int2binary(b[i]),-1,8,0);
return s;
}
function padding(s,direction,len,c){
while(length(s)<len)
s=direction<0?c""s:s""c;
return s;
}
function x(b1,b2,t,s,i){
b1=padding(b1,-1,length(b2),0);
b2=padding(b2,-1,length(b1),0);
for(i=1;i<=length(b1);i++)
s=s""(substr(b1,i,1)+substr(b2,i,1)==t?1:0);
return s;
}
function binary2int(b,i,k){
k=0;
for(i=1;i<=length(b);i++)
k+=2^(length(b)-i)*substr(b,i,1);
return k;
}
function binary2ip(b,s,i){
for(i=1;i<=length(b);i+=8)
s=s""(i==1?"":".")binary2int(substr(b,i,8));
return s;
}
function iprange(ip,i,s,start,end){
split(ip,a,"/");
start=x(padding(padding("",1,a[2],1),1,32,0) ,ip2binary(a[1]),2);
end=x(padding("",1,32-a[2],1) ,start,1);
return binary2ip(start)"-"binary2ip(end);
}
function ip2n(ip) {
split(ip,xiaofu,"."); return xiaofu[1]*256^3+xiaofu[2]*256^2+xiaofu[3]*256+xiaofu[4] } function n2ip(n) {
return int(n/256^3)"."int(n%256^3/256^2)"."int(n%256^2/256)"."int(n%256) } ######### main function ########### BEGIN {
FS = "-"
}
/name/{tag=$0;if(NR==FNR) seq[j++]=$0 # if(d[s]=="") # d[c[++j]=s]++; ##### END print c[i]
} /[0-9]/ {
if (NR == FNR)
{
ip[iprange($0)] = tag
next }
else {
for(i in ip)
{
flag = 0
split(i ,A ,"-")
if (ip2n($1) > ip2n(A[1]) && ip2n($1) <= ip2n(A[2]))
{
ip[n2ip(ip2n(A[1])) "-" n2ip(ip2n($1) - 1)] = ip[i]
flag = 1
}
if (ip2n($2) >= ip2n(A[1]) && ip2n($2) < ip2n(A[2]))
{
ip[n2ip(ip2n($2) + 1) "-" n2ip(ip2n(A[2]))] = ip[i]
flag = 1
}
if(ip2n($1) <= ip2n(A[1]) && ip2n($2) >= ip2n(A[2]))
{
flag = 1
}
if (flag)
delete ip[i]
}
ip[$0] = tag
}
} END {
print "\nregion_db user {\n" for(i in ip) final[ip[i]]=final[ip[i]] "\n"i for(i=0;i<j;i++)print " region {\n"seq[i]" "final[seq[i]]"\n }\n" } 其中iprange函数感谢一个朋友提供,我自己用ipcalc也做出了,比较简单这里就不写了。
#!/bin/awk -f # by expert1 # date :2010-10-12 ############################################################################
# #
# Warnings:don't touch it unless you know how it works!!! #
# #
# Any question please contact me at xxxxxx #
# #
############################################################################ ############ change ip/maksk to iprange ######## ########### e.g 192.168.0.0/24 ->192.168.0.0-192.168.0.255 ####
function int2binary(x,i){
while(x>0){
i=(x%2)""i;
x=int(x/2);
}
return i;
}
function ip2binary(ip,i,s){
split(a[1],b,".");
for(i=1;i<=4;i++)
s=s""padding(int2binary(b[i]),-1,8,0);
return s;
}
function padding(s,direction,len,c){
while(length(s)<len)
s=direction<0?c""s:s""c;
return s;
}
function x(b1,b2,t,s,i){
b1=padding(b1,-1,length(b2),0);
b2=padding(b2,-1,length(b1),0);
for(i=1;i<=length(b1);i++)
s=s""(substr(b1,i,1)+substr(b2,i,1)==t?1:0);
return s;
}
function binary2int(b,i,k){
k=0;
for(i=1;i<=length(b);i++)
k+=2^(length(b)-i)*substr(b,i,1);
return k;
}
function binary2ip(b,s,i){
for(i=1;i<=length(b);i+=8)
s=s""(i==1?"":".")binary2int(substr(b,i,8));
return s;
}
function iprange(ip,i,s,start,end){
split(ip,a,"/");
start=x(padding(padding("",1,a[2],1),1,32,0) ,ip2binary(a[1]),2);
end=x(padding("",1,32-a[2],1) ,start,1);
return binary2ip(start)"-"binary2ip(end);
}
function ip2n(ip) {
split(ip,xiaofu,"."); return xiaofu[1]*256^3+xiaofu[2]*256^2+xiaofu[3]*256+xiaofu[4] } function n2ip(n) {
return int(n/256^3)"."int(n%256^3/256^2)"."int(n%256^2/256)"."int(n%256) } ######### main function ########### BEGIN {
FS = "-"
}
/name/{tag=$0;if(NR==FNR) seq[j++]=$0 # if(d[s]=="") # d[c[++j]=s]++; ##### END print c[i]
} /[0-9]/ {
if (NR == FNR)
{
ip[iprange($0)] = tag
next }
else {
for(i in ip)
{
flag = 0
split(i ,A ,"-")
if (ip2n($1) > ip2n(A[1]) && ip2n($1) <= ip2n(A[2]))
{
ip[n2ip(ip2n(A[1])) "-" n2ip(ip2n($1) - 1)] = ip[i]
flag = 1
}
if (ip2n($2) >= ip2n(A[1]) && ip2n($2) < ip2n(A[2]))
{
ip[n2ip(ip2n($2) + 1) "-" n2ip(ip2n(A[2]))] = ip[i]
flag = 1
}
if(ip2n($1) <= ip2n(A[1]) && ip2n($2) >= ip2n(A[2]))
{
flag = 1
}
if (flag)
delete ip[i]
}
ip[$0] = tag
}
} END {
print "\nregion_db user {\n" for(i in ip) final[ip[i]]=final[ip[i]] "\n"i for(i=0;i<j;i++)print " region {\n"seq[i]" "final[seq[i]]"\n }\n" } 其中iprange函数感谢一个朋友提供,我自己用ipcalc也做出了,比较简单这里就不写了。
相关阅读 更多 +