Files
proxmox/scripts/unifi/add-vlan11-secondary-ip.sh
defiQUG b3a8fe4496
Some checks failed
Deploy to Phoenix / deploy (push) Has been cancelled
chore: sync all changes to Gitea
- Config, docs, scripts, and backup manifests
- Submodule refs unchanged (m = modified content in submodules)

Made-with: Cursor
2026-03-02 11:37:34 -08:00

105 lines
3.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# Load IP configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "${PROJECT_ROOT}/config/ip-addresses.conf" 2>/dev/null || true
# Add VLAN 11 secondary IP address to current machine
# This allows the machine to have both its current IP and an IP on VLAN 11
set -e
# Configuration
VLAN11_IP="${IP_SERVICE_23:-${IP_SERVICE_23:-192.168.11.23}}"
VLAN11_NETMASK="24"
VLAN11_GATEWAY="${NETWORK_GATEWAY:-192.168.11.1}"
# Detect primary interface
PRIMARY_IF=$(ip route show | grep default | awk '{print $5}' | head -1)
if [ -z "$PRIMARY_IF" ]; then
echo "❌ Could not detect primary network interface"
exit 1
fi
CURRENT_IP=$(ip -4 addr show $PRIMARY_IF 2>/dev/null | grep -oP 'inet \K[\d.]+' | head -1)
CURRENT_GW=$(ip route show | grep default | awk '{print $3}' | head -1)
echo "🔧 Adding VLAN 11 Secondary IP Address"
echo ""
echo "📋 Configuration:"
echo " Primary Interface: $PRIMARY_IF"
echo " Current IP: $CURRENT_IP"
echo " Current Gateway: $CURRENT_GW"
echo " VLAN 11 IP: $VLAN11_IP/$VLAN11_NETMASK"
echo " VLAN 11 Gateway: $VLAN11_GATEWAY"
echo ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "❌ This script must be run with sudo"
echo " Usage: sudo $0"
exit 1
fi
# Check if VLAN 11 IP already exists
if ip addr show $PRIMARY_IF | grep -q "$VLAN11_IP"; then
echo "✅ VLAN 11 IP ($VLAN11_IP) already configured on $PRIMARY_IF"
exit 0
fi
# Add secondary IP address
echo " Adding secondary IP address $VLAN11_IP/$VLAN11_NETMASK to $PRIMARY_IF..."
ip addr add $VLAN11_IP/$VLAN11_NETMASK dev $PRIMARY_IF
if [ $? -eq 0 ]; then
echo " ✅ Secondary IP added successfully"
else
echo " ❌ Failed to add secondary IP"
exit 1
fi
# Add route to VLAN 11 network (if not already present)
if ! ip route show | grep -q "${NETWORK_192_168_11_0:-192.168.11.0}/24"; then
echo " Adding route to VLAN 11 network..."
ip route add ${NETWORK_192_168_11_0:-192.168.11.0}/24 dev $PRIMARY_IF src $VLAN11_IP
echo " ✅ Route added"
else
echo " ✅ Route to VLAN 11 already exists"
fi
# Test connectivity
echo ""
echo "🧪 Testing Connectivity..."
sleep 2
# Test VLAN 11 gateway
if ping -c 1 -W 2 $VLAN11_GATEWAY >/dev/null 2>&1; then
echo " ✅ VLAN 11 gateway ($VLAN11_GATEWAY) is reachable"
else
echo " ⚠️ VLAN 11 gateway ($VLAN11_GATEWAY) is not reachable"
echo " (This may be normal if gateway is not configured)"
fi
# Test Proxmox hosts
for host in "${PROXMOX_HOST_ML110:-192.168.11.10}:ml110" "${PROXMOX_HOST_R630_01:-192.168.11.11}:r630-01" "${PROXMOX_HOST_R630_02:-192.168.11.12}:r630-02"; do
IFS=':' read -r ip name <<< "$host"
if ping -c 1 -W 2 $ip >/dev/null 2>&1; then
echo "$name ($ip) is reachable"
else
echo " ⚠️ $name ($ip) is not reachable"
fi
done
echo ""
echo "✅ Configuration Complete!"
echo ""
echo "📋 Current IP Addresses:"
ip addr show $PRIMARY_IF | grep "inet " | sed 's/^/ /'
echo ""
echo "💡 Note: This configuration is temporary and will be lost on reboot."
echo " To make it persistent, configure netplan or network manager."
echo ""