Using plink to modify ESXi host configuration files via SSH from a PowerCLI script

I am a big advocate of automation and saving time with a good script. Whenever I can find a task that is fairly lengthy, and is likely to be repeated in future, I always consider scripting it. There are many way to configure an ESXi host when it comes to writing build or automation scripts. In fact, I often feel we are quite spoilt for choice. Here are just some of the tools we have available to use:

  • PowerCLI
  • esxcli
  • vMA
  • vCLI

I was working on a build configuration script the other day using PowerCLI and found the need to edit some configuration files on the hosts. I wanted to edit the configuration file /etc/vmware/config during the execution of a single PowerCLI script without needing to stop the script or have an additional step to do myself. The following is what I came up with to achieve this:

  • Configure host as normal using PowerCLI
  • Use PowerCLI to start SSH service on host
  • execute plink script to connect to host, run command via SSH, then disconnect
  • Use PowerCLI to stop SSH service on host
  • Continue with rest of PowerCLI script

 

Plink is a command line connection tool – essentially a command line version of PuTTy. You can call it from dos prompt and issue it with a single, (or list) of commands to run once connected to a specified host. You can download Plink over here.

 

So without further ado, let’s take a look at the script as I described above.

# At start of our script we ask for the host's IP or name (this could be automated if you like)
$hostIP = Read-Host "Enter ESX host IP/dns name: "
$vmhost = Get-VMHost $hostIP

# Start the SSH service
$sshService = Get-VmHostService -VMHost $vmhost | Where { $_.Key -eq “TSM-SSH”}
Start-VMHostService -HostService $sshService -Confirm:$false

# Use SSH / plink to configure host with some additional script
cmd /c "plink.exe -ssh -pw HOSTROOTPASSWORD -noagent -m commands.txt root@$hostIP"

# Stop SSH service
Stop-VMHostService -HostService $sshService -Confirm:$false

 

As you can see, we start off by asking for the host IP or name, this is the only bit of manual input, but even this could be automated. The script then finds the SSH service on the host, and starts it. After this, the script calls the plink.exe file via cmd /c and connects using the root user@ the host’s IP as we entered at the beginning of the script over SSH. Plink is pointed to a commands.txt file (previously placed in the script execution folder), which contains the actual lines of bash script to be executed on the ESXi host via SSH.

Here is the content of the commands.txt file that I refer plink.exe to use (as an example, this bit of script enables copy/paste operations on all VMs running on this host in the guest OS’ console, as per VMware KB 1026437), but could contain any other commands you wish to execute on the ESXi host over SSH.

echo 'isolation.tools.copy.disable="FALSE"' >> /etc/vmware/config
echo 'isolation.tools.paste.disable="FALSE"' >> /etc/vmware/config

 

* Note two very useful techniques show by Alan in the comments section below, showing how to automatically download plink.exe if it is not available when the script is run, and also how to accept the SSH fingerprint key request by piping Y to plink.exe via the script – check out Alan’s blog post here for more detail.

vExpert spotlight

 

The vExpert title is given out to those who have provided a significant contribution to the VMware community over the previous year. Individuals lucky enough to be awarded this title may receive it for a number of different reasons. For the 2012 title, there were three paths possible to become a vExpert.

  • Evangelist path
  • Customer path
  • VPN (VMware Partner Network) path

Having created and run Shogan.tech over the last four and half years or so, providing content to the community around VMware, automation, and scripting (as well as a couple of small little utilities), I was lucky enough to receive the title on the evangelist path this year.

Gregg Robertson (from thesaffageek.com) has kindly been helping out with organising and publishing the vExpert spotlight entries. After finally managing to find a bit of time, I managed to get my entry together a few weeks ago and submitted it. It has just been posted up on the VMware VMTN blog: http://blogs.vmware.com/vmtn/2012/12/vexpert-spotlight-sean-duffy.html

 

 

UK VMware User Group (UKVMUG)

 

Last week I was lucky enough to be able to attend the UKVMUG, hosted at the National Motorcycle Museum near Birmingham.

 

Wednesday (14/11/2012)

Travelling up early Wednesday evening, I made it to the venue at about 18h45 – just in time for the Veeam sponsored vCurry evening. Here I met up with a few fellow bloggers and local London VMUG members, such as, Jane RimmerJeremy BowmanDarren Woollard, Julian Wood, Steve Bruck, Stu McHugh, and Jonathan Medd amongst others! An excellent vCurry was had along with a drink or two. After that it was back to the hotel in preparation for the UKVMUG the following day.

 

Thursday (15/11/2012)

Registration started at 08h00 with all attendees receiving a welcome pack and VMUG lanyard with their details. Breakfast and teas/coffees were served and everyone had a bit of time to chat before Alaric Davies (of the London VMUG team) opened the proceedings. After this, Joe Baguley did the opening keynote. His keynote was interesting, and generally well received, keeping everyone riveted as far as I could tell.

 

Proceedings were slightly delayed and led to Alaric asking if attendees wouldn’t mind forfeiting the first coffee break – much to the dismay of most, including Simon Long 😀

Throughout the rest of the day, I managed to attend the following sessions:

  • Nimble Storage | Stress-Free Data Protection for VMware and VDI
  • Alan Renouf and William Lam | Practical Automation for Everyone
  • Mike Laverick | Building my vCloud Director Home Lab
  • Duncan Epping and Frank Denneman | Deep-Dive Discussion Group

 

In-between sessions I managed to chat with Automation gurus William Lam, Alan Renouf and Jonathan Medd. Apart from demoing a small little iOS app I have been working on (for my company Xtravirt),  I bounced some ideas off of Jonathan regarding a small mini-project I have been working on to automate vSphere lab environment deployment. I got some useful advice here, especially with regard to deploying vCenter unattended (thanks Jonathan!) It was also great to meet William Lam in person and have a brief chat about the VMware MOB (which I find extremely useful for a project I am currently working on).

I was also interviewed by Steve Bruck for the vNews podcast – they were interested in chatting about vMetrics – so I managed to get a small plug in for my WordPress plugin there.

 

Finally the closing keynote by Scott Lowe was held, entitled “Staying Sharp and Relevant in IT”. This was an excellent session and provided some great insight, ideas and thoughts into learning new technologies or employing techniques to help study new areas for today’s IT pro.

 

After the event was over, Jane Rimmer from the London VMUG team was very kind to invite Darren and I to a small dinner gathering at a nearby restaurant. This was perfect for me as I wanted to avoid rush hour traffic for my drive back home (120 miles or so). We had an excellent dinner and chat with the likes of Jane Rimmer, Simon Gallagher, Alaric Davies, Hans De Leenheer, some ladies from the US VMUG team, and last but not least, Scott Lowe.

 

All in all the event was a success.

Just for fun, I decided to create pie/donut chart representing the number of #UKVMUGYAY hashtagged tweets vs #UKVMUGBOO (good vs bad) – note that some of the “boo” tweets did seem to be playful – i.e. not really showing dissatisfaction! This was a quick and rough calculation by searching for “ALL” tweets on each hashtag and counting them manually – I may have been a couple out here or there 🙂 This is as of tonight (18/11/2012).

 

UK VMUG, November 2012

 

The time is almost upon us – the UK VMUG is coming up this week. It will again be held at the National Motorcycle Museum in the West Midlands this Thursday, the 15th November.

 

There is an exciting line up, with lots of interesting sessions planned for the day. You can take a look at the official time table over here. I have still not decided on which sessions I will be attending, but the practical automation session with Alan Renouf and William Lam is right up my alley! Duncan Epping and Frank Denneman are also going to be hosting a deep-dive discussion group which I’m sure will provide for some interesting discussion. Having read two of their books (HA & DRS Technical deep-dive, and Clustering deep-dive), it would also be great to try and attend this session and see what is on the cards.

 

A work colleague of mine, Darren Woollard (@dawoo) is also running a side session on designing a highly available infrastructure. Having attended one of these myself, I can highly recommend it, as it is a great interactive session, with Darren getting everyone to participate in a mock ‘Whiteboard Design’ session! To find Darren, look out for his “VMW User Group” tshirt – resembling the VW logo.

 

There are also some great vendors and sponsors at this event. It will be interesting to get a more detailed look at some of the technologies I have not yet had the chance to see for myself. It will be a bit of a drive for myself getting from SW Greater London up to Coventry, but I’ll either be making a plan to find a hotel the night before, or to drive there and back on the same day. Any suggestions as to logistics / accommodation would be greatly appreciated!

I am looking forward to the event and hope to see many of the community members there this Thursday!

 

Blogs / websites of people mentioned in this blog post:

vMote (Darren Woollard)

Virtu-al.net (Alan Renouf)

virtuallyGhetto (William Lam)

frankdenneman.nl (Frank Denneman)

Yellow Bricks (Duncan Epping)

 

 

ESXi 5 Host Backup & Restore GUI Utility updated to 1.1

This little host backup utility I created back in February 2012 has been receiving quite a bit of attention, and has already managed to get over 2000 downloads.

 

Someone recently asked the other day if it was possible to restore a configuration file to a new host (i.e. new hardware). With version 1.0 of my utility, this was not possible due to mismatches that the PowerCLI cmdlet finds (i.e. MAC addresses on NICs etc… on the new hardware when compared to the existing backup). However, the Set-VMHostFirmware cmdlet allows the use of a -Force paramter, so I set about updating the utility to allow for this.

 

Here is a quick list of changes in version 1.1

  • Allows restore to new hardware (tick the “Force restore to new hardware” checkbox). Please note that I have only very briefly tested this on virtualised ESXi hosts – it works, but I am not sure how networking configurations are applied to NICs and differing physical NIC orders – so it is best to test this thoroughly in a dev/test environment before using anywhere else!
  • Tested against single ESXi hosts as opposed to connecting to vCenter first.
  • Updated labels to neaten up a bit – connection box now shows that you can connect to single hosts or vCenter
  • Tested on ESXi 5.1

 

You can download version 1.1 from the same page as before: ESXi 5 Host Backup & Restore GUI Utility