interger to I P address

Joe Greco jgreco at
Wed Aug 27 12:29:00 CDT 2008

> 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

BASH?  Hahaha.  Real Admins use sh.  More portable(*).


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

(*) Not a claim of actual portability of this code.  Just having fun
writing UGLY shell code.

... JG
Joe Greco - Network Services - Milwaukee, WI -
"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.

More information about the NANOG mailing list