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 [[ $EUID == 0 ]] ||
exec sudo
-E "${BASH_SOURCE[0]}" "$@"
35 m
() { printf "%s\n" "$*"; "$@"; }
37 ## begin arg parsing ##
41 comment
='# iank file id: ethusb-dhcp-v1'
53 comment
='# iank file id: ethusb-static-v1'
56 echo "$0: error unexpected argument: $1" >&2
68 # we already configured the interface once, afterwards, comment and
69 # uncomment to enable/disable. This makes it so we don't depend on /p
72 conf
=/etc
/NetworkManager
/system-connections
/ethusb-static.nmconnection
73 if ! $force && [[ -s $conf ]] && grep -qFx "$comment" $conf; then
74 # we already ran successfully in the past to set things this way, so
80 if [[ $
(dig +short @
10.2.0.1 -x 10.2.0.2 2>&1 ||
:) == kd.b8.nz.
]] \
81 && ip n show
10.2.0.1 |
grep .
&>/dev
/null
; then
84 while read -r ip_suf
host mac
; do
85 if [[ $mac != usb
]]; then
88 if [[ $host == ${HOSTNAME}c
]]; then
90 net_info
="address1=10.2.0.$ip_suf/16,10.2.0.1
97 if [[ ! $ip_suf ]]; then
98 echo "$0: error: failed to find ${HOSTNAME}c ip suffix in /p/c/host-info"
102 ip
=$
(getent ahostsv4
$HOSTNAME.office.fsf.org |
awk '{ print $1 }' |
head -n1)
103 net_info
="address1=$ip/24,192.168.0.1
104 dns=192.168.0.10;192.168.0.25;"
109 # device that has an eth0, but we aren't using it because it is
110 # broken. We could just hardcode a mac comparison with `cat
111 # /sys/class/net/eth0/address` but this is cooler.
112 if [[ -e /sys
/class
/net
/eth0
]]; then
113 bus_info
=$
(ethtool
-i eth0 |
awk '$1 == "bus-info:" { print $2 }')
114 if [[ $bus_info != usb
* ]]; then
119 ethx
=$
(( wiredx
- 1 ))
123 uuid
=$
(nmcli con show
"Wired connection $wiredx" 2>/dev
/null |
awk '$1 == "connection.uuid:" {print $2}' ||
:)
124 if [[ ! $uuid ]]; then
125 # just a uuid that nm generated for me at some point
126 uuid
=0da4c614-6a3c-3ad2-8d4b-c6eebe0814c3
130 # This template is the result of running, for example
131 # nmcli con mod "Wired connection 1" \
132 # ipv4.addresses "10.2.0.23/24" \
133 # ipv4.gateway "10.2.0.1" \
134 # ipv4.dns "8.8.8.4,8.8.8.8"
136 # which creates a fille named "Wired connection 1.nmconnection",
137 # below. I see no reason to keep the same file name, or a bunch of
138 # setting that seem irrelevant, and empty sections don't seem to do
139 # anything according to the man page.
142 # id=Wired connection 2
143 # uuid=b0fb7694-dfe6-31a1-81fa-7c17b61515a7
145 # interface-name=eth1
146 # timestamp=1715728264
151 # address1=10.2.0.23/16,10.2.0.1
152 # dns=8.8.8.4;8.8.8.8;
156 # addr-gen-mode=stable-privacy
164 id=Wired connection $wiredx
167 interface-name=eth$ethx
181 } |
install -T -m0600 /dev
/stdin
$conf
183 if ! $conf_only; then
184 state
=$
(nmcli con show
$uuid 2>/dev
/null |
awk '$1 == "GENERAL.STATE:" {print $2}' ||
:)
187 if [[ $state == activated
]]; then
194 m nmcli con down
$uuid
199 if ! grep -F "$comment" $conf; then
200 printf "%s\n" "$comment" >>$conf