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.

-Alan

Alan

Alan Renouf has a role of Automation Frameworks Product Manager at VMware responsible for providing the architects and operators of the cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/vipowershell and has a personal blog at http://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

You may also like...

5 Responses

  1. fsdfs says:

    Skip -batch and it work

  2. Sushant says:

    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.

  3. Sourav says:

    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.

  1. January 8, 2013

    [...] 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. Categories: How-tos, PowerCLI, Virtualization, VMware Tags: automate, plink, [...]

  2. January 13, 2013

    [...] Renouf делится маленьким трюком по тюнингу утилиты plink. Он позволяет выполнить команду в консоли хоста без [...]

Leave a Reply

%d bloggers like this: