-
Notifications
You must be signed in to change notification settings - Fork 11
/
addrof.sh
executable file
·82 lines (67 loc) · 1.56 KB
/
addrof.sh
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
#!/bin/bash
# SPDX-FileCopyrightText: © 2020 Georg Sauthoff <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
function help
{
cat <<EOF
addrof - list IP address(es) of a network device
call: addrof [OPT...] DEVICE
Options:
-4 only list IPv4 addresses
-6 only list IPv6 addresses
-a, --all list all IP addresses (i.e. not just global ones)
-p, --primary list only primary IP addresses
-h, --help display this help and exit
EOF
}
dev=
addrflag=
primaryflag=
scopeflag="scope global"
function parse_args
{
local t
t=$(getopt -o ap46h --long all,primary,help -n addrof -- "$@")
if [ $? -ne 0 ]; then
echo 'Option parse error' >&2
exit 1
fi
eval set -- "$t"
while :; do
case "$1" in
-a|--all)
scopeflag=
shift
;;
-p|--primary)
primaryflag=primary
shift
;;
-4)
addrflag=-4
shift
;;
-6)
addrflag=-6
shift
;;
-h|--help)
help
exit 0
shift
;;
--)
shift
break
;;
esac
done
if [ $# -lt 1 ]; then
echo 'device name missing' >&2
exit 1
fi
dev=$1
}
parse_args "$@"
ip $addrflag -o addr show dev "$dev" up $primaryflag $scopeflag \
| awk -F' +|/' '{ print $4}'