get_ip_addresses.sh
- This is a read-only script
#!/bin/bash
# This script scans Proxmox VE for running VMs and LXC containers
# and prints their IP addresses and hostnames to the console.
# --- Configuration ---
# --- Print Header ---
echo "Scanning for PVE Guest IPs..."
printf "%-20s %-30s %-10s\n" "IP ADDRESS" "HOSTNAME" "GUEST ID"
echo "-----------------------------------------------------------------"
# --- Process KVM Virtual Machines ---
for VMID in $(qm list | awk 'NR>1 {print $1}'); do
# Check if the VM is running
STATUS=$(qm status "$VMID" | awk '{print $2}')
if [ "$STATUS" != "running" ]; then
continue
fi
VM_NAME=$(qm config "$VMID" | grep 'name:' | awk '{print $2}')
# Get IP address, ignoring the 127.0.0.1 loopback address
IP_ADDRESS=$(qm guest cmd "$VMID" network-get-interfaces 2>/dev/null | grep '"ip-address"' | grep -v '127.0.0.1' | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | head -n 1)
if [ -n "$IP_ADDRESS" ]; then
printf "%-20s %-30s %-10s\n" "$IP_ADDRESS" "$VM_NAME" "$VMID (VM)"
fi
done
# --- Process LXC Containers ---
for CTID in $(pct list | awk 'NR>1 {print $1}'); do
# Check if the container is running
STATUS=$(pct status "$CTID" | awk '{print $2}')
if [ "$STATUS" != "running" ]; then
continue
fi
CT_NAME=$(pct config "$CTID" | grep 'hostname:' | awk '{print $2}')
# Get IP address, ignoring the 127.0.0.1 loopback address
IP_ADDRESS=$(pct exec "$CTID" ip a | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1 | head -n 1)
if [ -n "$IP_ADDRESS" ]; then
printf "%-20s %-30s %-10s\n" "$IP_ADDRESS" "$CT_NAME" "$CTID (LXC)"
else
# If no IP, check if it's because it's waiting for DHCP
if pct config "$CTID" | grep -q "ip=dhcp"; then
echo "-> INFO: LXC $CTID ($CT_NAME) is waiting for a DHCP lease."
fi
fi
done
echo "-----------------------------------------------------------------"
echo "Scan complete."