Skip to content

How to Get Information From A Virtual Machine in Proxmox Using a Bash Script?

get information from virtual machine proxmox

I have many proxmox servers where each proxmox server has many virtual machines and I have to record information from each virtual machine manually. Before I used this bash script, I had to fill it in manually and it was very tiring and confusing because the virtual machine often changes either it changes its specifications such as memory and hard disk, or the state of the virtual machine changes such as it is dead or has been deleted. Therefore, I made a bash script to automatically record information from each virtual machine from the proxmox server.



How to get information from a virtual machine in proxmox using a bash script?



Basically, the bash script that I will make is a group of proxmox commands that I usually do to find information about virtual machines on the proxmox server such as qm config, qm list, and so on. The items I will get from the virtual machine using this bash script are as shown below:

The items that I want to get from virtual machines

Here is a bash script to get information from the virtual machine that is in the virtual machine:


### Get Data from Virtual machines

qm list | sed '1d'  > list.txt
qm list | awk '{print $1}' | sed '1d' > pid.txt
qm list  | awk '{print $1,$3}' > qm.txt

ip_address () {

mac=$(qm config "$1" | awk '/net0/ { print tolower($2) }' | sed -r 's/virtio=(.*),.*/\1/g')
ip=$(arp-scan --interface=vmbr0 | grep "$mac" | awk '{print $1}')

echo $ip

cat pid.txt | while read line
  qm config $line | grep name | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep ostype | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep memory | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep cores | cut -d ' ' -f2- >> $line.txt
  qm config $line | grep sockets | cut -d ' ' -f2- >> $line.txt
  ip --oneline addr show | awk '$3 == "inet" && $2 != "lo" { print $2 ": " $4 }' | awk '{print $2}' | sed '$ s/.$//' | sed '$ s/.$//' | sed '$ s/.$//' | head -n 1 >> $line.txt
  ip_address $line >> $line.txt

  ip=`sed -n '7p' $line.txt | cut -b 1-2`

  if [ -z "$ip" ]
  sed -i "7i VM is stopped or QEMU is not configured" $line.txt

  qm config $line | grep description | cut -d ' ' -f2- >> $line.txt
  note=`sed -n '8p' $line.txt`
#echo $note

  if [ -z "$note" ]
  sed -i "8i Please write the description" $line.txt
  sed -i '/^$/d' $line.txt

 second=`qm status $line -verbose | grep uptime | awk '{print $2}'`
  printf '%dd %dh:%dm:%ds\n' $((second/86400)) $((second%86400/3600)) $((second%3600/60)) $((second%60)) >> $line.txt

  sed -i 's/l26/Linux/g' $line.txt

#Get vmid
number=`cat pid.txt | wc -l`

while [ $i -lt $number ]
  i=`expr $i + 1`
  value=`cat pid.txt | sed -n "$i"'p'`
  echo $value >> ` cat pid.txt | sed -n "$i"'p'`.txt

#Get status

qm list | sed '1d' > status.txt

cat pid.txt | while read line
  cat qm.txt | grep $line | awk '{print $2}' >> $line.txt

# get HDD
cat list.txt | awk '{print $5}' > hdd.txt

number=`cat hdd.txt | wc -l`

while [ $i -lt $number ]; do
    i=`expr $i + 1`
    value=`cat hdd.txt | sed -n "$i"'p'`
    echo $value >> ` cat pid.txt | sed -n "$i"'p'`.txt

cat pid.txt | while read line

  sed -i "s/$/,/g" $line.txt

  sed -i 's/%0A//g' $line.txt
  sed -i 's/%3A//g' $line.txt

awk 'BEGIN { ORS = " " } { print }' $line.txt | sed 's/"[[:space:]]\+"/","/g'  > $line.csv

Look at line 12 in the bash script above. you have to change the IP with the IP used by your proxmox server. The script will generate two file formats namely a txt file and a CSV file for each virtual machine. In these 2 file formats, sometimes there are some empty results because there is no data on the virtual machine. For example, the ip_vm column will result in VM is stopped or QEMU is not configured if the script does not get the IP of the VM. To see the IP of a VM on proxmox, please look at this article. Likewise, if the script does not get the VM description, the note field will be empty. This is because in the VM the Notes column does not contain information as shown below:

The Notes on proxmox

This is an example of 2 files that are generated after we run this bash script:

Example of 2 files



If you have several proxmox servers then you can use the cron application to run this bash script periodically to retrieve data from the virtual machines on all your proxmox servers. And you can also insert the CSV file that you get after running this script to be inserted into the MySQL/MariaDB database.




Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *