Chinaunix首页 | 论坛 | 博客
  • 博客访问: 113191
  • 博文数量: 38
  • 博客积分: 330
  • 博客等级: 一等列兵
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-08 23:30
文章分类

全部博文(38)

文章存档

2012年(36)

2011年(2)

我的朋友

分类:

2012-03-16 15:10:21

原文地址:一个处理IP的脚本 作者: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/mask 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)                 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
   }
其中iprange函数感谢一个朋友提供,我自己用ipcalc也做出了,比较简单这里就不写了。
这个有个bug就是a 1 10000 和a 247 250 ,a 1024 1088会得不到想要的结果。因为if没有包含这个情况。
阅读(492) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~