Python code mostly for the OGL and VSL Course
'''
Patrik Martinsson 2025
Script to check if QEMU guest agent is active on VMs in Proxmox
you need to install the following packages:
pip install proxmoxer
pip install requests
pip install paramiko
pip install openssh_wrapper
'''
from proxmoxer import ProxmoxAPI
# Connect to Proxmox node
proxmox = ProxmoxAPI(
"xxx.xxx.xxx.xxx", # IP address of Proxmox node
user="CHANGEME@pam", # Username for Proxmox node
password="CHANGEME", # Password for Proxmox node
verify_ssl=False # set to True if using SSL with valid certificate
)
node = "groupXX-srvXX" # Name of your Proxmox node
# Get list of VMs on the node
vms = proxmox.nodes(node).qemu.get()
for vm in vms:
vmid = vm["vmid"]
name = vm.get("name", f"vm-{vmid}")
try:
# Check if the agent is responding
proxmox.nodes(node).qemu(vmid).agent("ping").post()
# Get IP-info
net_info = proxmox.nodes(node).qemu(vmid).agent("network-get-interfaces").get()
ips = []
for iface in net_info.get("result", []):
for ip in iface.get("ip-addresses", []):
# Filter for link-local fe80::, ::1 and loopback
addr = ip.get("ip-address")
if not addr.startswith("fe80") and addr !="::1" and addr != "127.0.0.1":
ips.append(addr)
ip_list = ", ".join(ips) if ips else "No IP"
print(f"{vmid} ({name}) -> Guest agent active, IP: {ip_list}") # Print VM name, ID and IPs
except Exception as e:
print(f"{vmid} ({name}) -> no guest agent: {e}") # Print VM ID, Name and error message if agent is not active
# Patrik Martinsson 2025
# Simple script to ping an IP range
import subprocess
import platform
def ping_host(ip):
# Ping a single host. Returns True if reachable, False otherwise.
# Choose the right param: -n for Windows, -c for Unix
param = "-n" if platform.system().lower() == "windows" else "-c"
command = ["ping", param, "1", ip]
try:
result = subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return result.returncode == 0
except Exception:
return False
def scan_range(base_ip, start, end):
# Scan a given IP range and print which hosts respond.
for i in range(start, end + 1):
ip = f"{base_ip}.{i}"
if ping_host(ip):
print(f"{ip} is alive")
else:
print(f"{ip} is unreachable")
if __name__ == "__main__":
# Example usage: 192.168.20.101–109
base_ip = "192.168.20" # change base IP if you need
scan_range(base_ip, 101, 109) # Change to what range you want to ping ( 4th octet )
from proxmoxer import ProxmoxAPI
from requests import *
proxmox = ProxmoxAPI('192.168.20.222', user='root@pam', password='Network!337', verify_ssl=False)
nodes = proxmox.nodes.get()
nodename = nodes[0]['node']
print("Node= ", nodename)
vmlist = proxmox.nodes(nodename).qemu.get()
for vm in vmlist:
print("====================================")
print(f"ID: {vm['vmid']}, Name: {vm['name']}, Status: {vm['status']}")
# Untested code below !!!!!!!!!!!!!!!!!!!
# Get VM configuration
vm_config = proxmox.nodes(nodename).qemu(vm['vmid']).config.get()
print("vm config=", vm_config)
# Check if the CD-ROM is mounted
if 'ide2' in vm_config:
ide2_config = vm_config['ide2']
else:
ide2_config = "FALSE"
print("ide2_config=", ide2_config)
if 'cdrom' in ide2_config and 'size' in ide2_config:
print(f"CD is mounted: ==== {vm_config['ide2']} ====")
else:
print("No CD is mounted")
# else:
# print("No CD-ROM device found")
# More untested code !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Check guest agent status and request version
try:
# guest_version = proxmox.nodes(nodename).qemu(vm).guest.agent.get('version')
guest_version = proxmox.nodes(nodename).qemu(vm['vmid']).get('status')
print("QEMU Guest Agent Version:", guest_version)
except Exception as e:
print("ERROR: Could not retrieve guest agent version:", e)