From b55f9ec50bfd6b25d2f1e7445518625f42357735 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Fri, 7 Jun 2024 00:05:47 -0400 Subject: [PATCH] general improvements --- .../distro-install-common/ethusb-static | 246 +++++++++++------- wrt-setup-local | 4 +- 2 files changed, 148 insertions(+), 102 deletions(-) diff --git a/fai/config/distro-install-common/ethusb-static b/fai/config/distro-install-common/ethusb-static index fc31af1..0a6d1ae 100755 --- a/fai/config/distro-install-common/ethusb-static +++ b/fai/config/distro-install-common/ethusb-static @@ -32,6 +32,130 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR m() { printf "%s\n" "$*"; "$@"; } + +set-dynamic() { + + reup=false + if [[ $cur_state == activated ]]; then + reup=true + fi + + if [[ $cur_method != auto ]]; then + args+=(ipv4.method auto) + fi + if [[ $cur_ip != -- ]]; then + args+=(-ipv4.addresses "$ipv4_addresses") + fi + if [[ $cur_dns != -- ]]; then + args+=(-ipv4.dns "$ipv4_dns") + fi + if [[ $cur_gateway != -- ]]; then + # undocumented in t11 man nmcli. guessed randomly + args+=(ipv4.gateway 0.0.0.0) + fi + if (( ${#args[@]} >= 1 )); then + m nmcli con mod "$nm_con" "${args[@]}" + if $reup; then + m nmcli con up "$nm_con" + fi + else + echo "$0: found expected state, nothing to do." + fi + exit 0 + set-nm +} + +detect-net() { + + # this assumes we have wifi up + if [[ $(timeout 1 dig +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]] \ + && ip n show 10.2.0.1 | grep . &>/dev/null; then + net=home + 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 + net=work + else + echo "$0: error could not detect network" + exit 1 + fi + +} + +set-nm() { + m nmcli con mod "$nm_con" ipv4.method manual ipv4.addresses $ip ipv4.gateway $gateway ipv4.dns $dns + state=$(nmcli con show "$nm_con" 2>/dev/null | awk '$1 == "GENERAL.STATE:" {print $2}') + if [[ $state == activated ]]; then + m nmcli con up "$nm_con" + fi + +} + +get-ip() { + + case $net in + home) + + while read -r ip_suf host mac; do + if [[ ! $ip_suf || $ip_suf == \#* ]]; then + continue + fi + if [[ $mac != usb ]]; then + continue + fi + if [[ $host == ${HOSTNAME}c ]]; then + + ip=10.2.0.$ip_suf/16 + gateway=10.2.0.1 + dns=8.8.8.4,8.8.8.8 + break + fi + done

/dev/null; then + if [[ $cur_method != manual ]]; then + echo "$0: error. Need to be on wired network to get our ip" + exit 1 + fi + set-dynamic + sleep 10 + fi + myip=$(timeout 1 dig +short @192.168.0.25 $HOSTNAME.office.fsf.org) + if [[ ! $myip ]]; then + echo "$0: error: didnt detect home network and failed to get office ip" + exit 1 + fi + dns=192.168.0.10,192.168.0.25 + gateway=192.168.0.1 + ip=$myip/24 + + ;; + esac +} + + +get-cur-val() { + local key + key=$1 + printf "%s\n" "$tmpstr" | awk '$1 == "'$key':" {print $2}' +} + +get-cur() { + tmpstr=$(nmcli con show "$nm_con" 2>/dev/null) + + cur_method=$(get-cur-val ipv4.method) + cur_ip=$(get-cur-val ipv4.addresses) + cur_gateway=$(get-cur-val ipv4.gateway) + cur_dns=$(get-cur-val ipv4.dns) + cur_state=$(get-cur-val GENERAL.STATE) +} + + ## begin arg parsing ## force=false @@ -54,10 +178,11 @@ done ## end arg parsing ## - +## begin common setup / detection ## shopt -s nullglob wiredx=1 +declare -a args # device that has an eth0, but we aren't using it because it is # broken. We could just hardcode a mac comparison with `cat @@ -74,120 +199,41 @@ eth_dev=eth$(( wiredx - 1 )) nm_con=$(nmcli device show $eth_dev | \ awk '$1 == "GENERAL.CONNECTION:" {out=$2; for(i=3;i<=NF;i++){out=out" "$i}; print out}' ||:) -if [[ ! $nm_con ]]; then +if [[ ! $nm_con || $nm_con == -- ]]; then nm_con="Wired connection $wiredx" fi - -con_exists=false -if nmcli con | grep -q "^$nm_con " &>/dev/null; then - con_exists=true +if ! nmcli con | grep -q "^$nm_con " &>/dev/null; then + # Note: we could support creation through a file or via + # nmcli, but right now I'm ok with just having plugged in a device once + # since this os was installed. + echo "error: no existing connection: $nm_con found in output of nmcli con" + exit 0 fi -declare -a args -if $off; then - - if ! $con_exists; then - echo "warning: no existing connection: $nm_con found in output of nmcli con" - exit 0 - fi - - 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}' ) - { - read -r ipv4_method - read -r ipv4_gateway - read -r ipv4_dns - read -r ipv4_addresses - read -r state - }<<<"$tmpstr" +if ! type -p dig &>/dev/null; then + apt-get install dig +fi - reup=false - if [[ $state == activated ]]; then - reup=true - fi +get-cur +## end common setup / detection ## - if [[ $ipv4_method != auto ]]; then - args+=(ipv4.method auto) - fi - if [[ $ipv4_addresses != -- ]]; then - args+=(-ipv4.addresses "$ipv4_addresses") - fi - if [[ $ipv4_dns != -- ]]; then - args+=(-ipv4.dns "$ipv4_dns") - fi - if [[ $ipv4_gateway != -- ]]; then - # undocumented in t11 man nmcli. guessed randomly - args+=(ipv4.gateway 0.0.0.0) - fi - if (( ${#args[@]} >= 1 )); then - m nmcli con mod "$nm_con" "${args[@]}" - if $reup; then - m nmcli con up "$nm_con" - fi - else - echo "$0: found expected state, nothing to do." - fi +if $off; then + set-dynamic exit 0 fi +detect-net +get-ip -if [[ $(dig +short @10.2.0.1 -x 10.2.0.2 2>&1 ||:) == kd.b8.nz. ]] \ - && ip n show 10.2.0.1 | grep . &>/dev/null; then - # we are at_home=true - - while read -r ip_suf host mac; do - if [[ ! $ip_suf || $ip_suf == \#* ]]; then - continue - fi - if [[ $mac != usb ]]; then - continue - fi - if [[ $host == ${HOSTNAME}c ]]; then - - ip=10.2.0.$ip_suf/16 - gateway=10.2.0.1 - dns=8.8.8.4,8.8.8.8 - break - fi - done

/dev/null; then - apt-get install dig - fi - myip=$(dig +short @192.168.0.25 $HOSTNAME.office.fsf.org) - if [[ ! $ip ]]; then - echo "$0: error: didnt detect home network and failed to get office ip" - exit 1 - fi - dns=192.168.0.10,192.168.0.25 - gateway=192.168.0.1 - ip=$myip/24 -fi - -if ! $force && $con_exists; then - 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}') - expected="manual -$gateway -$dns -$ip" - if [[ $current == "$expected" ]]; then - echo "$0: found expected state, nothing to do." - exit 0 - fi +if ! $force && [[ "$cur_method $cur_gateway $cur_dns $cur_ip" == "manual $gateway $dns $ip" ]]; then + echo "$0: found expected state, nothing to do." + exit 0 fi -m nmcli con mod 'Wired connection 1' ipv4.method manual ipv4.addresses $ip ipv4.gateway $gateway ipv4.dns $dns +set-nm -state=$(nmcli con show "$nm_con" 2>/dev/null | awk '$1 == "GENERAL.STATE:" {print $2}') -if [[ $state == activated ]]; then - m nmcli con up "$nm_con" -fi # example of down cli diff --git a/wrt-setup-local b/wrt-setup-local index 092e486..ac33e3a 100755 --- a/wrt-setup-local +++ b/wrt-setup-local @@ -791,7 +791,7 @@ config redirect option src wan option src_dport 80 option dest lan - option dest_ip $l.2 + option dest_ip $l.9 option proto tcp config rule option src wan @@ -804,7 +804,7 @@ config redirect option src wan option src_dport 443 option dest lan - option dest_ip $l.2 + option dest_ip $l.9 option proto tcp config rule option src wan -- 2.30.2