From a188235cca79408368bb22fcd9c62b6635b01751 Mon Sep 17 00:00:00 2001 From: Tony Duckles Date: Sat, 29 Dec 2012 14:40:50 -0600 Subject: [PATCH] bin/ipaddr: Helper utility for showing interface IP addr(s) --- bin/ipaddr | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 bin/ipaddr diff --git a/bin/ipaddr b/bin/ipaddr new file mode 100755 index 0000000..0384885 --- /dev/null +++ b/bin/ipaddr @@ -0,0 +1,58 @@ +#!/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 +};' + +case "$UNAME" in + Darwin|FreeBSD) + test -n "$IFACE" && if_args="$IFACE" + test -z "$if_args" && if_args="-a" + ifconfig $if_args | $awk "$awk_mask2cdr"' + /^[a-z-0-9]/ {iface=$1}; + $1 == "inet" {printf("%-10s %s/%s\n",iface,$2,mask2cdr($4))}; + $1 == "inet6" && !/scopeid|inet6 ::1|temporary/ {printf("%-10s %s/%s\n",iface,$2,$4)}' + ;; + Linux) + test -n "$IFACE" && if_args="$IFACE" + test -z "$if_args" && if_args="-a" + ifconfig $if_args | $awk "$awk_mask2cdr"' + /^[^ ]+/ {iface=$1}; + $1 == "inet" { printf("%-10s %s/%s\n", iface ":", substr($2,6), mask2cdr(substr($0,index($0,"Mask:")+5))) }; + $1 == "inet6" && /Scope:Global/ { printf("%-10s%s\n", iface ":", $3) }' + ;; + 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]+/ {iface=$1}; + $1 == "inet" { printf("%-10s %s/%s\n", iface, $2, mask2cdr($4)) }; + $1 == "inet6" && !/inet6 ::|inet6 fe80:/ { printf("%-10s %s\n", iface, $2) }' + ;; + *) + echo "Unhandled host-type: $UNAME" +esac -- 2.43.0