-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmcli-cli-ipv6
executable file
·162 lines (134 loc) · 3.92 KB
/
nmcli-cli-ipv6
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/bash
#
# nmcli-cli-ipv6
#
# Copyright (c) 2019 Jun Futagawa (jfut)
#
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
ECHO_OR_EXECUTE="echo"
echo_or_execute() {
if [[ -z "${ECHO_OR_EXECUTE}" ]]; then
echo "Applying: ${@}"
eval ${@}
else
${ECHO_OR_EXECUTE} ${@}
fi
}
usage() {
cat << _EOF_
Usage:
$0 [-n] [-x] NAME dhcp|static|disable IP_SUBNET [GATEWAY] [DNS]
Options:
-n No interface check (Default: check interface)
-x Run command (Default: echo only)
Examples:
$0 eno1 auto
$0 eno1 dhcp
$0 eno1 static 2001:db8:1::101/48 2001:db8:1::1 "2001:db8:1::1,2001:db8:1::2"
$0 eno1 static 2001:db8:1::101/48
$0 eno1 link-local
$0 eno1 disable
_EOF_
}
check() {
local IF_NAME=${1:-}
# Check interface
nmcli connection show "${IF_NAME}" > /dev/null 2>&1
RET=$?
if [[ ${RET} -ne 0 ]]; then
echo "Error: Target interface \"${IF_NAME}\" not found."
exit 1
fi
}
ipv6_set_auto_method() {
local IF_NAME=${1:-}
local IF_METHOD=${2:-}
# DNS
echo_or_execute nmcli connection modify \"${IF_NAME}\" ipv6.dns \"\"
# Gateway
echo_or_execute nmcli connection modify \"${IF_NAME}\" ipv6.gateway \"\"
# IP
echo_or_execute nmcli connection modify \"${IF_NAME}\" ipv6.addresses \"\" ipv6.method "${IF_METHOD}"
}
ipv6_set_static() {
local IF_NAME=${1:-}
local IF_IP_SUBNET=${2:-}
local IF_GATEWAY=${3:-}
local IF_DNS=${4:-}
# IP
echo_or_execute nmcli connection modify \"${IF_NAME}\" ipv6.addresses \"${IF_IP_SUBNET}\" ipv6.method manual
# Option: Gateway
if [[ ! -z "${IF_GATEWAY}" ]]; then
echo_or_execute nmcli connection modify \"${IF_NAME}\" ipv6.gateway \"${IF_GATEWAY}\"
fi
# Option: DNS
if [[ ! -z "${IF_DNS}" ]]; then
echo_or_execute nmcli connection modify \"${IF_NAME}\" ipv6.dns \"${IF_DNS}\"
fi
}
ipv6_set() {
IF_NAME=${1:-}
IF_METHOD=${2:-}
IF_IP_SUBNET=${3:-}
IF_GATEWAY=${4:-}
IF_DNS=${5:-}
if [[ "${IF_METHOD}" = "auto" ]]; then
ipv6_set_auto_method "${IF_NAME}" "auto"
elif [[ "${IF_METHOD}" = "dhcp" ]]; then
ipv6_set_auto_method "${IF_NAME}" "dhcp"
elif [[ "${IF_METHOD}" = "link-local" ]]; then
ipv6_set_auto_method "${IF_NAME}" "link-local"
elif [[ "${IF_METHOD}" = "static" ]] || [[ "${IF_METHOD}" = "manual" ]]; then
ipv6_set_static "${IF_NAME}" "${IF_IP_SUBNET}" "${IF_GATEWAY}" "${IF_DNS}"
elif [[ "${IF_METHOD}" = "link-local" ]]; then
ipv6_set_auto_method "${IF_NAME}" "link-local"
elif [[ "${IF_METHOD}" = "disable" ]] || [[ "${IF_METHOD}" = "ignore" ]]; then
ipv6_set_auto_method "${IF_NAME}" "ignore"
else
echo "Error: method \"${IF_METHOD}\" is invalid. (auto|dhcp|static|link-local|disable) are supported."
exit 1
fi
cat << _EOF_
# Next steps:
# -> Restart the interface:
# nmcli-cli-restart "${IF_NAME}"
#
# -> Create a bond interface:
# nmcli-cli-bond-add bond1 mode=... "${IF_NAME}" "ens2f0"
# -> Create a vlan interface:
# nmcli-cli-vlan-add "${IF_NAME}.100" 100 "${IF_NAME}"
# -> Create a bridge interface:
# nmcli-cli-bridge-add br1 "${IF_NAME}"
_EOF_
}
# Main
main() {
local NO_CHECK="no"
while getopts nxh OPT; do
case "${OPT}" in
"n" )
NO_CHECK="yes" ;;
"x" )
ECHO_OR_EXECUTE="" ;;
"h" )
usage
exit 0
;;
* )
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
[[ $# -lt 1 ]] && usage && exit 1
if [[ "${NO_CHECK}" = "no" ]]; then
check "${@}"
fi
if [[ "${ECHO_OR_EXECUTE}" = "echo" ]]; then
echo "# ${ECHO_OR_EXECUTE} only."
fi
ipv6_set "${@}"
}
[[ ${#BASH_SOURCE[@]} = 1 ]] && main "${@}"