SSH PowerShell tricks with plink.exe

image

Recently I needed to perform some actions in PowerCLI from the ESXi Shell, as you may know there are currently no cmdlets from VMware to allow you to run shell commands but one option which is popular within the communities is using a 3rd party tool called plink.exe to run the commands via SSH.

Examples of this can be seen here and here.

Download plink.exe via PowerShell

The script I was writing was for someone who I knew didn’t already have plink installed and I wanted them to have little to no effort when using this script so the first trick I wanted to share was the ability to check for plink.exe and download it if it wasn’t in the same directory as the script, this can be seen below:

$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$PlinkLocation = $myDir + "\Plink.exe"
If (-not (Test-Path $PlinkLocation)){
	Write-Host "Plink.exe not found, trying to download..."
	$WC = new-object net.webclient
	$WC.DownloadFile("http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe",$PlinkLocation)
	If (-not (Test-Path $PlinkLocation)){
		Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
		Exit
	} Else {
		$PlinkEXE = Get-ChildItem $PlinkLocation
		If ($PlinkEXE.Length -gt 0) {
			Write-Host "Plink.exe downloaded, continuing script"
		} Else {
			Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: http://the.earth.li/~sgtatham/putty/latest/x86/plink.exe"
			Exit
		}
	}	
}

Accept plink.exe host key automatically

Secondly when connecting to a host for the first time you will need to accept the host key and allow plink.exe to connect to the host, the message is similar to the one below:

image

At the moment there is no option in plink.exe to skip the host key checking, however after some searching and messing around I came upon a solution.

It turns out you can actually send a Y to the script you are running by simply piping “echo Y” to plink.exe, this sends a Y accepting the host key when the question is asked from the command line, this can be scripted like the below example:

Echo Y | Plink.exe…..

Hopefully someone will find these useful in the future as I did with this script.

11 thoughts on “SSH PowerShell tricks with plink.exe

  1. Pingback: command line interface - How to compile capacity and used capacity of an EMC VNX via CLI? - ITTone

  2. Hauce

    Find it useful? Have you ever had to harden environment? 🙂 This is GOLD until PowerCLI contains cmdlets that allow modification of ESXi system configuration files directly.

  3. Pingback: Powershell script to re-pointfail-over the VC to another replication Platform Service Controller ( PSC – VCSA 6.0) | Techbrainblog

  4. Pingback: Powershell script to find which platform service controller ( PSC ) is pointing to my vCenter appliance VCSA -6.0 | Techbrainblog

  5. steve sonnenberg

    We have an issue which we haven’t found a solution. The program being invoked by plink outputs periodic messages. Unfortunately plink doesn’t display them until the plink command finishes in entirety. This seems to be a shortcoming of plink.exe? We are using -batch.

  6. Pingback: ESXi5.5 Host Services (vsantraced) non-VMHostService | IfItIsNotBroken

  7. Sushant

    Thanks Alan Renouf.

    Your resolution for using echo Y before plink helped me solving an issue wherin i was calling plink.exe within a java program.

  8. Sourav

    Hi Alan ,

    I was going through some of your’s & Luc’s old notes regarding executing cmds through plink.

    I tried using this script(for powerpath license check) for a single host & to grep the “State” status and redirect to a csv file for that particular host printing the hostname & “state” status.

    Below is my script i tried to use :

    $ExportFilePath = “C:\HostNicInfo.csv”
    $PuttyUser = “root”
    $PuttyPwd = “passwd”
    $hostlist = “esx-test-120”
    $Plink = “echo Y |C:\plink.exe”
    $PlinkOptions = ” -v -batch -pw $PuttyPwd”
    $cmd1 = ‘/opt/emc/powerpath/bin/powermt check_registration ‘
    $RCommand = ‘”‘ + $cmd1 + ‘”‘
    $command = $Plink + ” ” + $PlinkOptions + ” ” + $PuttyUser + “@” + $hostlist + ” ” + $RCommand
    $msg = Invoke-Expression -command $command

    Write-Host “Connecting to: ” $hostlist -ForegroundColor Green
    $HostInfo.license = ($Message[0] -split “State: “)[1]
    $Report += $HostInfo.license
    $Report | Export-Csv $ExportFilePath -NoTypeInformation

    The output of the $cmd1 if i execute manually gives me like this below :

    Type : served (counted)

    State : licensed

    Registered To : ACTIVATED

    Issue Date : xx-xx-xx

    Feature : PowerPathMP

    Feature Version : 5.4

    Registering Svr : xxxxxxxx

    License Count : xxxx

    Overdraft Count : xxx

    License Server : xxxxxxxx

    In the above output i tried for “State” status. But i am getting the below errors while executing the above script.

    Looking up host “esx-test-120”
    Connecting to 10.182.79.63 port 22
    Server version: SSH-2.0-OpenSSH_5.6
    Using SSH protocol version 2
    We claim version: SSH-2.0-PuTTY_Release_0.62
    Doing Diffie-Hellman group exchange
    Doing Diffie-Hellman key exchange with hash SHA-256
    The server’s host key is not cached in the registry. You
    have no guarantee that the server is the computer you
    think it is.
    The server’s rsa2 key fingerprint is:
    ssh-rsa 2048 54:d5:1a:c3:61:b0:2e:c1:43:cc:eb:06:29:2c:5c:86
    Connection abandoned.
    Disconnected: User aborted at host key verification
    Connecting to: esx-test-120
    Cannot index into a null array.
    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:13 char:31
    + $HostInfo.license = ($Message[ <<<< 0] -split "State: ")[1]
    + CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

    Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
    At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\abc.ps1:15 char:21
    + $Report | Export-Csv <<<< $ExportFilePath -NoTypeInformation
    + CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand

    Please share your advices how to make it work because in future i want it to import a server list instead of single server.

    Thanks in Advance.

  9. Pingback: Обзор блогов от 14/01/13 | vMind.ru

  10. Pingback: Using plink to modify ESXi host configuration files via SSH from a PowerCLI script | Shogan.tech

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.