shellcheck, static usb ethnet addresses, fix vpn config
[distro-setup] / filesystem / usr / local / bin / ethusb-static
1 #!/bin/bash
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
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
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
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
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.
22
23 if ! test "$BASH_VERSION"; then echo "error: shell is not bash" >&2; exit 1; fi
24 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
25 set -eE -o pipefail
26 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
27
28 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
29
30
31 shopt -s nullglob
32
33 # we already configured the interface once, afterwards, comment and
34 # uncomment to enable/disable. This makes it so we don't depend on /p
35 # being mounted.
36
37 if [[ -s /etc/network/interfaces.d/ethusb ]]; then
38 sed -i --follow-symlinks 's/^#//' /etc/network/interfaces.d/ethusb
39 exit 0
40 fi
41
42
43 while read -r ip host mac; do
44 if [[ $mac != usb ]]; then
45 continue
46 fi
47 if [[ $host = ${HOSTNAME}c ]]; then
48 usbip=$ip
49 break
50 fi
51 done </p/c/host-info
52
53 if [[ ! $usbip ]]; then
54 exit 0
55 fi
56
57 ethx=0
58
59 # device that has an eth0, but we aren't using it because it is
60 # broken. We could just hardcode a mac comparison with `cat
61 # /sys/class/net/eth0/address` but this is cooler.
62 if [[ -e /sys/class/net/eth0 ]]; then
63 bus_info=$(ethtool -i eth0 | awk '$1 == "bus-info:" { print $2 }')
64 if [[ $bus_info != usb* ]]; then
65 ethx=1
66 fi
67 fi
68
69 cat >/etc/network/interfaces.d/ethusb <<EOF
70 auto eth$ethx
71 iface eth$ethx inet static
72 address 10.2.0.$ip/16
73 gateway 10.2.0.1
74 EOF