diff --git a/setup-network.sh b/setup-network.sh index b1ada1f..00d083f 100755 --- a/setup-network.sh +++ b/setup-network.sh @@ -8,12 +8,27 @@ CYAN='\033[0;36m' NC='\033[0m' info() { echo -e "${GREEN}[+]${NC} $*"; } +check_deps() { + local missing=() + for cmd in "$@"; do + if ! command -v "$cmd" >/dev/null 2>&1; then + missing+=("$cmd") + fi + done + if [ ${#missing[@]} -gt 0 ]; then + die "Missing required dependencies: ${missing[*]}" + fi +} + + warn() { echo -e "${YELLOW}[!]${NC} $*"; } die() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; } header() { echo -e "\n${CYAN}══════════════════════════════════════════${NC}"; echo -e "${CYAN} $*${NC}"; echo -e "${CYAN}══════════════════════════════════════════${NC}"; } [ "$(id -u)" -eq 0 ] || die "Run as root" +check_deps ip netplan systemctl + header "TinyBoard Network Setup" info "Available interfaces:" @@ -105,8 +120,9 @@ fi header "Writing Netplan Config" if [ -f "$NETPLAN_FILE" ]; then - cp "$NETPLAN_FILE" "${NETPLAN_FILE}.bak" - info "Backup saved to ${NETPLAN_FILE}.bak" + BACKUP_FILE="/root/$(basename "${NETPLAN_FILE}").bak" + cp "$NETPLAN_FILE" "$BACKUP_FILE" + info "Backup saved to $BACKUP_FILE" fi if $IS_WIFI; then @@ -148,12 +164,29 @@ fi info "Netplan config written to $NETPLAN_FILE" header "Applying Configuration" -warn "Testing netplan config..." -if netplan try --timeout 10 2>/dev/null; then - info "Netplan config applied successfully." -else - warn "netplan try timed out or failed — applying anyway..." +warn "Testing netplan config with 30 second rollback window..." +netplan try --timeout 30 & +NETPLAN_PID=$! + +STATIC_ADDR_TEST="${STATIC_IP%%/*}" +CONNECTED=false +for i in $(seq 1 6); do + sleep 5 + if ping -c 1 -W 2 "$GATEWAY" >/dev/null 2>&1; then + CONNECTED=true + break + fi + warn "Network check $i/6 failed, retrying..." +done + +if $CONNECTED; then + info "Network connectivity confirmed — applying config permanently." netplan apply +else + warn "No network connectivity detected after 30 seconds — reverting to previous config." + kill "$NETPLAN_PID" 2>/dev/null || true + wait "$NETPLAN_PID" 2>/dev/null || true + die "Config reverted. Check your settings and try again." fi STATIC_ADDR="${STATIC_IP%%/*}"