Difference between revisions of "SSH Client (PowerShell)"

Jump to navigation Jump to search
Added "Download File"
(→‎Renci SSH.NET: Some rewording, and added .NET not installed error)
(Added "Download File")
Line 15: Line 15:
[http://sshnet.codeplex.com/ Renci SSH.NET] is an ongoing project, its still in beta, but already provides a workable solution with all the core features you're likely to want. Plus as its in active development there's high chance that any probs you might have would be promptly worked on.  It provides both .NET 4.0 and 3.5 libraries, '''you need to use the .NET 3.5 library for PowerShell v2'''.
[http://sshnet.codeplex.com/ Renci SSH.NET] is an ongoing project, its still in beta, but already provides a workable solution with all the core features you're likely to want. Plus as its in active development there's high chance that any probs you might have would be promptly worked on.  It provides both .NET 4.0 and 3.5 libraries, '''you need to use the .NET 3.5 library for PowerShell v2'''.


To get running you need to download the binary DLL file from http://sshnet.codeplex.com/releases/view/72214, and put it in a place where you can load the assembly into your PowerShell session, eg  
To get running you need to download the binary DLL file from http://sshnet.codeplex.com/releases/view/72214, 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">
Line 22: Line 22:


If you don't have .NET 3.5 installed, you'll need to do so from https://www.microsoft.com/download/en/details.aspx?displaylang=en&id=21 (the main installer is just a downloader/bootstrapper, there's a full fat install package link at the bottom of the page).
If you don't have .NET 3.5 installed, you'll need to do so from https://www.microsoft.com/download/en/details.aspx?displaylang=en&id=21 (the main installer is just a downloader/bootstrapper, there's a full fat install package link at the bottom of the page).
=== Simple Command ===
Whilst this is a very simplistic example, involving connecting to a device/server and running the <code>ifconfig</code> command (Unix equivalent of <code>ipconfig</code>; 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...'''
'''Create a SSH Client object, and connect...'''
Line 32: Line 35:
* <code>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.</code>
* <code>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.</code>


'''Run a command (eg a Unix <code>ifconfig</code> against the server...'''
'''Run a command (eg a Unix <code>ifconfig</code>) against the server...'''
<source lang="powershell">
<source lang="powershell">
if ($SshClient.IsConnected) {
if ($SshClient.IsConnected) {
     $SshCommand = $SshClient.RunCommand("ifconfig")
     $SshCommand = $SshClient.RunCommand("ifconfig") # Result of 'ifconfig' is returned to $SshCommand
     $ifconfig = $SshCommand.Result.Split("`n")
     $ifconfig = $SshCommand.Result.Split("`n") # Split up the result into individual lines for easier parsing
}
}
</source>
</source>
Line 46: Line 49:
$SshClient.Dispose()
$SshClient.Dispose()
</source>
</source>
=== Download File ===
Simple example showing how to use PowerShell to open up a [[Acronyms#S|SFTP]] connection to a server, and download a file.
'''Create a SFTP Client object, and connect...'''
<source lang="powershell">
$ConnInfo = New-Object Renci.SshNet.PasswordConnectionInfo("ssh-server", 22, "user", "pass")           
$SftpClient = New-Object Renci.SshNet.SftpClient($ConnInfo)           
$SftpClient.Connect()
</source>
'''Create an IO stream to local file'''
<source lang="powershell">
$RxFile = "c:\temp\download.txt"
$RxFileStream = [System.IO.File]::Create($RxFile)
</source>
'''Download file''', in this case, an [[:Category:Apache|Apache]] log file
<source lang="powershell">
$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
</source>
'''Clear down...'''
<source lang="powershell">
$RxFileStream.Dispose()
$SftpClient.Disconnect()
$SftpClient.Dispose()
</source>


[[Category:PowerShell]]
[[Category:PowerShell]]
[[Category:SSH]]
[[Category:SSH]]

Navigation menu