2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
9 # Copyright 2024 Ian Kelling
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
15 # http://www.apache.org/licenses/LICENSE-2.0
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
24 # off: Turn off static ip.
25 # -c config only, don't tell networkmanager to change anything
26 # -f force interface reup
28 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
29 shopt -s inherit_errexit
2>/dev
/null ||
: # ignore fail in bash < 4.4
31 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
33 m
() { printf "%s\n" "$*"; "$@"; }
39 if [[ $cur_state == activated
]]; then
43 if [[ $cur_method != auto
]]; then
44 args
+=(ipv4.method auto
)
46 if [[ $cur_ip != -- ]]; then
47 args
+=(-ipv4.addresses
"$ipv4_addresses")
49 if [[ $cur_dns != -- ]]; then
50 args
+=(-ipv4.dns
"$ipv4_dns")
52 if [[ $cur_gateway != -- ]]; then
53 # undocumented in t11 man nmcli. guessed randomly
54 args
+=(ipv4.gateway
0.0.0.0)
56 if (( ${#args[@]} >= 1 )); then
57 m nmcli con mod
"$nm_con" "${args[@]}"
59 m nmcli con up
"$nm_con"
62 echo "$0: found expected state, nothing to do."
70 # this assumes we have wifi up
71 if [[ $
(timeout
1 dig +short @
10.2.0.1 -x 10.2.0.2 2>&1 ||
:) == kd.b8.nz.
]] \
72 && ip n show
10.2.0.1 |
grep .
&>/dev
/null
; then
74 elif ip r show default |
grep 'via 10.0.3.1 dev wlan0' &>/dev
/null
&& [[ $
(timeout
1 dig +short @
10.0.3.1 -x 10.0.3.1) == cmc1.lan.
]]; then
77 echo "$0: error could not detect network"
84 m nmcli con mod
"$nm_con" ipv4.method manual ipv4.addresses
$ip ipv4.gateway
$gateway ipv4.dns
$dns
85 state
=$
(nmcli con show
"$nm_con" 2>/dev
/null |
awk '$1 == "GENERAL.STATE:" {print $2}')
86 if [[ $state == activated
]]; then
87 m nmcli con up
"$nm_con"
97 while read -r ip_suf
host mac
; do
98 if [[ ! $ip_suf ||
$ip_suf == \
#* ]]; then
101 if [[ $mac != usb
]]; then
104 if [[ $host == ${HOSTNAME}c
]]; then
113 if [[ ! $ip_suf ]]; then
114 echo "$0: error: failed to find ${HOSTNAME}c ip suffix in /p/c/host-info"
120 if ! ip r show default |
grep 'via 192.168.0.1 dev eth' &>/dev
/null
; then
121 if [[ $cur_method != manual
]]; then
122 echo "$0: error. Need to be on wired network to get our ip"
128 myip
=$
(timeout
1 dig +short @
192.168.0.25 $HOSTNAME.office.fsf.org
)
129 if [[ ! $myip ]]; then
130 echo "$0: error: didnt detect home network and failed to get office ip"
133 dns
=192.168.0.10,192.168.0.25
145 printf "%s\n" "$tmpstr" |
awk '$1 == "'$key':" {print $2}'
149 tmpstr
=$
(nmcli con show
"$nm_con" 2>/dev
/null
)
151 cur_method
=$
(get-cur-val ipv4.method
)
152 cur_ip
=$
(get-cur-val ipv4.addresses
)
153 cur_gateway
=$
(get-cur-val ipv4.gateway
)
154 cur_dns
=$
(get-cur-val ipv4.dns
)
155 cur_state
=$
(get-cur-val GENERAL.STATE
)
159 ## begin arg parsing ##
172 echo "$0: error unexpected argument: $1" >&2
179 ## end arg parsing ##
181 ## begin common setup / detection ##
187 # device that has an eth0, but we aren't using it because it is
188 # broken. We could just hardcode a mac comparison with `cat
189 # /sys/class/net/eth0/address` but this is cooler.
190 if [[ -e /sys
/class
/net
/eth0
]]; then
191 bus_info
=$
(ethtool
-i eth0 |
awk '$1 == "bus-info:" { print $2 }')
192 if [[ $bus_info != usb
* ]]; then
197 eth_dev
=eth$
(( wiredx
- 1 ))
199 nm_con
=$
(nmcli device show
$eth_dev | \
200 awk '$1 == "GENERAL.CONNECTION:" {out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}' ||
:)
202 if [[ ! $nm_con ||
$nm_con == -- ]]; then
203 nm_con
="Wired connection $wiredx"
206 if ! nmcli con |
grep -q "^$nm_con " &>/dev
/null
; then
207 # Note: we could support creation through a file or via
208 # nmcli, but right now I'm ok with just having plugged in a device once
209 # since this os was installed.
210 echo "error: no existing connection: $nm_con found in output of nmcli con"
215 if ! type -p dig &>/dev
/null
; then
220 ## end common setup / detection ##
230 if ! $force && [[ "$cur_method $cur_gateway $cur_dns $cur_ip" == "manual $gateway $dns $ip" ]]; then
231 echo "$0: found expected state, nothing to do."
239 # example of down cli
240 #nmcli con mod 'Wired connection 1' ipv4.method auto -ipv4.addresses 10.2.0.9/16 ipv4.gateway 0.0.0.0 -ipv4.dns "8.8.8.4,8.8.8.8"
243 # FYI: the result of running, for example
244 # nmcli con mod "Wired connection 1" \
245 # ipv4.method manual \
246 # ipv4.addresses "10.2.0.23/24" \
247 # ipv4.gateway "10.2.0.1" \
248 # ipv4.dns "8.8.8.4,8.8.8.8"
250 # creates a fille named "/etc/NetworkManager/system-connections/Wired connection 1.nmconnection",
253 # The nmcli man page says you should just edit files in that dir and
254 # then run nmcli con reload to reread them all to load your changes, but
255 # I've found that to be unreliable, the systemd journal would say
256 # something like "reload happened" then nothing would change in the
257 # connect that the file clearly modifies, so I switched over to using
258 # the command line and just ignoring those files.
260 # I see no reason to keep the same file name, or a bunch of
261 # setting that seem irrelevant, and empty sections don't seem to do
262 # anything according to the man page.
266 # id=Wired connection 1
267 # uuid=b0fb7694-dfe6-31a1-81fa-7c17b61515a7
269 # interface-name=eth1
270 # timestamp=1715728264
275 # address1=10.2.0.23/16,10.2.0.1
276 # dns=8.8.8.4;8.8.8.8;
280 # addr-gen-mode=stable-privacy