SSH Client (PowerShell): Difference between revisions
m (→Renci SSH.NET: Added links to vBlog) |
(→Renci SSH.NET: Reworded) |
||
(One intermediate revision by the same user not shown) | |||
Line 13: | Line 13: | ||
== Renci SSH.NET == | == Renci SSH.NET == | ||
[http://sshnet.codeplex.com/ Renci SSH.NET] is | [http://sshnet.codeplex.com/ Renci SSH.NET] is a stable project still receiving ongoing active development, providing a .NET SSH library, which can be used by PowerShell. It is very easy to use, and robust. | ||
It provides both .NET 4.0 and 3.5 libraries (see [[Getting_Started_(PowerShell)#Installed_Version...|Installed_Version]] if you don't know what version of PowerShell you're running) | |||
* PowerShell v1 - use .NET 3.5 library and see [http://vblog.strutt.org.uk/2011/11/ssh-client-using-powershell/#comment-245 here] for additional guidance on how to get working | |||
* PowerShell v2 - use .NET 3.5 library | |||
* PowerShell v3 - use either .NET 3.5 or .NET 4 libraries | |||
To get running you need to download the binary DLL file... | |||
# Go to http://sshnet.codeplex.com/ | |||
# Go to the '''Downloads''' page | |||
# Download the '''SshNet 3.5 Binary''', and put it in a place where you can load the assembly (the DLL file) into your PowerShell session, eg | |||
<source lang="powershell"> | <source lang="powershell"> | ||
[void][reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\RenciSSH\Renci.SshNet.dll") ) # DLL file is in \lib\RenciSSH\ folder below the calling script | [void][reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\RenciSSH\Renci.SshNet.dll") ) # DLL file is in \lib\RenciSSH\ folder below the calling script | ||
</source> | </source> | ||
You may need to install .NET if you don't have it running already. Install [https://www.microsoft.com/download/en/details.aspx?displaylang=en&id=21 .NET 3.5] or [https://www.microsoft.com/en-gb/download/details.aspx?id=30653 .NET 4.5] as appropriate | |||
'''If you get an error that includes <code>Exception from HRESULT: 0x80131515</code>''', you need to ''Unblock'' the downloaded DLL. Right-click over the file and select '''Properties''', then click on the '''Unblock''' button (if there's no button, its already unblocked). | |||
For some examples of what can you do, see my vBlog pages at http://vblog.strutt.org.uk/tag/renci-sshnet/. | For some examples of what can you do, see my vBlog pages at http://vblog.strutt.org.uk/tag/renci-sshnet/. |
Latest revision as of 13:43, 12 August 2013
Tamir Gal SharpSSH
There are a number of examples of using PowerShell to act as a client to interact with SSH servers, all that I've found using Tamir Gal's SharpSSH .NET library - eg PoSh 1010. Its a port of a Java implementation and whilst some people have obviously had success in using it, I always seemed to get the following error when trying to write to SSH session with a Vyatta router...
# ChannelSession.run System.IO.IOException: Write end dead at Tamir.Streams.PipedInputStream.read() at Tamir.Streams.PipedInputStream.read(Byte[] b, Int32 off, Int32 len) at Tamir.Streams.PipedInputStream.Read(Byte[] buffer, Int32 offset, Int32 count) at Tamir.SharpSsh.java.io.JStream.Read(Byte[] buffer, Int32 offset, Int32 count) at Tamir.SharpSsh.jsch.ChannelSession.run()
I suspect the Write end dead fault is caused by the SSH server throwing an error and closing the socket, due to an disliked command. Which in a way, is fair enough, but its not a particularly graceful way of handling things.
Renci SSH.NET
Renci SSH.NET is a stable project still receiving ongoing active development, providing a .NET SSH library, which can be used by PowerShell. It is very easy to use, and robust.
It provides both .NET 4.0 and 3.5 libraries (see Installed_Version if you don't know what version of PowerShell you're running)
- PowerShell v1 - use .NET 3.5 library and see here for additional guidance on how to get working
- PowerShell v2 - use .NET 3.5 library
- PowerShell v3 - use either .NET 3.5 or .NET 4 libraries
To get running you need to download the binary DLL file...
- Go to http://sshnet.codeplex.com/
- Go to the Downloads page
- Download the SshNet 3.5 Binary, and put it in a place where you can load the assembly (the DLL file) into your PowerShell session, eg
[void][reflection.assembly]::LoadFrom( (Resolve-Path ".\lib\RenciSSH\Renci.SshNet.dll") ) # DLL file is in \lib\RenciSSH\ folder below the calling script
You may need to install .NET if you don't have it running already. Install .NET 3.5 or .NET 4.5 as appropriate
If you get an error that includes Exception from HRESULT: 0x80131515
, you need to Unblock the downloaded DLL. Right-click over the file and select Properties, then click on the Unblock button (if there's no button, its already unblocked).
For some examples of what can you do, see my vBlog pages at http://vblog.strutt.org.uk/tag/renci-sshnet/.
Simple Command
Whilst this is a very simplistic example, involving connecting to a device/server and running the ifconfig
command (Unix equivalent of ipconfig
; it does open the door to more involved usage where you can run a command, parse the result, then run further commands based on the response.
Create a SSH Client object, and connect...
$SshClient = New-Object Renci.SshNet.SshClient("ssh-server", 22, "user", "password")
$SshClient.Connect()
If the connect fails with the following error - you need to install .NET 3.5 (the SSH library is trying to use components that don't exist).
Could not load file or assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
Run a command (eg a Unix ifconfig
) against the server...
if ($SshClient.IsConnected) {
$SshCommand = $SshClient.RunCommand("ifconfig") # Result of 'ifconfig' is returned to $SshCommand
$ifconfig = $SshCommand.Result.Split("`n") # Split up the result into individual lines for easier parsing
}
Clear down...
$SshCommand.Dispose()
$SshClient.Disconnect()
$SshClient.Dispose()
Download File
Simple example showing how to use PowerShell to open up a SFTP connection to a server, and download a file.
Create a SFTP Client object, and connect...
$ConnInfo = New-Object Renci.SshNet.PasswordConnectionInfo("ssh-server", 22, "user", "pass")
$SftpClient = New-Object Renci.SshNet.SftpClient($ConnInfo)
$SftpClient.Connect()
Create an IO stream to local file
$RxFile = "c:\temp\download.txt"
$RxFileStream = [System.IO.File]::Create($RxFile)
Download file, in this case, an Apache log file
$SftpClient.DownloadFile("/var/log/apache2/access.log", $RxFileStream) # Download file contents to stream
$RxFileStream.Flush() # Flush contents through
$RxFileStream.Close() # Write to file and close stream
Clear down...
$RxFileStream.Dispose()
$SftpClient.Disconnect()
$SftpClient.Dispose()