-
-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathset-static-ip
More file actions
executable file
·179 lines (156 loc) · 4.65 KB
/
set-static-ip
File metadata and controls
executable file
·179 lines (156 loc) · 4.65 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash
#
# Sets a static local IP address for the TinyPilot device.
# Exit on first error.
set -e
print_help() {
cat << EOF
Usage: ${0##*/} --router router_ip --ip static_ip [--dns dns_ip]...
[--interface name]
Sets a static local IP address for the TinyPilot device.
--router router_ip: The IP address of the local router (e.g., 10.0.0.1).
--ip static_ip: The static IP address to assign to the TinyPilot device
(e.g., 10.0.0.50).
--dns dns_ip: Optional. A DNS server to use to resolve domain names
(e.g., 8.8.8.8). Multiple DNS servers can be provided. If
unspecified, defaults to the router's DNS, then Google
(8.8.8.8), then Cloudflare (1.1.1.1).
--interface name: Optional. The interface to configure (e.g., eth0).
Defaults to eth0.
--help: Display this help text.
Note: there can only be one custom static IP configuration at a time. Running
this script will clear any potentially existing custom configuration
that might have been added previously by this script.
Examples:
1. Set static IP to 10.0.105 on a 10.0.0.x network.
set-static-ip \\
--router 10.0.0.1 \\
--ip 10.0.0.105
2. Configure wlan0 with a static IP of 192.168.1.100, with a Quad9 DNS server and a local fallback DNS server.
set-static-ip \\
--router 192.168.1.1 \\
--ip 192.168.1.100 \\
--dns 9.9.9.9 \\
--dns 192.168.1.1 \\
--interface wlan0
EOF
}
print_help_nudge() {
echo "For help on using this tool:" >&2
echo " set-static-ip --help" >&2
}
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 "ip:,router:,dns:,interface:" -- "$@"); then
print_help
exit 1
fi
# Process command-line arguments.
eval set -- "${NEW_ARGS}"
while true; do
case "$1" in
--ip)
if [[ -z "${IP_ADDRESS}" ]] ; then
readonly IP_ADDRESS="$2"
else
echo "Only one static IP address should be provided using --ip" >&2
print_help_nudge
exit 1
fi
shift 2
;;
--router)
if [[ -z "${ROUTERS}" ]] ; then
readonly ROUTERS="$2"
else
echo "Only one router IP address should be provided using --router" >&2
print_help_nudge
exit 1
fi
shift 2
;;
--dns)
if [[ -z "${DNS}" ]] ; then
DNS="$2"
else
DNS="${DNS} $2"
fi
shift 2
;;
--interface)
if [[ -z "${INTERFACE}" ]] ; then
readonly INTERFACE="$2"
else
echo "Only one interface should be provided using --interface" >&2
print_help_nudge
exit 1
fi
shift 2
;;
--)
shift
break
;;
esac
done
# There must be at least a static IP address and a router IP address.
if [[ -z "${IP_ADDRESS}" ]] ; then
echo "You must specify an IP address using --ip" >&2
print_help_nudge
exit 1
fi
if [[ -z "${ROUTERS}" ]] ; then
echo "You must specify a router IP address using --router" >&2
print_help_nudge
exit 1
fi
# Default DNS settings.
if [[ -z "${DNS}" ]] ; then
DNS="${ROUTERS} 8.8.8.8 1.1.1.1"
fi
readonly DNS
# Default interface.
if [[ -z "${INTERFACE}" ]] ; then
readonly INTERFACE="eth0"
fi
# Exit on unset variable.
set -u
# Determine which version of the OS is running.
OS_VERSION="$(lsb_release --release --short)"
readonly OS_VERSION
if (( "${OS_VERSION}" == 11 )); then
# Remove any existing automated configuration.
"${SCRIPT_DIR}/strip-marker-sections" "${CONFIG_FILE}"
# Only proceed if no other configuration exists.
if grep -q "^interface ${INTERFACE}" "${CONFIG_FILE}" ; then
echo "An existing configuration exists in ${CONFIG_FILE} for ${INTERFACE}." >&2
echo "Please remove the existing configuration and try again." >&2
exit 1
fi
# Write out the new configuration.
{
echo "${MARKER_START}"
echo "interface ${INTERFACE}"
echo "static ip_address=${IP_ADDRESS}"
echo "static routers=${ROUTERS}"
echo "static domain_name_servers=${DNS}"
echo "${MARKER_END}"
} | sudo tee --append "${CONFIG_FILE}" > /dev/null
else
nmcli connection modify 'Wired connection 1' \
ipv4.method manual \
ipv4.addresses "${IP_ADDRESS}" \
ipv4.gateway "${ROUTERS}" \
ipv4.dns "${DNS}"
fi
# Apply changes.
"${SCRIPT_DIR}/apply-static-ip"
echo "A static local IP address has been set."
echo " Interface: ${INTERFACE}"
echo " IP Address: ${IP_ADDRESS}"
echo " Routers: ${ROUTERS}"
echo " Domain Name Servers: ${DNS}"