Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marshyon/f67c2a7c424bae33a1d1fd9324369c1c to your computer and use it in GitHub Desktop.
Save marshyon/f67c2a7c424bae33a1d1fd9324369c1c to your computer and use it in GitHub Desktop.
proxmox list ips of running vms

Description

A script that on a proxmox node, will list the ip addresses of running virtual machines.

Pre-requisites

install required packages :

apt-get install net-tools arp-scan

Running

make the above script executable

chmod +x get_ips_of_vms.sh

run the script

./get_ips_of_vms.sh

resulting in something like :

# ./get_ips_of_vms.sh
192.168.22.131 k8s-master 200-k8s-master
192.168.22.132 k8s-worker01 201-k8s-worker01
192.168.22.133 k8s-worker02 202-k8s-worker02

#!/usr/bin/env bash
network="192.168.22.0/24"
qmids=$(qm list | grep running | awk '{print $1}')
for id in $qmids
do
name=$(qm list | grep -v stopped | grep $id | awk '{print $2}')
mac=$(qm config $id | egrep "^net0" | awk '/net0/ { print tolower($2) }' | sed "s/virtio=//" | cut -f1 -d',')
arp-scan --interface=vmbr0 $network | grep "$mac" | awk '{printf "%s ",$1}'
echo "$name $id-$name"
done
@dscaravaggi
Copy link

thanks a lot,
in your script network CIDR is hardcoded and arp-scanning is called for every qemu guest,
(multiple arp scanning sometimes hits EDR and intrusion SOC teams)

I added some variables

#!/usr/bin/env bash

def_interface=$(ip route get 1.1.1.1 | head -1 | cut -d ' ' -f 5)
network=$(ip  addr show $def_interface | fgrep 'inet ' | awk '//  {print $2}')

qmids=$(qm list | grep running | awk '{print $1}')

TMPFILE1=$(mktemp)
arp-scan --interface=$def_interface $network 1> $TMPFILE1 2>/dev/null

for id in $qmids
do
          name=$(qm list | grep -v stopped | grep $id | awk '{print $2}')
        mac=$(qm config $id | egrep "^net0" | awk '/net0/ { print tolower($2) }' | sed "s/virtio=//" | cut -f1 -d',')
        #arp-scan --interface=$def_interface $network 2>/dev/null | grep "$mac" | awk '{printf "%s ",$1}'
        cat $TMPFILE1 | fgrep "$mac" | awk '{printf "%s ",$1}'
        echo "$name $id $name"
done

/bin/rm -f $TMPFILE1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment