-
Notifications
You must be signed in to change notification settings - Fork 8
/
ifaddr
executable file
·67 lines (57 loc) · 1.78 KB
/
ifaddr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /bin/sh
# ifaddr (Bourne shell script) -- Pretty-print the output of ""
#
# Usage: ifaddr [ -v ]
#
# TO-DO: colourise with rainbow, etc.
if ! awk --version 2>&1 | grep -q '^GNU Awk'
then
echo 'ifaddr: Fatal error: "GNU awk" required' >&2
exit 3
fi
if [ "$1" = "-v" ] ; then
params="SHOW_HW=1"
fi
ip -o addr show |
awk '# Script parameters: SHOW_HW (optional), NO_SKIP_LL (optional)
function lprint(line)
{
if (line !~ /^\t/)
print "";
print line
}
# Main processing logic that strips interface name off address lines and
# indents them with a tab; also removes surplus information and
# everything after a backslash
{
gsub(/(qdisc|qlen) [^ \\]* */, "");
if (previd != $1)
line = gensub(/^[0-9]*: ([^\\]*).*/, "\\1", 1);
else
line = gensub(/^[0-9]*: [^ ]* *([^\\]*).*/, "\t\\1", 1);
# Used to track interface change
previd = $1;
}
# Ignore IPv6 link-local addresses
/scope link/ && NO_SKIP_LL != 1 { next }
# Grab the MAC address from the info after the backslash
SHOW_HW && /\\ link\/[^nl]/ { line = line gensub(/.*\\ (link\/[^ ]* *[^ ]*).*/, "\n\t\\1", 1); }
{
# Secondary formatting logic; puts IPv4 addr label before the address
line = gensub(/(\tinet )(.*scope [^ \\]*) (.*)/, "\\1\"\\3\" \\2", 1, line);
# Sorting logic; loopback first, tunnels last
if ($2 ~ /lo/)
loopback[lcount++] = line;
else if ($2 ~ /^tun/)
tunnels[tcount++] = line;
else
other[ocount++] = line
}
END {
# Print lines from the arrays, preceeding each interface line with a newline
# (except for loopback, which is shown first)
for (i in loopback) print loopback[i];
for (i in other) lprint(other[i]);
for (i in tunnels) lprint(tunnels[i]);
}
' $params