The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 11 Nov 2011

Networking, PowerShell

Tags: , ,


To my mind, the beauty of a truly powerful scripting language, is the sense that you can achieve anything that you might need to. You just need to apply yourself, and the full force of Google, and be prepared for a bit of trial and error.

The fact that PowerShell let’s you unleash the full might of the .NET army means that this is pretty much true for it. Recently I head the need to be able to interact with a number of Vyatta routers (see my SSH to Vyatta Router Using PowerShell post), in order to be able to get a load of virtual machine’s NAT’ed addresses, and found two possible solutions, one of which is fairly well documented on the Web (which I struggled to get on with), the other of which is new (in beta still at time of writing) and not particularly prevalent, but seems very flexible….

Firstly, if you want use Tamir Gal’s implementation, personally I didn’t have much success with it, but plenty have.  You can find a good example on how to use it here – http://poshcode.org/1010.

Renci SSH.NET

Renci SSH.NET is an ongoing project, still in beta, but already provides a workable solution. Providing both .NET 4.0 and 3.5 libraries.  You need to use .NET 3.5 if you’re running your scripts on PowerShell v2  (and this might also require that you install .NET 3.5 1st if you don’t already have it).  If you’re running PowerShell v1 see the comments below from William.

To get running you need to download the binary DLL file from the Downloads section of http://sshnet.codeplex.com/, and put it in a place where you can load the assembly into your PowerShell session.

If you place the DLL in a /lib sub-folder to where the script is running could be loaded using.

[void][reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\RenciSSH\Renci.SshNet.dll") )      # DLL file is in \lib\RenciSSH\ below calling script

If you get a HRESULT: 0x80131515 error then you’ll need to unblock the downloaded DLL file (see http://vwiki.co.uk/SSH_Client_(PowerShell)#Renci_SSH.NET for further info).

Basic Commands

Create a SSH Client object, and connect…

$SshClient = New-Object Renci.SshNet.SshClient("ssh-server", 22, "user", "password")
$SshClient.Connect()

Run a command (eg a Unix ifconfig against the server)…

if ($SshClient.IsConnected) {
    $SshCommand = $SshClient.RunCommand("ifconfig")
    $ifconfig = $SshCommand.Result.Split("`n")
}

Clear down…

$SshCommand.Dispose()
$SshClient.Disconnect()
$SshClient.Dispose()

Further Reading

For examples on how to use the PowerShell SSH client see these pages on this site.

 


4 Comments to “SSH Client using PowerShell”

  1. William says:

    If anyone else is using PowerShell v1.0 and is try to use Renci.SshNet.dll but having problems this is what I did to make it work.

    Create or Add to the file “$pshome\powershell.exe.config” and “$pshome\powershell_ise.exe.config”

    This will make powershell.exe and powershell_ise.exe load the .NET 4.0 Runtime along with 2.0 and allow you to load “Renci.SshNet.dll” you may need to reboot after making the changes. To check which runtime is loaded you can use the following commands in PowerShell
    [System.Reflection.Assembly]::GetExecutingAssembly().ImageRuntimeVersion
    and
    [Environment]::Version.ToString()
    the commands will return different versions if the config is loaded correctly.

    I hope this saves someone a little bit of time.

  2. William says:

    The code that goes inside of “$pshome\powershell.exe.config” and “$pshome\powershell_ise.exe.config” is at this link.

    http://pastebin.com/wf2wy0vZ

  3. […] There’s an alternative (free) PowerShell SSH solution, using a newer library implementation, see vBlog >> SSH Client Using PowerShell […]

Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.