Skip to content

Instantly share code, notes, and snippets.

@pschmitt
Created July 15, 2017 17:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pschmitt/b45490e2304b9302ea767ec0340b5705 to your computer and use it in GitHub Desktop.
Save pschmitt/b45490e2304b9302ea767ec0340b5705 to your computer and use it in GitHub Desktop.
Get the IP address of a VM hosted on Proxmox
#!/usr/bin/env bash
usage() {
echo "$(basename $0) VM"
}
get_vm_id() {
qm list | awk '/'"${1}"'/ { print $1 }'
}
get_vm_mac_addr() {
qm config "$1" | awk '/net0/ { print tolower($2) }' | \
sed -r 's/virtio=(.*),.*/\1/g'
}
get_ip_from_mac_arp() {
arp -an | awk '/'"$1"'/ { print $2 }' | sed 's/(\(.*\))/\1/g'
}
if [[ -z "$1" ]]
then
usage
exit 2
fi
case "$1" in
-h|--help|help)
usage
exit 0
esac
VM_ID=$(get_vm_id $1)
if [[ -z "$VM_ID" ]]
then
echo "Could not determine VM ID for $1"
exit 3
fi
VM_MAC_ADDR=$(get_vm_mac_addr "$VM_ID")
if [[ -z "$VM_MAC_ADDR" ]]
then
echo "Could not determine VM MAC address for $1 ($VM_ID)"
exit 3
fi
get_ip_from_mac_arp "$VM_MAC_ADDR"
# vim: set et ts=2 sw=2 ft=bash :
@Glitch3dPenguin
Copy link

It would be amazing if you could update for the newer versions of Poxmox!
Thanks!

@Mokarski
Copy link

It would be amazing if you could update for the newer versions of Poxmox!
Thanks!

MAC=$(qm config $1 | grep --color=always "net" | cut -f 2 -d "=" | cut -f 1 -d ",")
echo Found VM_MAC: $MAC
MAC1=$(echo $MAC | cut -f 1 -d ":")
MAC2=$(echo $MAC | cut -f 2 -d ":")
MAC3=$(echo $MAC | cut -f 3 -d ":")
MAC4=$(echo $MAC | cut -f 4 -d ":")
MAC5=$(echo $MAC | cut -f 5 -d ":")
MAC6=$(echo $MAC | cut -f 6 -d ":")
MAC_ADDR=$MAC1:$MAC2:$MAC3:$MAC4:$MAC5:$MAC6
arp -an | grep -i --color=always "$MAC_ADDR"

@Houssem12-ai
Copy link

@Mokarski i would be so grateful if you give me some hints or clarification that could help me understand your script
i am still beginners and i am looking to list all vm ip addresses in my proxmox VE
thanks a lot

@Houssem12-ai
Copy link

Houssem12-ai commented Dec 22, 2021

@Mokarski i get an error when running this script but it works now after replacing the $ sign with the vmid
is there something that is deprecated or i have to add ??

@Archerkong
Copy link

that is greatful,but how can i get ip from vm used lxc

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