PoshCode Logo PowerShell Code Repository

SharpSsh Functions (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1010"></script>download | new post

I’ve tweaked New-SshSession to read the default prompt, means you can just New-SshSession; Invoke-Ssh ls … and go on with life.

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( $RSAKeyFile, [switch]$Passthru)
  13.    $cred = $host.UI.PromptForCredential("SSH Login Credentials",
  14.                                         "Please specify credentials in user@host format",
  15.                                         "$UserName@$HostName","")
  16.                                            
  17.    if($RSAKeyFile -and (Test-Path $RSAKeyFile)){
  18.       $global:LastSshSession = new-object Tamir.SharpSsh.SshShell `
  19.                                           $cred.GetNetworkCredential().Domain,
  20.                                           $cred.GetNetworkCredential().UserName
  21.       $global:LastSshSession.AddIdentityFile( (Resolve-Path $RSAKeyFile) )
  22.    }
  23.    else {
  24.       $global:LastSshSession = new-object Tamir.SharpSsh.SshShell `
  25.                                           $cred.GetNetworkCredential().Domain,
  26.                                           $cred.GetNetworkCredential().UserName,
  27.                                           $cred.GetNetworkCredential().Password
  28.    }
  29.  
  30.    $global:LastSshSession.Connect()
  31.    $global:LastSshSession.RemoveTerminalEmulationCharacters = $true
  32.    if($Passthru) {
  33.       return $global:LastSshSession
  34.    }
  35.    
  36.    $global:LastSshSession.WriteLine("")
  37.    sleep -milli 500
  38.    $global:defaultPrompt = [regex]::Escape( $global:LastSshSession.Expect().Split("`n")[-1] )
  39. }
  40.  
  41. function Remove-SshSession {
  42. Param([Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession)
  43.    $SshShell.WriteLine( "exit" )
  44.    sleep -milli 500
  45.    if($SshShell.ShellOpened) { Write-Warning "Shell didn't exit cleanly, closing anyway." }
  46.    $SshShell.Close()
  47.    $SshShell = $null
  48. }
  49.  
  50. function Invoke-Ssh {
  51. Param(
  52.    [string]$command
  53. ,  [regex]$expect = $global:defaultPrompt## there ought to be a non-regex parameter set...
  54. ,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
  55. )
  56.  
  57.    if($SshShell.ShellOpened) {
  58.       $SshShell.WriteLine( $command )
  59.       if($expect) {
  60.          $SshShell.Expect( $expect ).Split("`n")
  61.       }
  62.       else {
  63.          sleep -milli 500
  64.          $SshShell.Expect().Split("`n")
  65.       }
  66.    }
  67.    else { throw "The ssh shell isn't open!" }
  68. }
  69.  
  70. function Send-Ssh {
  71. Param(
  72.    [string]$command
  73. ,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
  74. )
  75.  
  76.    if($SshShell.ShellOpened) {
  77.       $SshShell.WriteLine( $command )
  78.    }
  79.    else { throw "The ssh shell isn't open!" }
  80. }
  81.  
  82.  
  83. function Receive-Ssh {
  84. Param(
  85.    [RegEx]$expect ## there ought to be a non-regex parameter set...
  86. ,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
  87. )
  88.    if($SshShell.ShellOpened) {
  89.       if($expect) {
  90.          $SshShell.Expect( $expect ).Split("`n")
  91.       }
  92.       else {
  93.          sleep -milli 500
  94.          $SshShell.Expect().Split("`n")
  95.       }
  96.    }
  97.    else { throw "The ssh shell isn't open!" }
  98. }

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