Skip to content

Instantly share code, notes, and snippets.

@huksley
Last active August 4, 2024 22:44
Show Gist options
  • Save huksley/d36b9a56386ec0a79a333ef74c047be8 to your computer and use it in GitHub Desktop.
Save huksley/d36b9a56386ec0a79a333ef74c047be8 to your computer and use it in GitHub Desktop.
Get list of all virtual machines on proxmox 4+, both LXC and QEMU and detect IP addresses
#!/bin/bash
# proxvm
# Output in specifed format (default csv) all virtual machines on proxmox 4+
SERVER=localhost
USERNAME=apiread@pve
PASSWORD=123456
FORMAT=csv
while [[ $# > 0 ]]; do
key="$1"
case $key in
-p)
PASSWORD=$2
shift
;;
-u)
USERNAME=$2
shift
;;
-h)
SERVER=$2
shift
;;
-f)
FORMAT=$2
shift
;;
-h|--help|-?)
echo "Usage:"
echo "$0 [options]"
echo " -h <server> Server to connect to, default $SERVER"
echo " -u <username> Username, default $USERNAME"
echo " -p <password> Password, default $PASSWORD"
echo " -f <format> Output format (csv, json), default $FORMAT"
exit 0
;;
*)
# unknown option
echo "Error: unknown option $1"
exit 1
;;
esac
shift # past argument or value
done
#echo Connecting to $SERVER:8006 as $USERNAME:$PASSWORD
TOKEN=`curl -s -k -d "username=$USERNAME&password=$PASSWORD" https://$SERVER:8006/api2/json/access/ticket | jq -r .data.ticket`
if [ "${PIPESTATUS[0]}" != "0" ]; then
echo Auth failed
exit 1
fi
function csv() {
echo $VMID,$NODE,$1,$NAME,$HWADDR,$IP
}
function start_csv() {
echo vmid,node,type,name,mac,ip
}
function end_csv() {
echo -n
}
function json() {
if [ "$2" != "0" ]; then
echo ","
fi
echo "{"
echo "\"vmid\": \"$VMID\","
echo "\"node\": \"$NODE\","
echo "\"type\": \"$1\","
echo "\"name\": \"$NAME\","
echo "\"mac\": \"$HWADDR\","
echo "\"ip\": \"$IP\""
echo -n "}"
}
function start_json() {
echo "["
}
function end_json() {
echo "]"
}
start_$FORMAT
POS=0
NODES=`curl -s -k https://$SERVER:8006/api2/json/nodes -b "PVEAuthCookie=$TOKEN" | jq -r '.data[].node'`
for NODE in `echo $NODES`; do
curl -s -k https://$SERVER:8006/api2/json/nodes/$NODE/lxc -b "PVEAuthCookie=$TOKEN" > /tmp/proxvm-lxc.json
curl -s -k https://$SERVER:8006/api2/json/nodes/$NODE/qemu -b "PVEAuthCookie=$TOKEN" > /tmp/proxvm-qemu.json
for VMID in `cat /tmp/proxvm-lxc.json | jq -r '.data[].vmid'`; do
curl -s -k https://$SERVER:8006/api2/json/nodes/$NODE/lxc/$VMID/config -b "PVEAuthCookie=$TOKEN" > /tmp/proxvm-$VMID.json
JSON=`cat /tmp/proxvm-lxc.json | jq -r ".data[] | select(.vmid | tonumber | contains($VMID))"`
NAME=`echo $JSON | jq -r .name`
NET=`cat /tmp/proxvm-$VMID.json | jq -r .data.net0`
HWADDR=`echo $NET | sed -re "s/.*hwaddr=([a-zA-Z0-9:]+),[a-zA-Z0-9]+=.*/\1/g"`
IP=`arp -a | grep $HWADDR | sed -re "s/.*\(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*/\1/g"`
$FORMAT lxc $POS
POS=`expr $POS + 1`
done
for VMID in `cat /tmp/proxvm-qemu.json | jq -r '.data[].vmid'`; do
curl -s -k https://$SERVER:8006/api2/json/nodes/$NODE/qemu/$VMID/config -b "PVEAuthCookie=$TOKEN" > /tmp/proxvm-$VMID.json
JSON=`cat /tmp/proxvm-qemu.json | jq -r ".data[] | select(.vmid | tonumber | contains($VMID))"`
NAME=`echo $JSON | jq -r .name`
NET=`cat /tmp/proxvm-$VMID.json | jq -r .data.net0`
HWADDR=`echo $NET | sed -re "s/[a-zA-Z0-9]+=([a-zA-Z0-9:]+),.*/\1/g"`
IP=`arp -a | grep $HWADDR | sed -re "s/.*\(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*/\1/g"`
$FORMAT qemu $POS
POS=`expr $POS + 1`
done
done
end_$FORMAT
rm /tmp/proxvm-*.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment