I'm trying to construct myself a primitive network scanner, and I understand that there are multiple network classes and it has become necessary for my program to determine exactly what class of network the workstation is currently connected to.

I have trialled a few options which i consider to be needlessly complicated, such as truncating various commands such as ifconfig, however these methods are arduous and messy.

Is there a single Terminal command which can return simple output such as, thereby identifying the network class?


You can try something like this:


IPS=$(ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' | sed 's/')

FIRST=$(echo "${IPS%%.*}")

case 1 in
$(($FIRST <= 127))) echo Class A ;;
$(($FIRST <= 191))) echo Class B ;;
$(($FIRST <= 223))) echo Class C ;;
$(($FIRST <= 239))) echo Class D ;;
$(($FIRST <= 255))) echo Class E ;;
*) echo Something wrong! ;;

If You want only the IP, echo the $IPS variable.


With CIDR (Classless Inter Domain Routing) the old class concept is gone. Instead you create your networks by setting the proper subnetmask and your routing. So much for history. :-)

As my Yosemite outputs netmasks in ifconfig in hex, I wrote a shell script that analyses all ifconfig output and calculates the network from it. Maybe you could modify that to your needs. A sample output would be:

IP adress: Netmask: Network:
IP adress: Netmask: Network:
IP adress: Netmask: Network:
IP adress: Netmask: Network:

Here is the script; it omits inet6 adresses from ifconfig and relates only to IPv4:


IP="`ifconfig -a | fgrep 'inet ' | sed -e 's/^.*netmask:\(.*\)$/\1/g' -e 's/^.*inet\ \([^\ ]*\)\ .*$/\1/'`"

convert_hex_netmask_to_dec () {
    nd=$(($nh % 0x100))
    for i in 1 2 3
        ((nh = nh / 0x100))
        nd="$((nh % 0x100)).$nd"
    echo $nd
    # See more at: http://compgroups.net/comp.unix.shell/convert-hex-to-decimal/497395#sthash.ShzT161v.dpuf

for ipadress in ${IP}
    netmaskhex="`ifconfig -a | fgrep 'inet ' | grep ${ipadress} | awk '{print $4}'`"
    netmaskdec="`convert_hex_netmask_to_dec ${netmaskhex}`"
    IFS=. read -r i1 i2 i3 i4 <<< "${ipadress}"
    IFS=. read -r m1 m2 m3 m4 <<< "${netmaskdec}"
    NET="`printf \"%d.%d.%d.%d\n\" \"$((i1 & m1))\" \"$(($i2 & m2))\" \"$((i3 & m3))\" \"$((i4 & m4))\"`"

    printf "IP adress:%15s Netmask:%15s Network:%15s\n" ${ipadress} ${netmaskdec} ${NET}

If you want only the first octet from the network, just disable the final printf and replace it with

printf "%d\n" "$((i1 & m1))"

Please reconsider and reformulate your question.

You are mixing private IPv4 address spaces and classful networks (which are deprecated or at least "old-fashioned" since 1993) and you don't consider classless networks.

In the old-fashioned concept of classful networks the leading bits 0 (Class A), 10 (Class B), 110 (Class C), 1110 (Class D) or 1111 (Class E) you get by issueing a proper ifconfig or ipconfig command completely determine the class.

So all IP-addresses starting with

  •     0 - 127 belong to Class A
  • 128 - 191 belong to Class B
  • 192 - 223 belong to Class C
  • 224 - 239 belong to Class D
  • 240 - 255 belong to Class E

I can't add this as a comment because it's to long.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .