SSH fun!

This topic contains 7 replies, has 4 voices, and was last updated by  Cody Everingham 1 week ago.

  • Author
    Posts
  • #81938

    Cody Everingham
    Participant

    CODE:
    Install-packageprovider -name NuGet -MinimumVersion 2.8.5.201 -force
    Install-Module Posh-SSH -Force
    $Username = 'User'
    $Password = 'Password'
    $pass = ConvertTo-SecureString -AsPlainText $Password -Force
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
    New-SSHSession 192.168.1.253 -Credential $Cred -AcceptKey
    Invoke-SSHCommand -Index 0 -Command "show time"
    Remove-SSHSession 0
    RESULT:
    Exception calling "EndExecute" with "1" argument(s): "Command 'show time' has timed out."

    I've looked everywhere an I can't seem to find out why I'm running into this error. The ssh command is running on a sonicwall and it isn't prompting the user so it should just run. Hmm, I'm lost.

  • #81946

    Sam Boutros
    Participant

    I don't know if 'show time' is valid linux command

    This is what I usually use:

    Install-Module SB-Tools,POSH-SSH -Force 
    $IPv4Address = '192.168.1.253'
    $Cred = Get-SBCredential -UserName 'User'
    
    $mySession = New-SSHSession -ComputerName $IPv4Address -Credential $Cred -AcceptKey
    Invoke-SSHCommand -SessionId $mySession.SessionId -Command 'date'
    

    with output like

    Host       : 192.168.1.253
    Output     : {Thu Oct 12 16:35:15 CDT 2017}
    ExitStatus : 0
    
  • #81964

    Cody Everingham
    Participant

    Thanks for the input, and correct "show time" is not a linux command to my knowledge. this is running on a sonicwall firewall.

  • #81980

    Max Kozlov
    Participant

    Posh-SSH Author recommend to use SSHStream for exotic devices...

  • #82012

    Steve
    Participant

    This is how I got the streaming part to work from Posh-ssh

                  $newSSH = New-Session -ComputerName $address -Credential $Creds -AcceptKey
                   If ($newSSH){
                       $SSHSession - Get-Session $newSSH
                       $Global:SSHStream = $SSHSession.Session.CreateShellStream("PS-SSH", 0, 0, 0, 0, 1000)
                       Start-Sleep -Milliseconds 500
                       $SSHStream.read()
              }
              Catch [Exception] {
                  $Error
                  }
    

    From there you can enter commands like this:

         $SSHStream.WriteLine(“show time”)
         Start-Sleep -Milliseconds 500
         $SSHStream.read()
    
  • #82025

    Cody Everingham
    Participant

    Thanks for the input! I think this is the right track for sure and this is most likely an easy error to overcome, just cant seem to get past it: "Get-SSHSession : Cannot bind positional parameters because no names were given." Thanks again for the help

     
    if ((get-packageprovider).name -notcontains "NUGet") {Install-packageprovider -name NuGet -MinimumVersion 2.8.5.201 -force}
    if ((get-module).name -notcontains "Posh-SSH") {Install-Module Posh-SSH -Force}
    $Username = 'User'
    $pass = ConvertTo-SecureString -AsPlainText 'Password' -Force
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
    $newSSH = (New-SSHSession 192.168.1.253 -Credential $Cred -AcceptKey)
    If ($newSSH){
        $SSHSession = Get-SSHSession $newSSH
        $Global:SSHStream = $SSHSession.Session.CreateShellStream("show time", 0, 0, 0, 0, 1000)
        Start-Sleep -Milliseconds 500
        $SSHStream.read()
    } else {Write-Output "No SSH Session"}
    
  • #82028

    Steve
    Participant

    Not sure if line 7 needs to be in parens. ( )

    Possibly this may help, I have it at the start of my function:

       function Start-SSH {
            [cmdletbinding(SupportsShouldProcess=$true)]
            Param(
                [ValidateNotNull()]
                [System.Management.Automation.PSCredential]
                [System.Management.Automation.Credential()]
                $RCred = $syncHash.host.ui.PromptForCredential('Router Logon','Provide Router Username and Password','',''),
                
                [ValidateNotNull()]
                [System.Management.Automation.PSCredential]
                [System.Management.Automation.Credential()]
                $REnable = $syncHash.host.ui.PromptForCredential('Router Enable','Provide Enable Password','','')
            )
    
    

    If you do not use elevated privs, remove the second credentials argument.

    Then, simply call $RCred in the New-SSHSession command:

         $newSSH = New-SSHSession -ComputerName $IPAddress -Credential $RCred -AcceptKey
    

    In retrospect I may have opened a whole extra can of worms – you may need to remove the "$syncHash." element
    I have a global that sets host

    $syncHash.host = $host

  • #82204

    Cody Everingham
    Participant

    Thanks again, no luck. I'm going to try a third part program (pling in this case). to automate the ssh request. Ill update this afterwords.

You must be logged in to reply to this topic.