PoshCode Logo PowerShell Code Repository

SharpSsh Functions by Joel Bennett 36 months ago
View followups from Joel Bennett | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/877"></script>download | new post

A few wrapper functions to make working with the SSH portion of SharpSSH easier: New-SshSession, Invoke or Send Ssh commands, Receive output, all with support for “Expect” ... which means we’ll continue reading from the ssh output until we see the expected output, and then stop.

  1. ## USING the binaries from:
  2. ## http://downloads.sourceforge.net/sharpssh/SharpSSH-1.1.1.13.bin.zip
  3. [void][reflection.assembly]::LoadFrom( (Resolve-Path "~\Documents\WindowsPowerShell\Libraries\Tamir.SharpSSH.dll") )
  4.  
  5. ## NOTE: These are bare minimum functions, and only cover ssh, not scp or sftp
  6. ##       also, if you "expect" something that doesn't get output, you'll be completely stuck.
  7. ##
  8. ## As a suggestion, the best way to handle the output is to "expect" your prompt,  and then do
  9. ## select-string matching on the output that was captured before the prompt.
  10.  
  11. function New-SshSession {
  12. Param(
  13.    [string]$UserName
  14. ,  [string]$HostName
  15. ,  [string]$RSAKeyFile
  16. ,  [switch]$Passthru
  17. )
  18.    if($RSAKeyFile -and (Test-Path $RSAKeyFile)){
  19.       $global:LastSshSession = new-object Tamir.SharpSsh.SshShell `
  20.                                           $cred.GetNetworkCredential().Domain,
  21.                                           $cred.GetNetworkCredential().UserName
  22.       $global:LastSshSession.AddIdentityFile( (Resolve-Path $RSAKeyFile) )
  23.    }
  24.    else {
  25.       $cred = $host.UI.PromptForCredential("SSH Login Credentials",
  26.                                            "Please specify credentials in user@host format",
  27.                                            "$UserName@$HostName","")
  28.       $global:LastSshSession = new-object Tamir.SharpSsh.SshShell `
  29.                                           $cred.GetNetworkCredential().Domain,
  30.                                           $cred.GetNetworkCredential().UserName,
  31.                                           $cred.GetNetworkCredential().Password
  32.    }
  33.  
  34.  
  35.    $global:LastSshSession.Connect()
  36.    $global:LastSshSession.RemoveTerminalEmulationCharacters = $true
  37.    if($Passthru) {
  38.       return $global:LastSshSession
  39.    }
  40. }
  41.  
  42. function Remove-SshSession {
  43. Param([Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession)
  44.    $SshShell.WriteLine( "exit" )
  45.    sleep -milli 500
  46.    if($SshShell.ShellOpened) { Write-Warning "Shell didn't exit cleanly, closing anyway." }
  47.    $SshShell.Close()
  48.    $SshShell = $null
  49. }
  50.  
  51. function Invoke-Ssh {
  52. Param(
  53.    [string]$command
  54. ,  [regex]$expect ## there ought to be a non-regex parameter set...
  55. ,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
  56. )
  57.  
  58.    if($SshShell.ShellOpened) {
  59.       $SshShell.WriteLine( $command )
  60.       if($expect) {
  61.          $SshShell.Expect( $expect ).Split("`n")
  62.       }
  63.       else {
  64.          sleep -milli 500
  65.          $SshShell.Expect().Split("`n")
  66.       }
  67.    }
  68.    else { throw "The ssh shell isn't open!" }
  69. }
  70.  
  71. function Send-Ssh {
  72. Param(
  73.    [string]$command
  74. ,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
  75. )
  76.  
  77.    if($SshShell.ShellOpened) {
  78.       $SshShell.WriteLine( $command )
  79.    }
  80.    else { throw "The ssh shell isn't open!" }
  81. }
  82.  
  83.  
  84. function Receive-Ssh {
  85. Param(
  86.    [RegEx]$expect  ## there ought to be a non-regex parameter set...
  87. ,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
  88. )
  89.    if($SshShell.ShellOpened) {
  90.       if($expect) {
  91.          $SshShell.Expect( $expect ).Split("`n")
  92.       }
  93.       else {
  94.          sleep -milli 500
  95.          $SshShell.Expect().Split("`n")
  96.       }
  97.    }
  98.    else { throw "The ssh shell isn't open!" }
  99. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me