nanog mailing list archives
Re: interger to I P address
From: Joe Greco <jgreco () ns sol net>
Date: Wed, 27 Aug 2008 12:29:00 -0500 (CDT)
Sorry to be continuing this thread, but I find a certain kind of elegance in bash which isn't actually there, but helps me sleep at night. bash# iptoint(){ oct1=`echo $1|awk -F\. '{print $1}'`; oct2=`echo $1|awk -F\. '{print $2}'`; oct3=`echo $1|awk -F\. '{print $3}'`; oct4=`echo $1|awk -F\. '{print $4}'`; echo $[($oct1<<24)+($oct2<<16 )+($oct3<<8)+$oct4 ];} bash# inttoip(){ echo $[$1>>24].$[($1>>16)&255].$[($1>>8)&255].$[$1&255]; } bash# inttoip 1089055123 64.233.169.147
BASH? Hahaha. Real Admins use sh. More portable(*). Simple: v=$1;for n in 4 3 2;do eval o$n=`expr $v % 256`;v=`expr $v / 256` done; echo $v.$o2.$o3.$o4 Needlessly complex (just to keep up sh's reputation for complexity): v4=$1;for n in 4 3 2;do eval o$n=`eval expr \\$v$n % 256`;eval v`expr $n - 1`=`eval expr \\$v$n / 256` done; echo $v1.$o2.$o3.$o4 $ ipscript 1089055123 64.233.169.147 $ (*) Not a claim of actual portability of this code. Just having fun writing UGLY shell code. ... JG -- Joe Greco - sol.net Network Services - Milwaukee, WI - http://www.sol.net "We call it the 'one bite at the apple' rule. Give me one chance [and] then I won't contact you again." - Direct Marketing Ass'n position on e-mail spam(CNN) With 24 million small businesses in the US alone, that's way too many apples.
Current thread:
- Re: interger to I P address, (continued)
- Re: interger to I P address Beat Vontobel (Aug 28)
- RE: interger to I P address Boyd, Benjamin R (Aug 27)
- Re: interger to I P address Mike Damm (Aug 27)
- Re: interger to I P address bfeeny (Aug 27)
- Re: interger to I P address Johnny Eriksson (Aug 27)
- Re: interger to I P address Valdis . Kletnieks (Aug 27)
- Re: interger to I P address Randy Bush (Aug 27)
- Re: interger to I P address Mark Newton (Aug 27)
- Re: interger to I P address Rob Austein (Aug 27)
- interger to I P address chartley (Aug 27)
- Re: interger to I P address Joe Greco (Aug 27)
- RE: interger to I P address Darden, Patrick S. (Aug 27)
- Re: interger to I P address Colin Alston (Aug 27)
- Re: interger to I P address Henry Yen (Aug 27)
- Re: interger to I P address Joe Greco (Aug 27)