Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092063
  • 博文数量: 186
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(186)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类:

2010-11-21 10:54:12

#!/bin/sh
# by expert1
# date :2010-11-20 
#use to search the ip is in the region.user Or NOT.
############################################################################
#                                                                          #
#             Warnings:don't touch it unless you know how it works!!!      #
#                                                                          #
#   Any question please contact me at msn:xiaofu68#live.cn #
#                                                                          #
############################################################################
echo -n "please input the ip you have got(e.g 192.168.1.1):"
read var1
echo '###########################################'
awk -v var=$var1 '
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] }
######### main function ###########
/name/{tag=$0 }
/[0-9]/ {
          ip[iprange($0)] = tag }
END{for (i in ip)
   
          { split (i,tmp,"-")
            if(ip2n(tmp[1]) <= ip2n(var) && ip2n(tmp[2]) >= ip2n(var))
              
             print var,ip[i] 
          }
    
   }' region.user
echo '###########################################'
阅读(1495) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~