Files
bee/iso/overlay/usr/local/bin/netconf
Michael Chus 2d424c63cb fix(netconf): accept interface number as input, not just name
User sees a numbered list but could only type the name.
Now numeric input is resolved to the interface name via awk NR==N.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-28 07:27:49 +03:00

62 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
# Quick network configurator for the local console.
set -e
# List interfaces (exclude lo)
IFACES=$(ip -o link show | awk -F': ' '$2 != "lo" {print $2}' | cut -d@ -f1)
echo "Interfaces:"
i=1
for iface in $IFACES; do
ip=$(ip -4 addr show "$iface" 2>/dev/null | awk '/inet /{print $2}' | head -1)
echo " $i) $iface ${ip:-no IP}"
i=$((i+1))
done
echo ""
printf "Interface number or name [Enter = first]: "
read INPUT
if [ -z "$INPUT" ]; then
IFACE=$(echo "$IFACES" | head -1)
elif echo "$INPUT" | grep -qE '^[0-9]+$'; then
# Numeric input — resolve to name
IFACE=$(echo "$IFACES" | awk "NR==$INPUT")
if [ -z "$IFACE" ]; then
echo "Error: no interface with number $INPUT"
exit 1
fi
else
IFACE="$INPUT"
fi
echo "Selected: $IFACE"
echo ""
echo " 1) DHCP"
echo " 2) Static"
printf "Mode [1]: "
read MODE
MODE=${MODE:-1}
if [ "$MODE" = "1" ]; then
echo "Running DHCP on $IFACE..."
dhclient -v "$IFACE"
else
printf "IP address (e.g. 192.168.1.100/24): "
read ADDR
printf "Gateway (e.g. 192.168.1.1): "
read GW
printf "DNS [8.8.8.8]: "
read DNS
DNS=${DNS:-8.8.8.8}
ip addr flush dev "$IFACE"
ip addr add "$ADDR" dev "$IFACE"
ip link set "$IFACE" up
ip route add default via "$GW"
echo "nameserver $DNS" > /etc/resolv.conf
echo "Done."
fi
echo ""
ip -4 addr show "$IFACE"