ifconfig - display non-hex netmasks?

Is there a simple way to get ifconfig or some other utility to show netmasks in non-hex? Either decimal or CIDR is fine, but having to translate from hex and back is a little silly really.

Linux (CentOS anyway) has made its default behavior to show the mask in dotted quad, maybe it's time that FreeBSD did as well.

Though, if there are compelling reasons for leaving it hex, I'm game to hear them. Reasons other than "Enh, we like it that way, get off our lawn." :)
 
I agree with shofixti that this is ridiculous. Why on earth would anyone want to see the netmask in hexadecimal? ipconfig should at least have a flag to allow us to see it in decimal.

Although edhunter has helpfully pointed out a tool supply a workaround, this is not always viable. In my own situation, for example, we have commercial FreeBSD firewalls on which it is very difficult to install third-party software. To work around this I had to write the following script, which uses bc.


Code:
#!/bin/sh
#About: Convert hexadecimal IP address to dotted decimal.                                                             
 
MyName=`basename $0`
 
if [ ! "$1" ] ; then
        echo
        echo "$MyName - converts an IP address in hexadecimal to dotted decimal" 
        echo "Usage: $MyName <hex_address>"
        echo 
        exit 1
fi
 
echo $1 | sed 's/0x// ; s/../& /g' | tr [:lower:] [:upper:] | while read B1 B2 B3 B4 ; do                             
        echo "ibase=16;$B1;$B2;$B3;$B4" | bc | tr '\n' . | sed 's/\.$//'                                              
done
 
Gym said:
Why on earth would anyone want to see the netmask in hexadecimal?
Because it's easier to convert hexadecimal to binary.
 
@SirDice
OK, point taken. But in that case, wouldn't it be better to show the number of mask bits instead?

BTW, for anyone who's interested, here's a line to add to my script above to give the number of mask bits

Code:
echo "ibase=16;obase=2;$B1$B2$B3$B4" | bc | tr -d -c 1 | wc -c | awk '{print $1 " bits"}'
 
If you have perl this is a lot easier:
Code:
echo ff | perl -e 'print hex(<>)."\n";'

Or the other way around:
Code:
echo 255 | perl -e 'printf("0x%X\n",<>);'
 
  • Thanks
Reactions: Gym
For those without perl, here's another way to do it, much simpler than my script above - no bc, awk etc:

Code:
hex=$1
h1=${hex%????}
h2=${hex#????}
printf "%d.%d.%d.%d\n" 0x${h1%??} 0x${h1#??} 0x${h2%??} 0x${h2#??}

Credit to Chris F. A. Johnson here
 
Agree to shofixti - best way to display is decimal or CIDR.
Cant find any reason for displaying in hex format, cus
1) CIDR even easier converts to binary
2) decimal also converts not bad, but advantage - you get complete netmask you can copy to windows client, give user by phone etc

Finally, there is no reason in displaying netmask in hex while IP is displaying in decimal.
 
Back
Top