#!/bin/bash

# Fix VMID 6000 Network Interface
# Issue: eth0 is DOWN

set -euo pipefail

echo "=== Fixing VMID 6000 Network Interface ==="
echo ""

# Bring interface up
echo "=== Bringing eth0 UP ==="
ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@192.168.11.10 \
  "ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@r630-01 'pct exec 6000 -- ip link set eth0 up 2>&1'"

echo ""
echo "=== Verifying Interface Status ==="
ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@192.168.11.10 \
  "ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@r630-01 'pct exec 6000 -- ip link show eth0 2>&1'"

echo ""
echo "=== Checking IP Assignment ==="
ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@192.168.11.10 \
  "ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@r630-01 'pct exec 6000 -- ip addr show eth0 2>&1'"

echo ""
echo "=== Testing Gateway Connectivity ==="
ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@192.168.11.10 \
  "ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@r630-01 'pct exec 6000 -- ping -c 3 -W 1 192.168.11.1 2>&1'"

echo ""
echo "=== Testing Internet Connectivity ==="
ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@192.168.11.10 \
  "ssh -o StrictHostKeyChecking=accept-new -o ConnectTimeout=5 root@r630-01 'pct exec 6000 -- ping -c 2 -W 1 8.8.8.8 2>&1 || echo \"Internet unreachable\"'"

echo ""
echo "=== Fix Complete ==="
