[Gllug] hex IP addresses in /proc

will will at willj.net
Thu Mar 2 10:37:15 UTC 2006


Tethys wrote:
> Julian Somers writes:
> 
>> Does anyone know the method to convert the strange hexadecimal IP  
>> addresses in /proc (I'm concerned with /proc/net/ip_mr_cache, but  
>> they are there in /proc/net/tcp as well) into good old IP addresses?
> 
> Just convert them to decimal and swap endianness:
> 
> 	hexip=0201A8C0
> 	decip=
> 	sep=
> 	for i in 6 4 2 0
> 	do
> 		[ $i -eq 6 ] || sep=.
> 		decip="$decip$sep$((16#${hexip:$i:2}))"
> 	done
> 
> 	echo $decip

Just for fun I had a go at this in ruby:

cat /proc/net/ip_mr_cache | ruby -ne '$. == 1 and next; puts 
$_[0..16].split.collect{|x| a = []; 4.downto(1) {|i| a << "%d" % x[i*2-2 
.. i*2-1].to_i(16) }; a.join(".") }.join(" ")'

I am sure it isn't perfect, improvements welcome (and no, that doesn't 
mean doing it in perl :) )

Will
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list