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# iptoint 64.233.169.147 1089055123 Chris
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.
Somebody's going to bring in Emacs now. Then somebody else will claim VI can do it faster and using less memory.... Argh. ;-) --p -----Original Message----- From: Joe Greco [mailto:jgreco@ns.sol.net] Sent: Wednesday, August 27, 2008 1:29 PM To: chartley@oar.net Cc: nanog@nanog.org Subject: Re: interger to I P address
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(*).
On 2008/08/27 07:00 PM chartley@oar.net wrote:
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.
siza # ping -c 1 1089055123 | head -n 1 | awk '{{{print $3}}}' | sed 's/(\(.*\))/\1/' 64.233.169.147 Ok I'm out of bad ideas so I'll shut up now ;)
On Wed, Aug 27, 2008 at 13:00:41PM -0400, chartley@oar.net wrote:
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.
the (well, one of many, probably) REXX way: PARSE VALUE D2X(ARG(1)) WITH a 3 b 5 c 7 d . SAY X2D(a)"."X2D(b)"."X2D(c)"."X2D(d) -- Henry Yen Aegis Information Systems, Inc. Senior Systems Programmer Hicksville, New York
participants (5)
-
chartley@oar.net
-
Colin Alston
-
Darden, Patrick S.
-
Henry Yen
-
Joe Greco