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" "$*"; "$@"; }
35 ## begin arg parsing ##
48 echo "$0: error unexpected argument: $1" >&2
62 # device that has an eth0, but we aren't using it because it is
63 # broken. We could just hardcode a mac comparison with `cat
64 # /sys/class/net/eth0/address` but this is cooler.
65 if [[ -e /sys
/class
/net
/eth0
]]; then
66 bus_info
=$
(ethtool
-i eth0 |
awk '$1 == "bus-info:" { print $2 }')
67 if [[ $bus_info != usb
* ]]; then
72 eth_dev
=eth$
(( wiredx
- 1 ))
74 nm_con
=$
(nmcli device show
$eth_dev | \
75 awk '$1 == "GENERAL.CONNECTION:" {out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}' ||
:)
77 if [[ ! $nm_con ]]; then
78 nm_con
="Wired connection $wiredx"
83 if nmcli con |
grep -q "^$nm_con " &>/dev
/null
; then
90 if ! $con_exists; then
91 echo "warning: no existing connection: $nm_con found in output of nmcli con"
96 tmpstr
=$
(nmcli con show
"$nm_con" 2>/dev
/null |
sort -r |
awk '$1 == "ipv4.method:" || $1 == "ipv4.addresses:" || $1 == "ipv4.gateway:" || $1 == "ipv4.dns:" || $1 == "GENERAL.STATE:" {print $2}' )
101 read -r ipv4_addresses
106 if [[ $state == activated
]]; then
110 if [[ $ipv4_method != auto
]]; then
111 args
+=(ipv4.method auto
)
113 if [[ $ipv4_addresses != -- ]]; then
114 args
+=(-ipv4.addresses
"$ipv4_addresses")
116 if [[ $ipv4_dns != -- ]]; then
117 args
+=(-ipv4.dns
"$ipv4_dns")
119 if [[ $ipv4_gateway != -- ]]; then
120 # undocumented in t11 man nmcli. guessed randomly
121 args
+=(ipv4.gateway
0.0.0.0)
123 if (( ${#args[@]} >= 1 )); then
124 m nmcli con mod
"$nm_con" "${args[@]}"
126 m nmcli con up
"$nm_con"
129 echo "$0: found expected state, nothing to do."
135 if [[ $
(dig +short @
10.2.0.1 -x 10.2.0.2 2>&1 ||
:) == kd.b8.nz.
]] \
136 && ip n show
10.2.0.1 |
grep .
&>/dev
/null
; then
137 # we are at_home=true
139 while read -r ip_suf
host mac
; do
140 if [[ ! $ip_suf ||
$ip_suf == \
#* ]]; then
143 if [[ $mac != usb
]]; then
146 if [[ $host == ${HOSTNAME}c
]]; then
155 if [[ ! $ip_suf ]]; then
156 echo "$0: error: failed to find ${HOSTNAME}c ip suffix in /p/c/host-info"
160 if ! type -p dig &>/dev
/null
; then
163 myip
=$
(dig +short @
192.168.0.25 $HOSTNAME.office.fsf.org
)
165 echo "$0: error: didnt detect home network and failed to get office ip"
168 dns
=192.168.0.10,192.168.0.25
173 if ! $force && $con_exists; then
174 current
=$
(nmcli con show
"$nm_con" 2>/dev
/null |
sort -r |
awk '$1 == "ipv4.method:" || $1 == "ipv4.addresses:" || $1 == "ipv4.gateway:" || $1 == "ipv4.dns:" {print $2}')
179 if [[ $current == "$expected" ]]; then
180 echo "$0: found expected state, nothing to do."
185 m nmcli con mod
'Wired connection 1' ipv4.method manual ipv4.addresses
$ip ipv4.gateway
$gateway ipv4.dns
$dns
187 state
=$
(nmcli con show
"$nm_con" 2>/dev
/null |
awk '$1 == "GENERAL.STATE:" {print $2}')
188 if [[ $state == activated
]]; then
189 m nmcli con up
"$nm_con"
193 # example of down cli
194 #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"
197 # FYI: the result of running, for example
198 # nmcli con mod "Wired connection 1" \
199 # ipv4.method manual \
200 # ipv4.addresses "10.2.0.23/24" \
201 # ipv4.gateway "10.2.0.1" \
202 # ipv4.dns "8.8.8.4,8.8.8.8"
204 # creates a fille named "/etc/NetworkManager/system-connections/Wired connection 1.nmconnection",
207 # The nmcli man page says you should just edit files in that dir and
208 # then run nmcli con reload to reread them all to load your changes, but
209 # I've found that to be unreliable, the systemd journal would say
210 # something like "reload happened" then nothing would change in the
211 # connect that the file clearly modifies, so I switched over to using
212 # the command line and just ignoring those files.
214 # I see no reason to keep the same file name, or a bunch of
215 # setting that seem irrelevant, and empty sections don't seem to do
216 # anything according to the man page.
220 # id=Wired connection 1
221 # uuid=b0fb7694-dfe6-31a1-81fa-7c17b61515a7
223 # interface-name=eth1
224 # timestamp=1715728264
229 # address1=10.2.0.23/16,10.2.0.1
230 # dns=8.8.8.4;8.8.8.8;
234 # addr-gen-mode=stable-privacy