-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmcli-cli-restart
More file actions
executable file
·134 lines (112 loc) · 2.97 KB
/
Copy pathnmcli-cli-restart
File metadata and controls
executable file
·134 lines (112 loc) · 2.97 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
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
#!/bin/bash
#
# nmcli-cli-restart
#
# Copyright (c) 2019-2026 Jun Futagawa (jfut)
#
# This software is released under the MIT License.
# http://opensource.org/licenses/mit-license.php
VERSION="1.2.1"
ECHO_OR_EXECUTE="echo"
print_command() {
# Print the nmcli command in a reusable shell-quoted form.
printf '%q' "$1"
shift
printf ' %q' "$@"
printf '\n'
}
echo_or_execute() {
local RET
if [[ -z "${ECHO_OR_EXECUTE}" ]]; then
printf 'Applying: '
print_command "$@"
"$@"
RET=$?
if [[ ${RET} -ne 0 ]]; then
echo "Error: command failed (${RET})." >&2
exit "${RET}"
fi
else
print_command "$@"
fi
}
usage() {
local COMMAND_NAME
COMMAND_NAME="$(basename "${0}")"
cat << _EOF_
${COMMAND_NAME} ${VERSION}
Usage:
${COMMAND_NAME} [-n] [-x] IF_NAME
Options:
-n No interface check (Default: check interface)
-x Run command (Default: echo only)
Examples:
${COMMAND_NAME} eno1
_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
}
restart() {
local IF_NAME=${1:-}
local NMCLI
local UNIT_NAME
# down and up
# Schedule the recovery before disconnecting the current connection.
UNIT_NAME="nmcli-cli-restart-up-$$"
if [[ "${ECHO_OR_EXECUTE}" = "echo" ]]; then
# Keep command substitution literal so copied output resolves nmcli at runtime.
# shellcheck disable=SC2016
printf 'systemd-run --on-active=2s --unit=%q "$(command -v nmcli)" connection up %q\n' "${UNIT_NAME}" "${IF_NAME}"
echo_or_execute nmcli connection down "${IF_NAME}"
else
NMCLI=$(command -v nmcli)
if [[ -z "${NMCLI}" ]]; then
echo "Error: nmcli command not found." >&2
exit 1
fi
if ! command -v systemd-run > /dev/null 2>&1; then
echo "Error: systemd-run command not found." >&2
exit 1
fi
echo_or_execute systemd-run --on-active=2s --unit="${UNIT_NAME}" "${NMCLI}" connection up "${IF_NAME}"
echo_or_execute nmcli connection down "${IF_NAME}"
fi
}
# 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
restart "${@}"
}
[[ "${BASH_SOURCE[0]}" == "$0" ]] && main "$@"