Files
proxmox/scripts/review-r630-02-network-configs.sh

357 lines
12 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# Review all network configurations for VMs and containers on r630-02
# Usage: ./scripts/review-r630-02-network-configs.sh
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
PROXMOX_HOST="${PROXMOX_HOST_R630_02}"
PROXMOX_NODE="r630-02"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${CYAN}${NC} $1"
}
log_success() {
echo -e "${GREEN}${NC} $1"
}
log_warn() {
echo -e "${YELLOW}${NC} $1"
}
log_error() {
echo -e "${RED}${NC} $1"
}
log_section() {
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
}
# Function to parse network config string
parse_net_config() {
local config="$1"
# Extract key=value pairs and format output
if echo "$config" | grep -q "name="; then
echo " Interface: $(echo "$config" | grep -oE 'name=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -q "bridge="; then
echo " Bridge: $(echo "$config" | grep -oE 'bridge=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -q "ip="; then
echo " IP: $(echo "$config" | grep -oE 'ip=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -q "gw="; then
echo " Gateway: $(echo "$config" | grep -oE 'gw=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -qE "(hwaddr|macaddr)="; then
echo " MAC: $(echo "$config" | grep -oE '(hwaddr|macaddr)=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -q "tag="; then
echo " VLAN Tag: $(echo "$config" | grep -oE 'tag=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -q "type="; then
echo " Type: $(echo "$config" | grep -oE 'type=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -q "firewall="; then
echo " Firewall: $(echo "$config" | grep -oE 'firewall=[^,]+' | cut -d'=' -f2)"
fi
if echo "$config" | grep -q "model="; then
echo " Model: $(echo "$config" | grep -oE 'model=[^,]+' | cut -d'=' -f2)"
fi
}
# Function to get container network config
get_container_net_config() {
local vmid=$1
local config=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"pct config $vmid 2>/dev/null" || echo "")
if [ -z "$config" ]; then
echo "N/A"
return
fi
# Get all network interfaces (net0, net1, etc.)
local net_interfaces=$(echo "$config" | grep -E '^net[0-9]+:' | sed 's/^net[0-9]*://' | sed 's/^[[:space:]]*//')
if [ -z "$net_interfaces" ]; then
echo "No network configuration found"
return
fi
# Parse each network interface
echo "$net_interfaces" | while IFS= read -r net_config; do
if [ -n "$net_config" ]; then
parse_net_config "$net_config"
fi
done
}
# Function to get VM network config
get_vm_net_config() {
local vmid=$1
local config=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"qm config $vmid 2>/dev/null" || echo "")
if [ -z "$config" ]; then
echo "N/A"
return
fi
# Get all network interfaces (net0, net1, etc.)
local net_interfaces=$(echo "$config" | grep -E '^net[0-9]+:' | sed 's/^net[0-9]*://' | sed 's/^[[:space:]]*//')
if [ -z "$net_interfaces" ]; then
echo "No network configuration found"
return
fi
# Parse each network interface
echo "$net_interfaces" | while IFS= read -r net_config; do
if [ -n "$net_config" ]; then
parse_net_config "$net_config"
fi
done
# Also check for ipconfig entries (for cloud-init)
local ipconfigs=$(echo "$config" | grep -E '^ipconfig[0-9]+:' | sed 's/^ipconfig[0-9]*://' | sed 's/^[[:space:]]*//')
if [ -n "$ipconfigs" ]; then
echo "--- Cloud-init IP Config ---"
echo "$ipconfigs" | while IFS= read -r ipconfig; do
if [ -n "$ipconfig" ]; then
parse_net_config "$ipconfig"
fi
done
fi
}
# Function to get actual IP from running container/VM
get_actual_ip() {
local vmid=$1
local type=$2 # "lxc" or "qemu"
if [ "$type" = "lxc" ]; then
ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"pct exec $vmid -- ip addr show eth0 2>/dev/null | grep 'inet ' | grep -v '127.0.0.1' | awk '{print \$2}' | cut -d'/' -f1" 2>/dev/null || echo ""
else
# For VMs, try guest agent
ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"qm guest cmd $vmid network-get-interfaces 2>/dev/null | grep -oE '\"ip-address\":\"[0-9.]+' | grep -v '127.0.0.1' | cut -d'\"' -f4 | head -1" 2>/dev/null || echo ""
fi
}
# Main execution
log_section "Network Configuration Review for r630-02 ($PROXMOX_HOST)"
# Test connectivity
log_info "Testing connectivity to $PROXMOX_HOST..."
if ! ssh -o StrictHostKeyChecking=no -o ConnectTimeout=5 root@"$PROXMOX_HOST" "echo 'Connected'" >/dev/null 2>&1; then
log_error "Cannot connect to $PROXMOX_HOST"
exit 1
fi
log_success "Connected to $PROXMOX_HOST"
echo ""
# Get host network info
log_section "Host Network Configuration"
log_info "Host Bridge Configuration:"
BRIDGE_INFO=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"ip addr show vmbr0 2>/dev/null | grep -E 'inet |state' || echo 'N/A'")
echo "$BRIDGE_INFO" | sed 's/^/ /'
echo ""
log_info "Host Routing Table:"
ROUTES=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"ip route show | grep -E 'default|192.168.11' | head -5" || echo "N/A")
echo "$ROUTES" | sed 's/^/ /'
echo ""
# LXC Containers
log_section "LXC Container Network Configurations"
# Get list of container VMIDs
CONTAINER_VMIDS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"pct list 2>/dev/null | tail -n +2 | awk '{print \$1}'" || echo "")
if [ -z "$CONTAINER_VMIDS" ]; then
log_warn "No LXC containers found"
else
for vmid in $CONTAINER_VMIDS; do
if [ -z "$vmid" ] || [ "$vmid" = "VMID" ]; then
continue
fi
# Get container info
CONTAINER_INFO=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"pct list 2>/dev/null | grep \"^$vmid\" || echo \"\"")
if [ -z "$CONTAINER_INFO" ]; then
continue
fi
status=$(echo "$CONTAINER_INFO" | awk '{print $2}')
name=$(echo "$CONTAINER_INFO" | awk '{print $3}')
echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${CYAN}Container: $vmid - $name${NC}"
echo -e "${CYAN}Status: $status${NC}"
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
# Get network configuration
log_info "Network Configuration (from config):"
NET_CONFIG=$(get_container_net_config "$vmid")
if [ "$NET_CONFIG" != "N/A" ] && [ "$NET_CONFIG" != "No network configuration found" ]; then
echo "$NET_CONFIG"
else
echo " $NET_CONFIG"
fi
echo ""
# Get actual IP if running
if [ "$status" = "running" ]; then
log_info "Actual IP Address (from running container):"
ACTUAL_IP=$(get_actual_ip "$vmid" "lxc")
if [ -n "$ACTUAL_IP" ]; then
echo " IP: $ACTUAL_IP"
else
echo " (Could not determine - container may not have network configured)"
fi
echo ""
fi
# Get hostname
HOSTNAME=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"pct config $vmid 2>/dev/null | grep '^hostname:' | awk '{print \$2}'" 2>/dev/null || echo "N/A")
if [ "$HOSTNAME" != "N/A" ]; then
log_info "Hostname: $HOSTNAME"
fi
done
fi
echo ""
# QEMU/KVM VMs
log_section "QEMU/KVM VM Network Configurations"
# Get list of VM VMIDs
VM_VMIDS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"qm list 2>/dev/null | tail -n +2 | awk '{print \$1}'" || echo "")
if [ -z "$VM_VMIDS" ]; then
log_warn "No QEMU/KVM VMs found"
else
for vmid in $VM_VMIDS; do
if [ -z "$vmid" ] || [ "$vmid" = "VMID" ]; then
continue
fi
# Get VM info
VM_INFO=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"qm list 2>/dev/null | grep \"^$vmid\" || echo \"\"")
if [ -z "$VM_INFO" ]; then
continue
fi
status=$(echo "$VM_INFO" | awk '{print $2}')
name=$(echo "$VM_INFO" | awk '{print $3}')
echo ""
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${CYAN}VM: $vmid - $name${NC}"
echo -e "${CYAN}Status: $status${NC}"
echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
# Get network configuration
log_info "Network Configuration (from config):"
NET_CONFIG=$(get_vm_net_config "$vmid")
if [ "$NET_CONFIG" != "N/A" ] && [ "$NET_CONFIG" != "No network configuration found" ]; then
echo "$NET_CONFIG"
else
echo " $NET_CONFIG"
fi
echo ""
# Get actual IP if running
if [ "$status" = "running" ]; then
log_info "Actual IP Address (from guest agent):"
ACTUAL_IP=$(get_actual_ip "$vmid" "qemu")
if [ -n "$ACTUAL_IP" ]; then
echo " IP: $ACTUAL_IP"
else
echo " (Could not determine - guest agent may not be available)"
fi
echo ""
fi
# Get hostname
HOSTNAME=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"qm config $vmid 2>/dev/null | grep '^name:' | awk '{print \$2}'" 2>/dev/null || echo "N/A")
if [ "$HOSTNAME" != "N/A" ]; then
log_info "Name: $HOSTNAME"
fi
done
fi
echo ""
# Summary
log_section "Summary"
CONTAINER_COUNT=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"pct list 2>/dev/null | tail -n +2 | wc -l" || echo "0")
RUNNING_CONTAINERS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"pct list 2>/dev/null | grep running | wc -l" || echo "0")
VM_COUNT=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"qm list 2>/dev/null | tail -n +2 | wc -l" || echo "0")
RUNNING_VMS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
"qm list 2>/dev/null | grep running | wc -l" || echo "0")
echo "LXC Containers: $CONTAINER_COUNT (Running: $RUNNING_CONTAINERS)"
echo "QEMU/KVM VMs: $VM_COUNT (Running: $RUNNING_VMS)"
echo ""
# Network summary
log_info "Network Summary:"
ALL_IPS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "
for vmid in \$(pct list 2>/dev/null | tail -n +2 | awk '{print \$1}'); do
pct config \$vmid 2>/dev/null | grep -oE 'ip=[0-9.]+/[0-9]+' | cut -d'=' -f2 | cut -d'/' -f1
done
for vmid in \$(qm list 2>/dev/null | tail -n +2 | awk '{print \$1}'); do
qm config \$vmid 2>/dev/null | grep -oE 'ip=[0-9.]+/[0-9]+' | cut -d'=' -f2 | cut -d'/' -f1
done
" 2>/dev/null | sort -u)
if [ -n "$ALL_IPS" ]; then
echo "Configured IP Addresses:"
echo "$ALL_IPS" | sed 's/^/ /'
else
echo " (No static IPs configured)"
fi
echo ""
log_success "Network configuration review complete!"