Home > Virtualization > Powerful Hacks With ESXi vim-cmd Command, Together With Shell Commands

Powerful Hacks With ESXi vim-cmd Command, Together With Shell Commands

December 1st, 2013 Leave a comment Go to comments

If you have read my previous article on the vim-cmd, you may have realized how handy it is, especially when it comes to manage virtual machines. There is however a pretty challenging problem to use it – for most commands for a virtual machine, it requires vmid which is an integer that uniquely identifies the virtual machine in the context of an ESXi server. It’s like primary key in SQL database to locate a record (virtual machine instance) in a table (virtual machine type). For people who are familiar with vSphere APIs, the vmid is the same as the value of ManagedObjectReference value of a virtual machine in ESXi. Because most administrators who use commands are not necessarily familiar with vSphere API, it doesn’t help much.

Because we mostly refer a virtual machine with its name or even IP address, it’s not easy to get a virtual machine’s vmid. What we can do is to list all the virtual machines with the following command and search for a virtual machine’s name and write the vmid down:

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

HTML5 App
~ # vim-cmd vmsvc/getallvms
Vmid     Name                           File                                   Guest OS    Version Annotation                                                                                                 
67     SteveTestVM                [datastore1] SteveTestVM/SteveTestVM.vmx    centos64Guest  vmx-08
...

From the list, I can search and find the vmid for my virtual machine so that I can take other actions on it, for example, powering it on/off. Since ESXi shell support other Linux commands, it’s natural to consider grep a virtual machine, just to save some eye search as follows:

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM
67     SteveTestVM                [datastore1] SteveTestVM/SteveTestVM.vmx                             centos64Guest         vmx-08

With the above command, I don’t need to scroll down pages, which is a big plus, but not enough if you want to further automate it. Then, we can use cut command to get the vmid immediately: (Don’t try to get other items, say f 2 for the name. If you wan to get it, use sed to consolidate multiple spaces to one first).

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM | cut -d ' ' -f 1
67

With the vmid in hand, we can now run almost all the command in the vmsvc namespace. Don’t try to pipeline directly as the vim-cmd command does not take standard input as its parameter. Here is where xargs command comes to help. The following commmand pass the vmid to the next vim-cmd command to suspend that virtual machine.

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM | cut -d ' ' -f 1 | xargs vim-cmd vmsvc/power.suspend
Suspending VM:

Another interesting usage is to get a virtual machine’s IP address with the vim-cmd command. If you don’t have VMware Tools installed, there is no way for you to do it via the command. If you run the related command, you will get something like the following:

~ # vim-cmd vmsvc/getallvms | grep SteveTestVM | cut -d ' ' -f 1 | xargs vim-cmd vmsvc/get.guest
Guest information:
 
(vim.vm.GuestInfo) {
   dynamicType = <unset>,
   toolsStatus = "toolsNotRunning",
   toolsVersionStatus = "guestToolsCurrent",
   toolsVersionStatus2 = "guestToolsCurrent",
   toolsRunningStatus = "guestToolsNotRunning",
   toolsVersion = "9216",
   guestId = <unset>,
   guestFamily = <unset>,
   guestFullName = <unset>,
   hostName = <unset>,
   ipAddress = <unset>,
   screen = (vim.vm.GuestInfo.ScreenInfo) null,
   guestState = "notRunning",
   powerPolicy = (vim.vm.PowerPolicy) null,
   appHeartbeatStatus = "appStatusGray",
   guestOperationsReady = false,
   interactiveGuestOperationsReady = false,
}

But if you have a virtual machine with VMware Tools installed, you may have the ipAddress set. Then, you can get the IP as follows:

~ # vim-cmd vmsvc/getallvms | grep -i vCenter | cut -d ' ' -f 1 | xargs vim-cmd vmsvc/get.guest | grep ipAddress | sed -n 1p | cut -d '"' -f 2
192.168.8.198

With the above command, you get the IP address of a named virtual machine in one line. When you have the IP address of a virtual machine, you can do a lot of fun stuff, for example, ssh to it.

The point I want to make here is that vim-cmd is powerful, and even more so if combined with other shell commands in ESXi. What I just showed is limited use case. You can also list all the virtual machines on a particular datastore, get all the vmdk files used by a particular virtual machine, and so on. If you have interesting use cases and/or scripts, please feel free to share them in the comments.

Update: Got a tweet from William who mentioned his writing on vimsh which is very similar to vim-cmd. You may find many useful samples there too.

Categories: Virtualization Tags: , , ,
  1. bish
    December 2nd, 2015 at 15:17 | #1

    you mean

    xargs -n1 vim-cmd vmsvc/power.suspend

    I think the other one could bail out, which may (or may not) be undesirable.

  2. December 4th, 2015 at 12:31 | #2

    Hi Bish, the -n1 option makes it cleaner. Thanks for the tip!

    Steve

  3. Rajeev
    February 19th, 2016 at 17:52 | #3

    Hi Steve..

    Is there a way to get the snapshot ID based on the Snapshot name for this output

    |-ROOT
    –Snapshot Name : 6.5.4.01
    –Snapshot Id : 19
    –Snapshot Desciption :
    –Snapshot Created On : 2/9/2016 4:12:44
    –Snapshot State : powered off
    –|-CHILD
    —-Snapshot Name : 6.5.4.02
    —-Snapshot Id : 20
    —-Snapshot Desciption : Testing
    —-Snapshot Created On : 2/9/2016 23:48:29
    —-Snapshot State : powered off

    Thanks
    Rajeev

  4. February 19th, 2016 at 18:07 | #4

    I think you use grep and pipe the out to cut for all the snapshot IDs. Good luck.

    Steve

  5. Fred Nielsen
    August 31st, 2016 at 14:27 | #5

    Best post ever, seriously. Who needs vCenter? šŸ˜‰

  6. August 31st, 2016 at 22:57 | #6

    Thanks Fred, right on! You can also try our vSearch product as an alternative: http://www.doublecloud.net/prod_vsearch.php
    -Steve

  7. September 19th, 2016 at 16:40 | #7

    Thank you, I’ve just been looking for information about
    this subject for a while and yours is the greatest I’ve
    came upon till now. However, what in regards to the conclusion? Are you certain concerning the source?

  8. October 12th, 2016 at 00:52 | #8

    Good article! We are linking to this particularly great post on our site.
    Keep up the good writing.

  9. November 3rd, 2016 at 09:48 | #9

    I read this article fully about the resemblance of hottest and earlier technologies,
    it’s remarkable article.

  10. bobbyf
    April 12th, 2017 at 15:12 | #10

    This fo course all goes for a BIG BALL OF CHALK when you have a VM with a NOT/ANNOTATION .. do you have a work-around for this as it’s quite difficult to get a script working properly when there’s a not/annotation especially when using column and row values :-(

  1. No trackbacks yet.