forked from finn/tinyboard
setup-network.sh: add menu with hostname change and static IP options, fix SSID extraction quoting using heredoc
This commit is contained in:
@@ -26,9 +26,40 @@ check_deps() {
|
|||||||
|
|
||||||
[ "$(id -u)" -eq 0 ] || die "Run as root"
|
[ "$(id -u)" -eq 0 ] || die "Run as root"
|
||||||
|
|
||||||
check_deps ip netplan systemctl ping
|
check_deps ip netplan systemctl ping hostnamectl
|
||||||
|
|
||||||
header "TinyBoard Network Setup"
|
header "TinyBoard Network Setup"
|
||||||
|
echo ""
|
||||||
|
echo " 0) Change hostname"
|
||||||
|
echo " 1) Configure static IP"
|
||||||
|
echo " q) Quit"
|
||||||
|
echo ""
|
||||||
|
read -rp "Choose: " NET_OPT
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
case "$NET_OPT" in
|
||||||
|
0)
|
||||||
|
header "Change Hostname"
|
||||||
|
CURRENT_HOSTNAME=$(hostname)
|
||||||
|
echo -e "Current hostname: ${YELLOW}$CURRENT_HOSTNAME${NC}"
|
||||||
|
read -rp "Enter new hostname (e.g. rocky): " NEW_HOSTNAME
|
||||||
|
[ -n "$NEW_HOSTNAME" ] || die "Hostname cannot be empty."
|
||||||
|
[[ "$NEW_HOSTNAME" =~ ^[a-zA-Z0-9._-]+$ ]] || die "Invalid hostname — use only letters, numbers, dots, underscores, hyphens."
|
||||||
|
hostnamectl set-hostname "$NEW_HOSTNAME"
|
||||||
|
echo "$NEW_HOSTNAME" > /etc/hostname
|
||||||
|
sed -i "s/${CURRENT_HOSTNAME}/${NEW_HOSTNAME}/g" /etc/hosts
|
||||||
|
info "Hostname changed to: $NEW_HOSTNAME"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
1)
|
||||||
|
;;
|
||||||
|
q|Q)
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
die "Invalid choice."
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
info "Available interfaces:"
|
info "Available interfaces:"
|
||||||
ip -o link show | awk -F': ' 'NR>1 {print " " $2}'
|
ip -o link show | awk -F': ' 'NR>1 {print " " $2}'
|
||||||
@@ -98,7 +129,13 @@ if $IS_WIFI; then
|
|||||||
header "WiFi Credentials"
|
header "WiFi Credentials"
|
||||||
CURRENT_SSID=""
|
CURRENT_SSID=""
|
||||||
if [ -f "$NETPLAN_FILE" ]; then
|
if [ -f "$NETPLAN_FILE" ]; then
|
||||||
CURRENT_SSID=$(grep -A1 'access-points:' "$NETPLAN_FILE" 2>/dev/null | tail -1 | tr -d ' "' | sed 's/:$//' || true)
|
CURRENT_SSID=$(python3 - "$NETPLAN_FILE" <<'PYEOF'
|
||||||
|
import sys, re
|
||||||
|
txt = open(sys.argv[1]).read()
|
||||||
|
m = re.search(r'access-points:\s*\n\s+["\']{0,1}([^"\':\n]+)["\']{0,1}:', txt)
|
||||||
|
print(m.group(1).strip() if m else "")
|
||||||
|
PYEOF
|
||||||
|
)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
KEEP_WIFI="n"
|
KEEP_WIFI="n"
|
||||||
|
|||||||
Reference in New Issue
Block a user