Re: IP range regex question
- From: Loki Harfagr <l0k1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: 27 May 2011 13:13:57 GMT
Thu, 26 May 2011 22:49:08 +0000, Loki Harfagr did cat :
Thu, 26 May 2011 22:59:48 +0200, Tuxedo did cat :
Could the above errors refer specifically to Bash, Awk or something
else specific to FreeBSD? I guess it probably relates to Awk and maybe
I should try and install a different Awk version on the FreeBSD boxes.
in a quick word as I'm off to bed the next minute I'd bet the best bet
would be to try and use 'gawk' on your FreeBSD machines, that should
fill the gaps. (maybe it is already installed but you'll have to call it
explicitly)
Post again if no luck but hope for other posters while I'm flat ;-)
I confirm that the problem you had is that I used 2 bitwise functions
('rshift' and 'and') and they are "gawkisms" (I suppose next 2 posts would
be "that was in mawk long before" and "lies, it's a stolen tawk feature" ;-)
So, to resolve your problem you'll have to either:
- use gawk
or
- try this version where I replaced the bitwise stuff by workarounds
and successfuly tested using gawk in --traditional mode.
(note that the FNR/NR trigger was replaced by a quick workaround, this
has to be set up accordingly to your actual usage)
---------------
#!/usr/bin/awk -cf
###
### Usage example: echo 31.172.160.22 | isIPinCIDR - CIDRlistfile
###
/^[[:blank:]]*$/{next}
/^#/{next}
NF!=1{next}
### first parm is the IP to check against the next parm (path to cidr list)
NR==1{IPtotest=$1;next}
###
{
### if (shell parm) IP matches the ($1) CIDR mask print the line
print "### testing IP="IPtotest" against mask:"$1
if( isIPinMASK(IPtotest,$1) ) print "IP "IPtotest" is in mask:",$1
}
###
function _rshift(num,n)
{
while(n--)
num=int(num/2)
return num
}
function _isEven(num)
{
if(num==2*int(num/2)) return 1
else return 0
}
function _num2bin(num,_len,bin, mask)
{
for (; num != 0; num=_rshift(num, 1)) {
bin=(_isEven(num) ?"0":"1") bin
}
while ((length(bin) % _len) != 0) bin = "0" bin
return bin
}
function isIPinMASK(ip,maskip,_vIP,_mip,_vmip,netIP,netMKIP,_bm,sb_netIP,sb_netMKIP){
split(ip,_vIP,".")
_bm=split(maskip,_mip,"/")
_bm=0+(_bm==2)?_mip[2]:32
split(_mip[1],_vmip,".")
netIP = _vIP[4]+256*(_vIP[3]+256*(_vIP[2]+256*_vIP[1]))
netMKIP = _vmip[4]+256*(_vmip[3]+256*(_vmip[2]+256*_vmip[1]))
sb_netIP=substr(_num2bin(netIP,32),1,_bm)
sb_netMKIP=substr(_num2bin(netMKIP,32),1,_bm)
return((sb_netMKIP==sb_netIP)?1:0)
}
---------------
.
- Follow-Ups:
- Re: IP range regex question
- From: Tuxedo
- Re: IP range regex question
- References:
- IP range regex question
- From: Tuxedo
- Re: IP range regex question
- From: Randal L. Schwartz
- Re: IP range regex question
- From: Tuxedo
- Re: IP range regex question
- From: Loki Harfagr
- Re: IP range regex question
- From: Tuxedo
- Re: IP range regex question
- From: Loki Harfagr
- Re: IP range regex question
- From: Tuxedo
- Re: IP range regex question
- From: Tuxedo
- Re: IP range regex question
- From: Loki Harfagr
- IP range regex question
- Prev by Date: Re: What shell feature did surprise you?
- Next by Date: Re: What shell feature did surprise you?
- Previous by thread: Re: IP range regex question
- Next by thread: Re: IP range regex question
- Index(es):
Relevant Pages
|