]> Tony Duckles's Git Repositories (git.nynim.org) - dotfiles.git/blob - bin/ipaddr
.vimrc: Change hotkey for QuickfixToggle()
[dotfiles.git] / bin / ipaddr
1 #!/bin/sh
2 # Usage: ipaddr [<iface>]
3 # Show the IP address (IPv4 and/or IPv6) for interface <iface> or all interfaces
4 # when no <iface> given.
5
6 UNAME=$(uname)
7 IFACE=$1
8 awk="command awk"
9
10 # Helper function for converting a hex (or decimal) netmask into CIDR format
11 awk_mask2cdr='
12 function mask2cdr(mask) {
13 if(mask ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {
14 split(mask,d,".");
15 mask = sprintf("0x%x%x%x%x",d[1],d[2],d[3],d[4]);
16 };
17 sub("^0x","",mask);
18 cidr=0;
19 while(mask != "") {
20 digit=substr(mask,1,1);
21 mask=substr(mask,2);
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;
26 };
27 return cidr
28 };'
29 # Display the final results
30 awk_print_ifdata='
31 iflen=0;
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]) }}
34 '
35
36 case "$UNAME" in
37 Darwin|FreeBSD|AIX)
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
43 # Ex: `inet x.x.x.x netmask 0xffffff00 broadcast x.x.x.x`
44 $1 == "inet" { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr($4) };
45 # Ex: `inet6 xx:xx::1 prefixlen 64 autoconf secured`
46 $1 == "inet6" && !/scopeid|inet6 ::1|temporary/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" $4 };
47
48 END { '"$awk_print_ifdata"'}'
49 ;;
50 Linux)
51 test -n "$IFACE" && if_args="$IFACE"
52 test -z "$if_args" && if_args="-a"
53 ifconfig $if_args | $awk "$awk_mask2cdr"'
54 /^[^ ]+/ { gsub(":", "", $1); ifnam=$1 ":" };
55
56 # ifconfig net-tools 1.x
57 # Ex: `inet addr:x.x.x.x Bcast:x.x.x.x Mask:255.255.255.0`
58 $1 == "inet" && /inet addr/ { ifdata[ifnam]=ifdata[ifnam] "^" substr($2,6) "/" mask2cdr(substr($0,index($0,"Mask:")+5)) };
59 # Ex: `inet6 addr: xx:xx::1/64 Scope:Global`
60 $1 == "inet6" && /Scope:Global/ { ifdata[ifnam]=ifdata[ifnam] "^" $3 }
61
62 # ifconfig net-tools 2.x
63 # Ex: `inet x.x.x.x netmask 255.255.255.0 broadcast x.x.x.x`
64 $1 == "inet" && !/inet addr/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr($4) };
65 # Ex: `inet6 xx:xx::1 prefixlen 64 scopeid 0x0<global>`
66 $1 == "inet6" && /scopeid 0x0<global>/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" $4 }
67
68 END { '"$awk_print_ifdata"'}'
69 ;;
70 SunOS)
71 test -x "/usr/gnu/bin/awk" && awk=/usr/gnu/bin/awk
72 test -n "$IFACE" && if_args="$IFACE"
73 test -z "$if_args" && if_args="-a"
74 ifconfig $if_args | $awk "$awk_mask2cdr"'
75 /^[a-z0-9:]+/ { ifnam=$1 };
76
77 $1 == "inet" { ifdata[ifnam]=ifdata[ifnam] "^" $2 "/" mask2cdr($4) };
78 $1 == "inet6" && !/inet6 ::|inet6 fe80:/ { ifdata[ifnam]=ifdata[ifnam] "^" $2 }
79
80 END { '"$awk_print_ifdata"'}'
81 ;;
82 *)
83 echo "Unhandled host-type: $UNAME"
84 esac