1834786 Members
2860 Online
110070 Solutions
New Discussion

Re: netmask

 
SOLVED
Go to solution
Shivkumar
Super Advisor

netmask

Hi,

I want to see the subnet value in decimal format such 255.255.255.0 etc.

"$ifconfig lan0" command gives me output in hexadecimal format.

Can someone suggest how to convert these hexadecimal values into decimal format (such as 255.255.255.0 format ) ?

Thanks in advance.
Shiv
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: netmask

To my way of thinking the hexadecimal values are easier because they are more directly translatable to binary --- which is the real way to view subnets but, in any event, every real UNIX guy needs to know how to use bc for base-conversions and high-precision calculations:

Essentialy after setting the input base to 16, you then enter each 2-digit hex pair and immediately the decimal equivalant is output.

e.g.
bc
ibase=16
>FF
255
>FF
255
>FF
255
>00
0
q # exits bc

Man bc for details.

You can also code fancy Perl or bc scripts to translate the entire 32-bit hex value into decimal octets but I'm not smart enough to do that (or maybe I think you should do it yourself; after all, you need it, I don't).
If it ain't broke, I can fix that.
inventsekar_1
Respected Contributor

Re: netmask

HI SHiv,

var=`ifconfig $lan |grep inet|awk '{print $4}'`
oct1=0x`echo $var | cut -c 1-2`
oct2=0x`echo $var | cut -c 3-4`
oct3=0x`echo $var | cut -c 5-6`
oct4=0x`echo $var | cut -c 7-8`
printf "Subnet: %d.%d.%d.%d" $oct1 $oct2 $oct3 $oct4

this is giving what we want.
Be Tomorrow, Today.
Shivkumar
Super Advisor

Re: netmask

i wanted to convert fffffe00.

it is coming to 255.255.254.0 correct ?

after my college day first time i am required to use it.

Is there any direct command in hpux so that we can get netmask in 255.255.... etc format ?

Regards,
Shiv
inventsekar_1
Respected Contributor

Re: netmask

one small change:
set the variale "$lan"

$lan=lan0
othervise use lan0 directly:
=============================
var=`ifconfig lan0 |grep inet|awk '{print $4}'`
oct1=0x`echo $var | cut -c 1-2`
oct2=0x`echo $var | cut -c 3-4`
oct3=0x`echo $var | cut -c 5-6`
oct4=0x`echo $var | cut -c 7-8`
printf "Subnet: %d.%d.%d.%d" $oct1 $oct2 $oct3 $oct4

============================
when i ran this:

$sh ifconfig_test
Subnet: 255.255.254.0



Be Tomorrow, Today.
Shivkumar
Super Advisor

Re: netmask

Thanks for help!! I needed this info immediately.

Regards,
Shiv