Topics: Scripting, Virtualization

Using govc

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. You can find the repo for govc at https://github.com/vmware/govmomi/tree/master/govc, along with installation instructions. govc is written in Go, which means it has support on Linux as well as most other platforms.

To perform a quick install on Linux, run this command:

$ sudo curl -L -o - \
"https://github.com/vmware/govmomi/releases/latest/download/govc_$(uname -s)_$(uname \
-m).tar.gz" | sudo tar -C /usr/local/bin -xvzf - govc
Next, you'll want to set up basic connectivity to the vCenter, and for this purpose, you can use a set of environment variables, so the CLI knows how to connect to the vCenter.
# vCenter host
export GOVC_URL=myvcenter.name.com
# vCenter credentials
export GOVC_USERNAME=myuser
# disable cert validation
export GOVC_INSECURE=true
Next, you can try out a few 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

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

$ govc ls
Next, set a variable $dc, so that we can use it later:
$ dc=$govc ls /)
Now you can request various information from the vCenter. For example:

$ 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]

# 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]

VM information:
# 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

Shtudown 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

Topics: Scripting


ShellCheck is a tool that can be used to check the integrity of shell scripts, which can be very useful while developing shell scripts, as it may indicate where issues are within shell scripts. It works with different shells, such as Bash, and the Korn shell.

Visit www.shellcheck.net to get information about how to obtain the tool.

For Red Hat Enterprise Linux based systems (and its derivatives), it can simply be installed as follows:

# yum -y install epel-release
# yum install ShellCheck
Or on Fedora:
# dnf install ShellCheck
ShellCheck helps to identify potential issues with check scripts, and thus can be an important tool for shell script developers. Even if a shell script works fine, the ShellCheck tool can be used to identify potential improvements to shell scripts. Considering that UNIX Health Check software is completely written in shell scripts, it is a tool used by us quite a lot to aid in ensuring that our code is valid and correct.

To check a specific script, e.g. to check script test.sh, run:
# shellcheck test.sh
For more information about the use of ShellCheck, run:
# man shellcheck

Topics: Red Hat / Linux, Scripting

Bash scripting: SSH breaks out of while-loop

If you use a bash shell script that does an ssh command within a while-loop, you may encounter that the ssh command will break out of the while-loop, and that the script doesn't complete all the intended ssh commands. An example of a script is below:

# cat hostsfile
# cat script
cat hostsfile | while read server ; do
        echo $server
        ssh $server uptime
# ./script
 16:19:22 up 11 days, 22:30,  0 users,  load average: 0.00, 0.01, 0.05
As you can see in the example above; the script should run a ssh command for all files in the file "hostsfile". Instead, it stops after the first one.

This can be very easily resolved, by adding the "-n" option for the ssh command, as follows:
# cat script
cat hostsfile | while read server ; do
        echo $server
        ssh -n $server uptime
# ./script
 16:19:22 up 11 days, 22:30,  0 users,  load average: 0.00, 0.01, 0.05
 15:20:56 up 11 days, 22:32,  0 users,  load average: 0.00, 0.00, 0.00

Number of results found for topic Scripting: 3.
Displaying results: 1 - 3.