-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmcli-cli-bridge-delete
executable file
·100 lines (80 loc) · 1.97 KB
/
nmcli-cli-bridge-delete
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
#!/bin/bash
#
# nmcli-cli-bridge-delete
#
# 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] BRIDGE_NAME
Options:
-n No interface check (Default: check interface)
-x Run command (Default: echo only)
Examples:
$0 br1
_EOF_
}
check() {
local BRDIGE_IF_NAME=${1:-}
# Check bridge interface
nmcli connection show "${BRDIGE_IF_NAME}" > /dev/null 2>&1
RET=$?
if [[ ${RET} -ne 0 ]]; then
echo "Error: Target bridge interface \"${BRDIGE_IF_NAME}\" not found."
exit 1
fi
}
bridge_delete() {
local BRIDGE_NAME=${1:-}
# Find lower interface
INTERFACE_LIST=$(ls "/sys/class/net/${BRIDGE_NAME}/" | grep "^lower_" | sed 's|lower_||')
# Delete lower interface
for i in ${INTERFACE_LIST}
do
echo_or_execute nmcli connection modify \"${i}\" connection.master \"\" connection.slave-type \"\"
done
# Delete bridge interface
echo_or_execute nmcli connection delete \"${BRIDGE_NAME}\"
}
# 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
bridge_delete "${@}"
}
[[ ${#BASH_SOURCE[@]} = 1 ]] && main "${@}"