#!/bin/sh # Usage: ipaddr [] # Show the IP address (IPv4 and/or IPv6) for interface or all interfaces # when no given. UNAME=$(uname) IFACE=$1 awk="command awk" # Helper function for converting a hex (or decimal) netmask into CIDR format awk_mask2cdr=' function mask2cdr(mask) { if(mask ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) { split(mask,d,"."); mask = sprintf("0x%x%x%x%x",d[1],d[2],d[3],d[4]); }; sub("^0x","",mask); cidr=0; while(mask != "") { digit=substr(mask,1,1); mask=substr(mask,2); if(digit ~ /f|F/) cidr += 4; if(digit ~ /e|E/) cidr += 3; if(digit ~ /c|C/) cidr += 2; if(digit ~ /8/) cidr += 1; }; return cidr };' # Display the final results awk_print_ifdata=' iflen=0; for ( ifnam in ifdata ) { if(length(ifnam)>iflen) iflen=length(ifnam) }; for ( ifnam in ifdata ) { split(ifdata[ifnam], data, "^"); for (v in data) { if (length(data[v])>1) printf("%-" int(iflen+1) "s %s\n", ifnam, data[v]) }} ' case "$UNAME" in Darwin|FreeBSD|AIX) test -n "$IFACE" && if_args="$IFACE" test -z "$if_args" && if_args="-a" ifconfig $if_args | $awk "$awk_mask2cdr"' /^[a-z0-9:]+/ { ifnam=$1 }; # Ex: `inet x.x.x.x netmask 0xffffff00 broadcast x.x.x.x` $1 == "inet" { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr($4) }; # Ex: `inet6 xx:xx::1 prefixlen 64 autoconf secured` $1 == "inet6" && !/scopeid|inet6 ::1|temporary/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" $4 }; END { '"$awk_print_ifdata"'}' ;; Linux) test -n "$IFACE" && if_args="$IFACE" test -z "$if_args" && if_args="-a" ifconfig $if_args | $awk "$awk_mask2cdr"' /^[^ ]+/ { gsub(":", "", $1); ifnam=$1 ":" }; # ifconfig net-tools 1.x # Ex: `inet addr:x.x.x.x Bcast:x.x.x.x Mask:255.255.255.0` $1 == "inet" && /inet addr/ { ifdata[ifnam]=ifdata[ifnam] "^" substr($2,6) "/" mask2cdr(substr($0,index($0,"Mask:")+5)) }; # Ex: `inet6 addr: xx:xx::1/64 Scope:Global` $1 == "inet6" && /Scope:Global/ { ifdata[ifnam]=ifdata[ifnam] "^" $3 } # ifconfig net-tools 2.x # Ex: `inet x.x.x.x netmask 255.255.255.0 broadcast x.x.x.x` $1 == "inet" && !/inet addr/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr($4) }; # Ex: `inet6 xx:xx::1 prefixlen 64 scopeid 0x0` $1 == "inet6" && /scopeid 0x0/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" $4 } END { '"$awk_print_ifdata"'}' ;; SunOS) test -x "/usr/gnu/bin/awk" && awk=/usr/gnu/bin/awk test -n "$IFACE" && if_args="$IFACE" test -z "$if_args" && if_args="-a" ifconfig $if_args | $awk "$awk_mask2cdr"' /^[a-z0-9:]+/ { ifnam=$1 }; $1 == "inet" { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr($4) }; $1 == "inet6" && !/inet6 ::|inet6 fe80:/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 } END { '"$awk_print_ifdata"'}' ;; *) echo "Unhandled host-type: $UNAME" esac