Skip to content

Instantly share code, notes, and snippets.

@christian-korneck
Last active August 24, 2024 03:02
Show Gist options
  • Save christian-korneck/0c7bca93a6fb9eb9e143cdd05d923a85 to your computer and use it in GitHub Desktop.
Save christian-korneck/0c7bca93a6fb9eb9e143cdd05d923a85 to your computer and use it in GitHub Desktop.
Proxmox vmlookup - nslookup but for Proxmox VMs - Proxmox get VM IP

vmlookup - get IP(s) for Proxmox VM name

bash alias for a vmlookup command (nslookup but for Proxmox). Uses qm and jq.

install

put in .bashrc:

function vmlookup () {
    if [ "${1}" == "" ]; then echo -e "Get Proxmox VM ipv4 IP(s). \nUsage: vmlookup [vm name]" && return 1; fi
    local vmid=$(qm list | egrep -i "\s${1}\s" | awk '{print $1}')
    if [ "$vmid" == "" ]; then echo -e "[ERR] can't get VM ID" && return 1; fi
    # exclude link local loopback, link local, docker standard subnet, carrier grade nat (tailscale, etc)
    qm guest cmd $vmid network-get-interfaces | jq -r '.[] | .["ip-addresses"][] | select(.["ip-address-type"] == "ipv4" and (.["ip-address"] | tostring | test("^(127\\.0\\.0\\.1|169\\.254\\.|172\\.(1[6-9]|2[0-9]|3[0-1])\\.|100\\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\\.)") | not)) | ."ip-address"'
    return $?

usage:

$ vmlookup
Get Proxmox VM ipv4 IP(s).
Usage: vmlookup [vm name]

get VM IP from VM name:

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