Collecting MAC and IP addresses from VMs

dpearceFL

Member
Jun 1, 2020
75
3
13
65
I'm trying to create a script that will output the MAC and IP addresses from the VMs on this host.

"qm guest cmd 1017 network-get-interfaces | jq ." will generate a valid JSON file. But when I try to address the element "hardware-address" all I get is errors like this:
Bash:
# qm guest cmd 1017 network-get-interfaces | jq . | jq .network-interfaces
jq: error: interfaces/0 is not defined at <top-level>, line 1:
.network-interfaces         
jq: 1 compile error

Yes I know this is more of a JSON or jq problem, but I am hoping someone has tried to gather this info before. If there a better way?
 
First, there is no field called "network-interfaces", at least in my output.

Second you need to quote the dash:
Code:
qm guest cmd 3004 network-get-interfaces|jq '.[].hardware-address'
jq: error: address/0 is not defined at <top-level>, line 1:
.[].hardware-address             
jq: 1 compile error
Unable to flush stdout: Broken pipe
vs
Code:
qm guest cmd 3004 network-get-interfaces|jq '.[]."hardware-address"'
"00:00:00:00:00:00"
"bc:24:11:6a:61:1e"
"02:42:55:a6:c7:4b"
"aa:ce:4b:73:02:40"


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
Yes, the element I am trying to look at is called "hardware-address".

I have so much to learn about JSON and addressing. Please tolerate on more question...

So given this:
Code:
# qm guest cmd 1017 network-get-interfaces | jq  '.[1] | ."ip-addresses"'
[
  {
    "ip-address": "10.1.0.76",
    "ip-address-type": "ipv4",
    "prefix": 23
  },
  {
    "ip-address": "fe80::be24:11ff:fe69:8d37",
    "ip-address-type": "ipv6",
    "prefix": 64
  }
]

How do I get at the value for "ip-address"?
 
Is this what you were looking for?

Code:
qm guest cmd 3004 network-get-interfaces|jq -r '.[]|."hardware-address" +" " + (."ip-addresses"[]|select(."ip-address-type"=="ipv4")|."ip-address")'
00:00:00:00:00:00 127.0.0.1
bc:24:11:6a:61:1e 172.16.202.54
02:42:55:a6:c7:4b 172.17.0.1


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
Found it!
Code:
 qm guest cmd 1017 network-get-interfaces | jq  '.[1] | ."ip-addresses" | .[0] | ."ip-address"'
"10.1.0.76"
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!