SSH From Windows Server to Linux Server - Invoke-SSHCommand

This topic contains 1 reply, has 1 voice, and was last updated by Profile photo of Tim Bolton Tim Bolton 1 year, 8 months ago.

  • Author
    Posts
  • #31930
    Profile photo of Tim Bolton
    Tim Bolton
    Participant

    I am using an SSH Module – https://github.com/darkoperator/Posh-SSH – to connect to a Linux Server, then attempting to run curl -v : against both Windows Target Servers and Linux Target Servers.

    The purpose of this is to test, as needed, if Linux Job Servers can connect via Port 4750 to Target Servers running both Windows & Linux OS...

    I am trying to create this simple test using PowerShell which will be made available to all users via a Web Console. This way if they are having issues they can ensure that the Linux Job Servers can connect to the Agent via assigned Port 4750.

    I first set my credentials-

    PS C:\>$Global:Cred = ( Get-Credential -Message "(Windows) AD Credential are required for access" -UserName ([Environment]::UserDomainName + {\} + [Environment]::UserName))
    
    
    

    Then I create a new SSH session to a Linux Server (One of the Linux Job Servers) –

    $LinuxServer = 'LinuxServerA'
    
    $IndexID = New-SSHSession -ComputerName $LinuxServer -Credential $Cred
    
    SessionId  Host                                                      Connected
    ---------        ----                                                         ---------
        2          LinuxServerA                                           True
    
    
    

    When I run a curl -v command against a Windows Target Server I get the expected results-

    PS C:\> Invoke-SSHCommand $IndexID.SessionID -command "curl -v WinServerA:4750"
    
    Host       : WinServerA
    Output     : {00000006-1;7530000001180;65;0;850;850;0}
    ExitStatus : 0
    
    
    

    But when I run the same curl -v command against a Linux Target Server LinuxServerB I am getting these results –

    PS C:\Users\TIM> Invoke-SSHCommand $IndexID.SessionID -command "curl -v LinuxServerB:4750"
    
    
    Host       : LinuxServerB
    Output     : {}
    ExitStatus : 52
    
    
    

    When I run the same curl -v command while connected to LinuxServerA via a Putty Session I get these results –

    [u1234567@LinuxServerA ~]$ curl -v WindowsServerA:4750
    * About to connect() to WindowsServerA port 4750
    *   Trying 10.10.10.10... connected
    * Connected to WindowsServerA (10.10.10.10) port 4750
    > GET / HTTP/1.1
    > User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
    > Host: WindowsServerA:4750
    > Accept: */*
    >10.97.69.44
    Connection #0 to host WindowsServerA left intact
    * Closing connection #0
    00000006-1;7530000001180;65;0;850;850;0[u1234567@LinuxServerA ~]$
    
    [u1234567@LinuxServerA ~]$ curl -v LinuxServerB:4750
    * About to connect() to LinuxServerB port 4750
    *   Trying 10.10.10.11... connected
    * Connected to LinuxServerB (10.10.10.11) port 4750
    > GET / HTTP/1.1
    > User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
    > Host: LinuxServerB:4750
    > Accept: */*
    >
    * Empty reply from server
    * Connection #0 to host LinuxServerB left intact
    curl: (52) Empty reply from server
    * Closing connection #0
    
    [u1234567@LinuxServerA ~]$
    
    
    

    I also tried using telnet within the command curl -v telnet://servername:port, again via a Putty Session.

    [u1234567@LinuxServerA ~]$ curl -v telnet://WindowsServerA:4750
    * About to connect() to WindowsServerA port 4750
    *   Trying 10.10.10.10... connected
    * Connected to WindowsServerA (10.10.10.10) port 4750
    
    * Closing connection #0
    00000006-1;7530000001180;65;0;850;850;0[u1234567@LinuxServerA ~]$
    
    [u1234567@LinuxServerA ~]$ curl -v telnet://LinuxServerB:4750
    * About to connect() to LinuxServerB port 4750
    *   Trying 10.10.10.11... connected
    * Connected to LinuxServerB (10.10.10.11) port 4750
    
    * Closing connection #0
    [u1234567@LinuxServerA ~]$
    
    
    

    However, when I try this using the SSH Session it will timeout.

    PS C:\> Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://WindowsServerA:4750"
    Exception calling "EndExecute" with "1" argument(s): "Command 'curl -v telnet://WindowsServerA:4750' has timed out."
    At C:\Users\TIM\Documents\WindowsPowerShell\Modules\Posh-SSH\Posh-SSH.psm1:265 char:25
    +                         $Output = $_.cmd.EndExecute($_.Async)
    +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SshOperationTimeoutException
    
    Host       : LinuxServerA
    Output     : {}
    ExitStatus : 0
    
    
    PS C:\> Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://LinuxServerB:4750"
    Exception calling "EndExecute" with "1" argument(s): "Command 'curl -v telnet://LinuxServerB:4750' has timed out."
    At C:\Users\TIM\Documents\WindowsPowerShell\Modules\Posh-SSH\Posh-SSH.psm1:265 char:25
    +                         $Output = $_.cmd.EndExecute($_.Async)
    +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SshOperationTimeoutException
    
    Host       : LinuxServerA
    Output     : {}
    ExitStatus : 0
    
    
    

    If there is a better SSH Module or Method to complete this I am completely open to trying them. I just do not understand why this is not working...

  • #31955
    Profile photo of Tim Bolton
    Tim Bolton
    Participant

    Thanks to my brilliant co-worker Ramesh Podila, we have sorted this out...

    I now have two methods to use. One using the & sleep 2; kill $! and another redirecting stderr to stdout — 2>&1 & sleep 2; kill $!

    Using 2 seconds seems to work for all of the test servers which is why it is being used.

    
    PS C:\> Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://WindowsServerA:4750& sleep 2; kill $!"
    
    
    Host       : LinuxServerA
    Output     : {}
    ExitStatus : 0
    
    
    
    PS C:\> Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://LinuxServerB:4750& sleep 2; kill $!
    "
    
    
    Host       : LinuxServerA
    Output     : {}
    ExitStatus : 0
    
    
    
    PS C:\> Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://WindowsServerA:4750 2>&1 & sleep 2; kill $!"
    
    
    Host       : LinuxServerA
    Output     : {* About to connect() to WindowsServerA port 4750, *   Trying 10.10.10.10... connected, * Connected to
                 WindowsServerA (10.10.10.10) port 4750}
    ExitStatus : 0
    
    
    
    PS C:\> Invoke-SSHCommand $IndexID.SessionID -command "curl -v telnet://LinuxServerB:4750 2>&1 & sleep 2; kill $!"
    
    
    Host       : LinuxServerA
    Output     : {* About to connect() to LinuxServerB port 4750, *   Trying 10.10.10.11... connected, * Connected to
                 LinuxServerB (10.10.10.11) port 4750}
    ExitStatus : 0
    
    

    Now I can create my Function with all of the Linux Job Servers that will be used to test Port 4750 connectivity to both Windows and Linux Target Servers.

You must be logged in to reply to this topic.