-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddr
More file actions
executable file
·108 lines (94 loc) · 2.8 KB
/
addr
File metadata and controls
executable file
·108 lines (94 loc) · 2.8 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2014 Jeremy Brubaker <jbru362@gmail.com>
#
# abstract: list bound IP addresses
#
# Documentation {{{1
#
VERSION='1.0'
print_help() (
[ -n "$1" ] && printf "%s\n" "$1"
cat <<END
Usage: $0 [OPTION] [INTERFACE]
List bound IP addresses
-a include 127.* addresses
-l prepend address with the interface it is bound to
-4 print IPv4 addresses (default)
-6 print IPv6 addresses
-s sort output
-h display this help and exit
-v output version information and exit
END
) >&2
print_version() (
cat <<END
$0 $VERSION
Copyright (C) 2014 Orion Arts
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jeremy Brubaker.
END
) >&2
# Process options {{{1
#
all='n'
long='n'
sort='n'
v4='y'
v6='n'
while getopts ':alshv46' c; do
case $c in
a) all='y' ;;
l) long='y' ;;
4) v4='y'; v6='n' ;;
6) v6='y'; v4='n' ;;
s) sort='y' ;;
h) print_help; exit 0 ;;
v) print_version; exit 0 ;;
?) print_help "Invalid option: -$OPTARG"; exit 1 ;;
esac
done
shift $((OPTIND-1))
OPTIND=1
# Easy way to not ignore 127.* addresses if we ask for such an interface
# explicitly
[ -n "$1" ] && all='y'
# Main {{{1
#
if ! command -v ip >/dev/null 2>/dev/null; then
printf "ip(8) not found. Exiting\n" >&2
exit 1
fi
[ "$v4" = 'y' ] && inet="inet"
[ "$v6" = 'y' ] && inet="inet6"
# Not quoting $1 allows ignoring non-existent parameter
# shellcheck disable=SC2086
addrs=$(ip address show $1 | awk ' \
/^[[:digit:]]+:/ { iface = gensub(":", "", 1, $2) } \
/'$inet' / { printf("%s\t%s\t%s\n", iface, \
gensub("/.*$", "", 1, $2), \
gensub("^[^/]*", "", 1, $2))}')
[ "$all" != 'y' ] && addrs=$(echo "$addrs" \
| grep -Ev '\s127')
[ "$long" = 'n' ] && addrs=$(echo "$addrs" \
| sed 's@^.*\s\(.*\)\s.*$@\1@')
# Pipe through sort if needed, otherwise tee just passes it on
echo "$addrs" |
if [ "$sort" = 'y' ]; then sort
else tee
fi | column --table