VMware: Using the govc CLI to automate vCenter commands

Share At:

Learn How to Install, Configure and Test GOVC? - MSys Technologies

The vSphere web GUI is a nice visual tool, but if you need to retrieve vCenter information in bulk or perform mass operations across VMs, then a command line tool such as govc in invaluable.

govc is written in Go, which means it has support on Linux as well as most other platforms.

Installation

Identify the latest release on releases page for govc. Then install on Linux using these commands:

# Download govc package and unzip
wget https://github.com/vmware/govmomi/releases/download/v0.20.0/govc_linux_amd64.gz
gunzip govc_linux_amd64.gz

# rename
mv govc_linux_amd64 govc
sudo chown root govc
sudo chmod ug+r+x govc
sudo mv govc /usr/local/bin/.

# validate in path
which govc

# validate version
govc version

Basic Connectivity

Export a set of environment variables so the CLI knows how to connect to vCenter:

# vCenter host
export GOVC_URL=myvcenter.name.com

# vCenter credentials
export GOVC_USERNAME=myuser
export GOVC_PASSWORD=MyP4ss

# disable cert validation
export GOVC_INSECURE=true

And now try the most basic commands:

$ govc about
Name:         VMware ESXi
Vendor:       VMware, Inc.
Version:      6.7.0
Build:        8169922
OS type:      vmnix-x86
API type:     HostAgent
API version:  6.7
Product ID:   embeddedEsx
UUID

$ govc datacenter.info
Name:                mydc
  Path:              /mydc
  Hosts:             1
  Clusters:          0
  Virtual Machines:  3
  Networks:          1
  Datastores:        1

$ govc ls
/mydc/vm
/mydc/network
/mydc/host
/mydc/datastore

# set variable 'dc' so we can use it later
$ dc=$(govc ls /)

Network

govc ls -l=true $dc/network

ESXi Cluster

# cluster name
govc ls $dc/host
# details on cluster, all members and their cpu/mem utilization
govc host.info <clusterPath>

# all members listed (type: HostSystem, ResourcePool)
govc ls -l=true <clusterPath>

# for each cluster member of type HostSystem, individual stats
govc host.info <memberPath>

Datastores

# top level datastores (type: Datastore and StoragePod)
govc ls -l=true $dc/datastore

# for atomic Datastore type, get capacity
govc datastore.info <datastorePath>

# get StoragePod overall utilization
govc datastore.cluster.info <storagePodPath>

# get list of storage pod members
govc ls <storagePodPath>

# then get capacity of each member
govc datastore.info <storagePodMemberPath>

Get VM information

Now search for a VM by name, get a summary of the VM details, then full VM details.

# show basic info on any VM names that start with 'myvm'
govc vm.info myvm*

# show basic info on single VM
govc vm.info myvm-001

# use full path to get detailed VM metadata
vmpath=$(govc vm.info myvm-001 | grep "Path:" | awk {'print $2'})
govc ls -l -json $vmpath

Shutdown VM, power up VM

# gracefully shutdown guest OS using tools
govc vm.power -s=true myvm-001

# force immediate powerdown
govc vm.power -off=true myvm-001 

# power VM back on
govc vm.power -on=true myvm-001

Happy Learning !!!


Share At:
0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
gate.io para yatırma
12 days ago

Thank you very much for sharing. Your article was very helpful for me to build a paper on gate.io. After reading your article, I think the idea is very good and the creative techniques are also very innovative. However, I have some different opinions, and I will continue to follow your reply.

binance推薦
24 days ago

Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.com/zh-TC/register?ref=UM6SMJM3

Back To Top

Contact Us