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 if ! type -p dig &>/dev
/null
; then
105 ip
=$
(dig +short @
192.168.0.25 $HOSTNAME.office.fsf.org
)
106 net_info
="address1=$ip/24,192.168.0.1
107 dns=192.168.0.10;192.168.0.25;"
112 # device that has an eth0, but we aren't using it because it is
113 # broken. We could just hardcode a mac comparison with `cat
114 # /sys/class/net/eth0/address` but this is cooler.
115 if [[ -e /sys
/class
/net
/eth0
]]; then
116 bus_info
=$
(ethtool
-i eth0 |
awk '$1 == "bus-info:" { print $2 }')
117 if [[ $bus_info != usb
* ]]; then
122 ethx
=$
(( wiredx
- 1 ))
126 uuid
=$
(nmcli con show
"Wired connection $wiredx" 2>/dev
/null |
awk '$1 == "connection.uuid:" {print $2}' ||
:)
127 if [[ ! $uuid ]]; then
128 # just a uuid that nm generated for me at some point
129 uuid
=0da4c614-6a3c-3ad2-8d4b-c6eebe0814c3
133 # This template is the result of running, for example
134 # nmcli con mod "Wired connection 1" \
135 # ipv4.addresses "10.2.0.23/24" \
136 # ipv4.gateway "10.2.0.1" \
137 # ipv4.dns "8.8.8.4,8.8.8.8"
139 # which creates a fille named "Wired connection 1.nmconnection",
140 # below. I see no reason to keep the same file name, or a bunch of
141 # setting that seem irrelevant, and empty sections don't seem to do
142 # anything according to the man page.
145 # id=Wired connection 2
146 # uuid=b0fb7694-dfe6-31a1-81fa-7c17b61515a7
148 # interface-name=eth1
149 # timestamp=1715728264
154 # address1=10.2.0.23/16,10.2.0.1
155 # dns=8.8.8.4;8.8.8.8;
159 # addr-gen-mode=stable-privacy
167 id=Wired connection $wiredx
170 interface-name=eth$ethx
184 } |
install -T -m0600 /dev
/stdin
$conf
186 if ! $conf_only; then
187 state
=$
(nmcli con show
$uuid 2>/dev
/null |
awk '$1 == "GENERAL.STATE:" {print $2}' ||
:)
190 if [[ $state == activated
]]; then
197 m nmcli con down
$uuid
202 if ! grep -F "$comment" $conf; then
203 printf "%s\n" "$comment" >>$conf