Operating System - HP-UX
1825345 Members
4670 Online
109679 Solutions
New Discussion юеВ

convert netmask ffffff00 to decimal

 
SOLVED
Go to solution
sebastien_7
Occasional Advisor

convert netmask ffffff00 to decimal

Hello,

I need to get the netmask of each lan cards in my systems, so i use the following:

var=`ifconfig $lan |grep inet|awk '{print $4}'`
var equals ffffff00 which in decimal is 255.255.255.0

My problem is to make my script return the decimal value using something like:

echo "16i ${VAR} p" | dc

But I cannot use the hole 8bit through dc, I need to cut the 8 bits into 4 portions of 2 bits and pass one portion at a time.

Does anyone know a way to cut the 8bit hex mask ? or maybe there is a better way to get the netmask value in decimal of each lan cards.

any help would be appreciated,
thanks
Sebastien
5 REPLIES 5
Les Ligetfalvy
Esteemed Contributor

Re: convert netmask ffffff00 to decimal

I don't know what script languages you know, but here is some VBS using WMI.

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objAdapter in colAdapters
Wscript.Echo "Host name: " & objAdapter.DNSHostName
Wscript.Echo "DNS domain: " & objAdapter.DNSDomain
Wscript.Echo "DNS suffix search list: " & objAdapter.DNSDomainSuffixSearchOrder
Wscript.Echo "Description: " & objAdapter.Description
Wscript.Echo "Physical address: " & objAdapter.MACAddress
Wscript.Echo "DHCP enabled: " & objAdapter.DHCPEnabled
If Not IsNull(objAdapter.IPAddress) Then
For i = LBound(objAdapter.IPAddress) To UBound(objAdapter.IPAddress)
Wscript.Echo "IP address: " & objAdapter.IPAddress(i)
Next
End If
If Not IsNull(objAdapter.IPSubnet) Then
For i = LBound(objAdapter.IPSubnet) To UBound(objAdapter.IPSubnet)
Wscript.Echo "Subnet: " & objAdapter.IPSubnet(i)
Next
End If
If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = LBound(objAdapter.DefaultIPGateway) To UBound(objAdapter.DefaultIPGateway)
Wscript.Echo "Default gateway: " & objAdapter.DefaultIPGateway(i)
Next
End If
Wscript.Echo "DHCP server: " & objAdapter.DHCPServer
If Not IsNull(objAdapter.DNSServerSearchOrder) Then
For i = LBound(objAdapter.DNSServerSearchOrder) To UBound(objAdapter.DNSServerSearchOrder)
Wscript.Echo "DNS server: " & objAdapter.DNSServerSearchOrder(i)
Next
End If
Wscript.Echo "Primary WINS server: " & objAdapter.WINSPrimaryServer
Wscript.Echo "Secondary WINS server: " & objAdapter.WINSSecondaryServer
Wscript.Echo "Lease obtained: " & objAdapter.DHCPLeaseObtained
Wscript.Echo "Lease expires: " & objAdapter.DHCPLeaseExpires
Next
Gerhard Roets
Esteemed Contributor
Solution

Re: convert netmask ffffff00 to decimal

HI Sebastian this piece of code should do it

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

Now the trick is

printf "%d" 0xHEXNUMBER returns the decimal of the hexadecimal number.
printf "%x" DECIMALNUMBER returns the hexadecimalnumber of the decimal number

HTH
Regards
Gerhard
Gerhard Roets
Esteemed Contributor

Re: convert netmask ffffff00 to decimal

Erm sidenote you mean bytes ... ;)

"But I cannot use the hole 8byte through dc, I need to cut the 8 bytes into 4 portions of 2 bytes and pass one portion at a time.

Does anyone know a way to cut the 8byte hex mask ? or maybe there is a better way to get the netmask value in decimal of each lan cards."

Regards
Gerhard
sebastien_7
Occasional Advisor

Re: convert netmask ffffff00 to decimal

Sorry Les, forgot to mention this was for HP-UX.

Gerhard, "cut" is exactly what I was looking for, thanks man, and you are right about the bits and bytes ;)

take care guys,
Sebastien
sebastien_7
Occasional Advisor

Re: convert netmask ffffff00 to decimal

closing