From 7ad39e6e28ccd4c4f8b2468d9e0ac469a4c96a64 Mon Sep 17 00:00:00 2001 From: Olivier Grand Date: Fri, 25 Oct 2024 19:28:41 +0200 Subject: [PATCH] fix vm-info with multi ip on same cluster --- tools/scripts/get-vm-info.sh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tools/scripts/get-vm-info.sh b/tools/scripts/get-vm-info.sh index f91c7a4..7b8f509 100755 --- a/tools/scripts/get-vm-info.sh +++ b/tools/scripts/get-vm-info.sh @@ -30,10 +30,18 @@ while [ ${nbParameters} -gt 0 ] ; do if [ "${vm_ip}" = "" ] ; then usage else - vm_name="$(govc vm.info -vm.ip=${vm_ip} | grep "Name:" | sed -e "s+^Name: *++g")" - if [ "${vm_name}" = "" ] ; then + vm_ids="$(govc object.collect -json -type m / guest | jq -r '.|select(.changeSet[].val.ipAddress == "'$vm_ip'") | [.obj.type, .obj.value] | join(":")')" + for vm_id in ${vm_ids} ; do + result="$(echo "${vm_id}" | xargs govc ls -L | xargs -I {} -n 1 echo "{}" | grep "${GOVC_RESOURCE_POOL}")" + if [ "${result}" != "" ] ; then + vm_ipath="${result}" + fi + done + + if [ "${vm_ipath}" = "" ] ; then printf "\n%bERROR : No existing vm with ip \"${vm_ip}\".%b\n" "${RED}" "${STD}" ; exit 1 fi + vm_name="$(govc vm.info -vm.ipath="${vm_ipath}" | grep "Name:" | awk '{print $2}')" fi ;; "-d"|"--disk-id") vm_disk_id="$2" ; shift ; shift ; nbParameters=$# @@ -54,7 +62,7 @@ while [ ${nbParameters} -gt 0 ] ; do usage else vm_macaddress="$(echo "${vm_macaddress}" | tr [:upper:] [:lower:])" - vm_id="$(govc object.collect -json -type m / config.hardware.device | jq -r '. | select(.changeSet[].val.virtualDevice[].macAddress == "'$vm_macaddress'") | [.obj.type, .obj.value] | join(":")')" + vm_id="$(govc object.collect -json -type m / guest | jq -r '.|select(.changeSet[].val.net[]?.macAddress == "'$vm_macaddress'") | [.obj.type, .obj.value] | join(":")')" if [ "${vm_id}" = "" ] ; then printf "\n%bERROR : No existing vm with mac address \"${vm_macaddress}\".%b\n" "${RED}" "${STD}" ; exit 1 fi