a simplified command-line interface to VMware vCenter

2017-08-08 15:38:20

govc allows you to interface with VMware vCenter without the need for the dreaded vClient,Windows machines or to write you own scripts to access the horrible VMware API. This way you can easily automate many tasks on VMware directly from the command line or your bash scripts

by the way ,I don’t like interface with ESX/vCenter,this is so cool tools what I desired

First thing,you can compile it yourself, but there’s handly binaries already available on the project github page here

I downloaded it and created a small wrapper for it:

1
2
3
4
5
6
7
8
9
10
11
12
13
# curl -LO https://github.com/vmware/govmomi/releases/download/v0.15.0/govc_linux_amd64.gz
# gunzip govc_linux_amd64.gz
# cat >govc <<EOF
#!/bin/bash

export GOVC_URL='https://username:password@vsphere-ip-or-hostname/sdk'
export GOVC_DATACENTER=VSPHERE_DC
export GOVC_INSECURE=true

/usr/bin/govc_linux_amd64 \$@
EOF
# chmod +x govc*
# cp -i govc* /usr/bin/

Note that you only need GOVC_INSECURE=true if you are using self-signed certificates and you don’t have CA added to you local trusted certs.

For a start,you can get some basic info about you environment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@localhost daily]# ./govc about
Name: VMware vCenter Server
Vendor: VMware, Inc.
Version: 5.1.0
Build: 880146
OS type: win32-x64
API type: VirtualCenter
API version: 5.1
Product ID: vpx
UUID: 4AC51BFC-DC4E-47D3-A912-B51A1A28BAFA

[root@localhost daily]# ./govc datacenter.info
Name: ws_dc01
Path: /ws_dc01
Hosts: 5
Clusters: 0
Virtual Machines: 69
Networks: 3
Datastores: 10
Name: ws_dc02
Path: /ws_dc02
Hosts: 5
Clusters: 0
Virtual Machines: 118
Networks: 32
Datastores: 5

if you want to get usage about govc,just run govc without any argument. To see what parameters a command supports, run govc command –help

eg,you can easily get some info with one simple command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@localhost daily]# ./govc datastore.info 
Name: datastore21
Path: /ws_dc01/datastore/datastore21
Type: VMFS
URL: ds:///vmfs/volumes/522460d8-f202b496-a69f-90b11c2afe3d/
Capacity: 2508.2 GB
Free: 470.2 GB
Name: datastore20
Path: /ws_dc02/datastore/datastore20
Type: VMFS
URL: ds:///vmfs/volumes/52778cbb-107633af-7209-90b11c2a9f0f/
Capacity: 2508.2 GB
Free: 176.5 GB

[root@localhost daily]# ./govc host.info
/usr/bin/govc_linux_amd64: default host resolves to multiple instances, please specify
[root@localhost daily]# ./govc host.info -host.ip=192.168.1.20
Name: 192.168.1.20
Path: /ws_dc01/host/192.168.1.20/192.168.1.20
Manufacturer: Dell Inc.
Logical CPUs: 32 CPUs @ 2599MHz
Processor type: Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
CPU usage: 2703 MHz (3.3%)
Memory: 130976MB
Memory usage: 92697 MB (-1.2%)
Boot time: 2017-06-26 11:09:53.406999 +0000 UTC


[root@localhost daily]# ./govc vm.info -vm.ip=192.168.1.180
Name: ws-180
Path: /ws_dc01/vm/ws-180
UUID: 4232a9b7-a1de-e45b-453b-beade5b7935a
Guest name: CentOS 4/5/6 (64-bit)
Memory: 4096MB
CPU: 8 vCPU(s)
Power state: poweredOn
Boot time: 2017-06-26 11:34:54.042422 +0000 UTC
IP address: 192.168.1.180
Host: 192.168.1.20

If you want to look someting , it good way to use ls command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost daily]# ./govc ls
/ws_dc01/vm
/ws_dc01/network
/ws_dc01/host
/ws_dc01/datastore
[root@localhost daily]# ./govc ls /ws_dc01/vm
/ws_dc01/vm/elk149
/ws_dc01/vm/elk148
/ws_dc01/vm/elk147
...

[root@localhost daily]# ./govc ls /ws_dc01/host
/ws_dc01/host/192.168.1.20
/ws_dc01/host/192.168.1.21
...

[root@localhost daily]# ./govc ls /ws_dc01/datastore
/ws_dc01/datastore/datastore21
/ws_dc01/datastore/datastore20
...

govc also allow you to run esxcli on the specified host, for example in one of my many experiments I ran:

1
2
3
4
5
6
7
8
[root@localhost daily]# ./govc host.esxcli  --host.ip=172.16.56.20 vm process list|grep DisplayName | awk {'print $2'} | sort
debug140
debug141
debug142
elk147
elk148
elk149
...

A very import thing to notice is that every command can be run with the parameters -json=true.This output a machine-parsable format that includes many more details than the normal text output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@localhost daily]# ./govc ls -json /ws_dc01/datastore/datastore20|python -m json.tool
{
"DeRef": false,
"Dump": false,
"JSON": true,
"Long": false,
"Out": {},
"TTY": false,
"ToRef": false,
"Type": "",
"elements": [
{
"Object": {
"AlarmActionsEnabled": true,
"AvailableField": null,
"Browser": {
"Type": "HostDatastoreBrowser",
"Value": "datastoreBrowser-datastore-514"
},
"Capability": {
"DirectoryHierarchySupported": true,
"NativeSnapshotSupported": false,
"PerFileThinProvisioningSupported": true,
"RawDiskMappingsSupported": true,
"SeSparseSupported": null,
"StorageIORMSupported": true,
"TopLevelDirectoryCreateSupported": null,
"UpitSupported": null,
"VmfsSparseSupported": null,
"VsanSparseSupported": null
},
[...]

of course,could be very handy for your scripts!

ref
govmomi (vSphere SDK for Go), govc CLI & Kubernetes on vSphere
Automate your vCenter interactions from the Linux commandline with govmomi and govc
PySphere script to clone a template into multiple VMs with post processing
terraform-provider-vsphere


您的鼓励是我写作最大的动力

俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。