-
First download and install govc binary (Reference)
# extract govc binary to /usr/local/bin # note: the "tar" command must run with root permissions curl -L -o - "https://github.com/vmware/govmomi/releases/latest/download/govc_$(uname -s)_$(uname -m).tar.gz" | tar -C /usr/local/bin -xvzf - govc
-
put this script in your .bashrc/.zshrc, replace your credentials (Lines 6 to 16)
-
Use it
$ find_vm vcsa1 jira No VMs found matching 'jira' in datacenter 'vcsa' $ find_vm vcsa2 jira Matching VMs with IPs in datacenter 'vc': VM: /Hetzner/vm/10.11-infra-jira | IP: 172.16.10.11
Created
March 28, 2024 13:04
-
-
Save mrhalix/6b0592d7320170861820d1c0a910eeda to your computer and use it in GitHub Desktop.
VMware vSphere (VCSA) VM Finder in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find_vm() { | |
datacenter="$1" | |
query="$2" | |
# Set environment variables based on the datacenter | |
if [ "$datacenter" = "vcsa1" ]; then | |
export GOVC_URL="https://vsca1url/sdk" | |
export GOVC_USERNAME="john" | |
export GOVC_PASSWORD="PAASSWord" | |
export GOVC_INSECURE="true" | |
elif [ "$datacenter" = "vcsa2" ]; then | |
export GOVC_URL="https://vsca2url/sdk" | |
export GOVC_USERNAME="john" | |
export GOVC_PASSWORD="PAASSWord" | |
export GOVC_INSECURE="true" | |
else | |
echo "Unsupported datacenter: $datacenter" | |
return 1 | |
fi | |
# Run govc command to search for VMs and retrieve their IP addresses | |
vms=$(govc ls "*/vm" | grep -i "$query") | |
if [ -z "$vms" ]; then | |
echo "No VMs found matching '$query' in datacenter '$datacenter'" | |
else | |
echo "Matching VMs with IPs in datacenter '$datacenter':" | |
while read -r vm_path; do | |
# Extract IP address using govc command | |
ip_address=$(govc vm.ip "${vm_path}") | |
if [ -n "$ip_address" ]; then | |
echo "VM: $vm_path | IP: $ip_address" | |
else | |
echo "VM: $vm_path | IP: N/A" | |
fi | |
done <<< "$vms" | |
fi | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment