ESXi 5.1 Command line

Posted on Thursday, April 2, 2015







I have an ESXi 5.1.0, I make use of it a lot.  Typically I am using VSphere 5.5 as my only view/control into the ESXi box.

I am going to spend some time trying to figure out how to use the command line tools in ESXi.  This is my first attempt down that road.




Turn on SSH access


If I am going to do this via the command line I need access to the command line on the ESXi box itself.  To do this I need to turn on ESXishell and SSH access.




Select the ESXi server






Open the Configurations tab.







Open Security Profile.






Click on Properties in the upper right.








This window should open.  Select SSH. And click Options







Select "Start and stop with host"  Then click OK.






Now select ESXi Shell and click Options.







Select "Start and stop with host" and click OK.







SSH and ESXi Shell are not running yet.  But if you reboot your ESXi box they should come up automatically.


Click OK to close this window.








Right click on your box and select Reboot.





Click Yes






Give a reason, if you want, and click OK.

Wait for it to reboot.




Go back into Configurations -> Security Profile -> Properties






Now SSH and ESXi shell should be running.








Adding SSH keys


My plan, in the long run, is to have another server run some commands via ssh.  To make this work, easily, I need to put my public ssh keys on the esxi box.


Copy over your public key from your box.


  > cat ~/.ssh/id_rsa.pub | ssh root@192.168.0.111 'cat >> /etc/ssh/keys-root/authorized_keys'


Now you should be able to login without a password.








Esxi commands

Before I start into this here are a few sites I found that show some example commands to query the ESXi server.


(This next one is a really good how to on vim-cmd go read it!)






Poking at it


Run the following command (this is actually only a partial command, but it will list all the available options)


  > vim-cmd vmsvc




List the Current VMs



  > vim-cmd vmsvc/getallvms





There is more information deisplayed, but here you can see the Vmid for each VM I have, and the name for each VM.

The VMID is important to note as it's needed for many other commands.






Make sure vmtools are installed


Many of the tools require VMWare tools installed on the VM.

Run the folling command, replacing vmid with the id of the actual VM.


  > vim-cmd vmsvc/get.guest <vmid> | grep toolsStatus


 


Looks like some of my machines don't have it installed or have an old version.

Here is a longer command that will see if the tool is running on all the machines.


  > vim-cmd vmsvc/getallvms | tail -n+2 | sed 's/\[data.*//g' | sed 's/ */:/g' | while read i; do echo $i | cut -d ':' -f2,3; vim-cmd vmsvc/get.guest `echo $i | cut -d ':' -f2` | grep toolsStatus; done


It may be ugly but it works





Looks like I have not been properly keeping things clean over the years.
Before I go on I need to install the VMware tools on all these machines.



To do this I would normally do the following
  •   Open up VSphere and log into the ESXi box
  •   Right click on the VM





            And choose Guest -> Install/Upgrade VMware Tools.

This would mount a CD to the VM.
  • Then I would follow a procedure to untar the files and install the tools.

But, now I am wondering can I mount the tools from the command line?



I am going to see if I can fix my OpenVPN server



This servers vmid is 24, it has no tools installed, and it’s off.

Turn it on with this command


  > vim-cmd vmsvc/power.on 24





I forgot what static IP I set for this server so let me see if I can look it up via the command line.


Normally I could run this command


  > vim-cmd vmsvc/get.summary 24 | grep ip


But, it does not work…  I am guessing because the tools are not installed.



I logged in via the Console on VSphere and simply ran > ifconfig -a to get the IP address…

OK now I am logged in to ESXi in one window and this OpenVPN server on the other.

Mount the Install Tools to the server with this command.


  > vim-cmd vmsvc/tools.install 24


Looks like that kinda worked.





This popped up in my VSphere.   Looks like it did try to mount the install tools, so the command was correct!  But, I am guessing I left a virtual CD on this server (Probably the Ubuntu install disk)

I set it to no and clicked OK so I could investigate.



Yep that is exactly what happened.

Can I toggle the connect at power on via command line?




I could not find a command line way of doing this so I simply unchecked these and clicked ok in VSphere




Since the Machine was on I got this pop up.  Click OK.

OK, let's try this again….
Mount the Install Tools to the server with this command.


  > vim-cmd vmsvc/tools.install 24


That worked


Here is the procedure I used to mount the CD and install the vmware tools.

Run the following commands


   > sudo su root
   > mkdir /mnt/cdrom
   > mount /dev/cdrom /mnt/cdrom
   > cd /tmp
   > tar zxpf /mnt/cdrom/VMwareTools-8.6.0-425873.tar.gz
   > cd vmware-tools-distrib
   >./vmware-install.pl -d


The -d option will answer all the question for you with the defaults.

To test if it's running


    > sudo service vmware-tools status






You should see this.

To confirm from the command line its running.  Go to the ESXi shell and run.


    > vim-cmd vmsvc/get.guest 24 | grep toolsStatus





Looks good!

I am going to follow this procedure with all my other servers that don't have VMware tools installed.

Run this command to list all the servers that do not have vmware tools installed


  > vim-cmd vmsvc/getallvms | tail -n+2 | sed 's/\[data.*//g' | sed 's/ */:/g' | while read i; do echo $i | cut -d ':' -f2,3; vim-cmd vmsvc/get.guest `echo $i | cut -d ':' -f2` | grep toolsStatus; done | grep toolsNotInstalled -B 1





I am going to quickly mount the tools on all of these.

Turns out 25 is not running so I need to start that one up real quick…


  > vim-cmd vmsvc/power.on 25


Now I can mount them all


  > vim-cmd vmsvc/tools.install 25
  > vim-cmd vmsvc/tools.install 27
  > vim-cmd vmsvc/tools.install 28
  > vim-cmd vmsvc/tools.install 29
  > vim-cmd vmsvc/tools.install 31









Now that server 25 has the VMware tools installed I can easily power it off


  > vim-cmd vmsvc/power.off 25




OK they should all be fixed now


  > vim-cmd vmsvc/getallvms | tail -n+2 | sed 's/\[data.*//g' | sed 's/ */:/g' | while read i; do echo $i | cut -d ':' -f2,3; vim-cmd vmsvc/get.guest `echo $i | cut -d ':' -f2` | grep toolsStatus; done | grep toolsNotInstalled -B 1


I ran this as a double check and go back nothing J

Ooops problem…



  > vim-cmd vmsvc/getallvms | tail -n+2 | sed 's/\[data.*//g' | sed 's/ */:/g' | while read i; do echo $i | cut -d ':' -f2,3; vim-cmd vmsvc/get.guest `echo $i | cut -d ':' -f2` | grep toolsStatus; done




toolsNotRunning….  Oh wait those are my VMs that are currently powered down





I do have one server with toolsOld.  I guess I need to update the tools.  I think I can do this via command line, let me try.



  > vim-cmd vmsvc/tools.upgrade 1


It upgraded it J


As a quick confirmation I ran this


  > vim-cmd vmsvc/getallvms | tail -n+2 | sed 's/\[data.*//g' | sed 's/ */:/g' | while read i; do vim-cmd vmsvc/get.guest `echo $i | cut -d ':' -f2` | grep toolsStatus; done





Looks good.




References


[1]        Enabling Password Free SSH Access on ESXi 5.0
            Accessed 3/2015
[2]        Performing common virtual machine-related tasks with command-line utilities
            Accessed 3/2015
[3]        VMware ESXi vim-cmd Command: A Quick Tutorial
            Accessed 3/2015

No comments:

Post a Comment