#!/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) |