]>
Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - bin/ipaddr
2 # Usage: ipaddr [<iface>]
3 # Show the IP address (IPv4 and/or IPv6) for interface <iface> or all interfaces
4 # when no <iface> given.
10 # Helper function for converting a hex (or decimal) netmask into CIDR format
12 function mask2cdr(mask) {
13 if(mask ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {
15 mask = sprintf("0x%x%x%x%x",d[1],d[2],d[3],d[4]);
20 digit=substr(mask,1,1);
22 if(digit ~ /f|F/) cidr += 4;
23 if(digit ~ /e|E/) cidr += 3;
24 if(digit ~ /c|C/) cidr += 2;
25 if(digit ~ /8/) cidr += 1;
29 # Display the final results
32 for ( ifnam in ifdata ) { if(length(ifnam)>iflen) iflen=length(ifnam) };
33 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]) }}
38 test -n " $IFACE " && if_args
= " $IFACE "
39 test -z " $if_args " && if_args
= "-a"
40 ifconfig
$if_args | $awk " $awk_mask2cdr " '
41 /^[a-z0-9:]+/ { ifnam= $1 };
42 $1 == "inet" { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr( $4 ) };
43 $1 == "inet6" && !/scopeid|inet6 ::1|temporary/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" $4 };
44 END { ' " $awk_print_ifdata " '}'
47 test -n " $IFACE " && if_args
= " $IFACE "
48 test -z " $if_args " && if_args
= "-a"
49 ifconfig
$if_args | $awk " $awk_mask2cdr " '
50 /^[^ ]+/ { ifnam= $1 ":" };
51 $1 == "inet" { ifdata[ifnam]=ifdata[ifnam] "^" substr( $2 ,6) "/" mask2cdr(substr( $0 ,index( $0 ,"Mask:")+5)) };
52 $1 == "inet6" && /Scope:Global/ { ifdata[ifnam]=ifdata[ifnam] "^" $3 }
53 END { ' " $awk_print_ifdata " '}'
56 test -x "/usr/gnu/bin/awk" && awk =/ usr
/ gnu
/ bin
/ awk
57 test -n " $IFACE " && if_args
= " $IFACE "
58 test -z " $if_args " && if_args
= "-a"
59 ifconfig
$if_args | $awk " $awk_mask2cdr " '
60 /^[a-z0-9:]+/ { ifnam= $1 };
61 $1 == "inet" { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr( $4 ) };
62 $1 == "inet6" && !/inet6 ::|inet6 fe80:/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 }
63 END { ' " $awk_print_ifdata " '}'
66 echo "Unhandled host-type: $UNAME "