PoshCode Archive  Artifact Content

Artifact 09c7e24e918d41469884dd81c957c868916f4611b729e3235c2f1e3f5e9f6986:

  • File SharpSsh-Functions.ps1 — part of check-in [c64fa71706] at 2018-06-10 13:33:31 on branch trunk — I’ve tweaked New-SshSession to read the default prompt, means you can just New-SshSession; Invoke-Ssh ls … and go on with life. (user: Joel Bennett size: 4130)

# encoding: ascii
# api: powershell
# title: SharpSsh Functions
# description: I’ve tweaked New-SshSession to read the default prompt, means you can just New-SshSession; Invoke-Ssh ls … and go on with life.
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: New-SshSession
# x-poshcode-id: 3948
# x-archived: 2013-02-19T01:29:34
# x-published: 2013-02-14T15:27:00
#
# 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.
#
## USING the binaries from:
## http://downloads.sourceforge.net/sharpssh/SharpSSH-1.1.1.13.bin.zip
[void][reflection.assembly]::LoadFrom( (Resolve-Path "~\Documents\WindowsPowerShell\Libraries\Tamir.SharpSSH.dll") )

## NOTE: These are bare minimum functions, and only cover ssh, not scp or sftp
##       also, if you "expect" something that doesn't get output, you'll be completely stuck.
##
## As a suggestion, the best way to handle the output is to "expect" your prompt,  and then do 
## select-string matching on the output that was captured before the prompt.

function New-SshSession {
Param( $RSAKeyFile, [switch]$Passthru)
   $cred = $host.UI.PromptForCredential("SSH Login Credentials",
                                        "Please specify credentials in user@host format",
                                        "$UserName@$HostName","")
                                           
   if($RSAKeyFile -and (Test-Path $RSAKeyFile)){
      $global:LastSshSession = new-object Tamir.SharpSsh.SshShell `
                                          $cred.GetNetworkCredential().Domain, 
                                          $cred.GetNetworkCredential().UserName
      $global:LastSshSession.AddIdentityFile( (Resolve-Path $RSAKeyFile) )
   }
   else {
      $global:LastSshSession = new-object Tamir.SharpSsh.SshShell `
                                          $cred.GetNetworkCredential().Domain, 
                                          $cred.GetNetworkCredential().UserName,
                                          $cred.GetNetworkCredential().Password
   }

   $global:LastSshSession.Connect()
   $global:LastSshSession.RemoveTerminalEmulationCharacters = $true
   if($Passthru) {
      return $global:LastSshSession
   }
   
   $global:LastSshSession.WriteLine("")
   sleep -milli 500
   $global:defaultPrompt = [regex]::Escape( $global:LastSshSession.Expect().Split("`n")[-1] )
}

function Remove-SshSession {
Param([Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession)
   $SshShell.WriteLine( "exit" )
   sleep -milli 500
   if($SshShell.ShellOpened) { Write-Warning "Shell didn't exit cleanly, closing anyway." }
   $SshShell.Close()
   $SshShell = $null
}

function Invoke-Ssh {
Param(
   [string]$command
,  [regex]$expect = $global:defaultPrompt## there ought to be a non-regex parameter set...
,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
)

   if($SshShell.ShellOpened) {
      $SshShell.WriteLine( $command )
      if($expect) {
         $SshShell.Expect( $expect ).Split("`n")
      }
      else {
         sleep -milli 500
         $SshShell.Expect().Split("`n")
      }
   }
   else { throw "The ssh shell isn't open!" } 
}

function Send-Ssh {
Param(
   [string]$command
,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
)

   if($SshShell.ShellOpened) {
      $SshShell.WriteLine( $command )
   }
   else { throw "The ssh shell isn't open!" } 
}


function Receive-Ssh {
Param(
   [RegEx]$expect ## there ought to be a non-regex parameter set...
,  [Tamir.SharpSsh.SshShell]$SshShell=$global:LastSshSession
)
   if($SshShell.ShellOpened) {
      if($expect) {
         $SshShell.Expect( $expect ).Split("`n")
      }
      else {
         sleep -milli 500
         $SshShell.Expect().Split("`n")
      }
   }
   else { throw "The ssh shell isn't open!" } 
}