forked from tiny-pilot/tinypilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunset-static-ip
executable file
·126 lines (108 loc) · 3.12 KB
/
unset-static-ip
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
#!/bin/bash
#
# Reverts changes made by the set-static-ip script.
# Exit on first error.
set -e
print_help() {
cat << EOF
Usage: ${0##*/} [--interface name]
Reverts changes made by the set-static-ip script.
--interface name: Optional. The interface to configure (e.g., eth0). Defaults
to eth0.
--quiet: Optional. Surpress the confirmation message.
--help: Display this help text.
Examples:
1. Unset the static IP address on the default eth0 interface.
unset-static-ip
2. Quietly unset a static IP address on the wlan0 interface.
unset-static-ip \\
--interface wlan0
EOF
}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
readonly SCRIPT_DIR
readonly CONFIG_FILE="/etc/dhcpcd.conf"
# shellcheck source=lib/markers.sh
. "${SCRIPT_DIR}/lib/markers.sh"
# Parse command-line arguments.
if ! NEW_ARGS=$(getopt -q -o "" -l "quiet,interface:" -- "$@"); then
print_help
exit 1
fi
# Process command-line arguments.
eval set -- "${NEW_ARGS}"
while true; do
case "$1" in
--quiet)
readonly QUIET="true"
shift 1
;;
--interface)
if [[ -z "${INTERFACE}" ]] ; then
readonly INTERFACE="$2"
else
echo "Only one interface should be provided using --interface" >&2
echo "For help on using this tool:" >&2
echo " unset-static-ip --help" >&2
exit 1
fi
shift 2
;;
--)
shift
break
;;
esac
done
# Default interface.
if [[ -z "${INTERFACE}" ]] ; then
readonly INTERFACE="eth0"
fi
# Default to not quiet.
if [[ -z "${QUIET}" ]] ; then
readonly QUIET="false"
fi
# Exit on unset variable
set -u
# Remove any existing auto-generated configuration for the interface.
dhcpcd_original=()
section=()
interface_matched="false"
while IFS= read -r line; do
if [[ "${#section[@]}" -ne 0 ]] && [[ "${line}" == "${MARKER_END}" ]]; then
section+=("${line}")
if [[ "${interface_matched}" == "false" ]] ; then
dhcpcd_original=("${dhcpcd_original[@]}" "${section[@]}")
fi
section=()
interface_matched="false"
continue
fi
if [[ "${#section[@]}" -ne 0 ]] || [[ "${line}" == "${MARKER_START}" ]]; then
if [[ "${line}" == "interface ${INTERFACE}" ]] ; then
interface_matched="true"
fi
section+=("${line}")
continue
fi
dhcpcd_original+=("${line}")
done < "${CONFIG_FILE}"
if [[ "${#section[@]}" -ne 0 ]] ; then
echo "The contents of ${CONFIG_FILE} are not in the expected format." >&2
echo "Auto-generated changes may have been manually edited." >&2
echo "" >&2
echo "Remove the relevant configuration manually to proceed." >&2
exit 1
fi
# Convert array of lines to a single string.
OLD_CONFIG_FILE=$(printf "%s\n" "${dhcpcd_original[@]}")
readonly OLD_CONFIG_FILE
# Write original file contents back to the file.
echo "${OLD_CONFIG_FILE}" | sudo tee "${CONFIG_FILE}" > /dev/null
# Changes require a reboot so prompt the user.
if [[ "${QUIET}" == "false" ]] ; then
echo "Any changes made by the set-static-ip script have been reverted."
echo " Interface: ${INTERFACE}"
echo ""
echo "Reboot your TinyPilot device to apply the changes."
fi