Automation

 View Only
Expand all | Collapse all

Get output from New-SSHShellStream in an ESXi shell session

  • 1.  Get output from New-SSHShellStream in an ESXi shell session

    Posted Dec 16, 2020 08:19 PM

    Given the following code:

    #Start SSH session
    $ssh = New-SSHSession -ComputerName $lvhost -Credential $hostCreds -AcceptKey
    Start-Sleep -Seconds 5

    #Send commands to esx
    $SSHStream = New-SSHShellStream -Index $ssh.SessionId
    $SSHStream.WriteLine("cd /scratch/log")
    $SSHStream.WriteLine("pwd")
    Start-Sleep -seconds 10
    $esxout = $SSHStream.read()
    $esxout
    $sshstream
    break

    I need a simple way to see that the ("cd /scratch/log") actually worked. When I do the $esxout=$SSHStream.read() I get ALL the output including the banner and all the commands I typed. I just want to see that the "pwd" command gives me the current directory after the "cd" command and I can make sure the "cd" command worked.

    I guess I could parse all output and do a substring?

    Ty.



  • 2.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Dec 16, 2020 08:43 PM

    A stream can be compared with a continuous stream of characters, so you have to make sure you read everything up to the point where you want to capture the output.
    I normally use a Read which I throw away up to the point where I send the command whose output I want to capture.
    For me it works with a single ReadLine just after the 'pwd', some Linux type tty echo the commands as well.
    So there it would be your 2nd ReadLine() that returns the output you want to capture

     



  • 3.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Dec 16, 2020 09:06 PM

     

    I knew I had to separate the commands out somehow but just couldn't picture it. This makes total sense! Thank you for the quick

    response.

     

    ...alan



  • 4.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Dec 16, 2020 09:55 PM

    Hmmm,

    The output I get is just

    pwd

    Even tried a sleep to let it work...

    #Send commands to esx
    $SSHStream = New-SSHShellStream -Index $ssh.SessionId
    $SSHStream.WriteLine("cd /scratch/log")
    $SSHStream.read() | out-null
    $SSHStream.WriteLine("pwd")
    Start-Sleep 10
    $esxout = $SSHStream.readline()
    $esxout

    break



  • 5.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Dec 17, 2020 06:31 AM

    I forgot an extra readline(), the 1st one reads the echo of the pws command.



  • 6.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Dec 17, 2020 06:32 AM

    stream.png



  • 7.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Feb 02, 2023 10:09 AM

    Hi LucD,

    I need some help on the below as I am trying to capture the output of the command to a variable but every time it is coming as blank. Please advise.

    #Start SSH session
    $ssh = New-SSHSession -ComputerName $esx -Credential $hostCreds -AcceptKey

    #Send commands to esx
    $SSHStream = New-SSHShellStream -Index $ssh.SessionId
    $SSHStream.WriteLine("vmware -v")
    $output = $SSHStream.read()

    Write-Host $output

     



  • 8.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Feb 02, 2023 11:37 AM

    Working with the Stream is tricky, since you get everything back that is echoed to the console.
    It does require that you know exactly what is being produced, so you filter out the unnecessary lines.

    In your case, this should do the trick


    But why use the stream in this case?
    This can be done, much simpler with an Invoke-SSHCommand.



  • 9.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Feb 02, 2023 12:10 PM

    Thanks a lot LucD. 

    Actually there is an requirement to gather some information from Cisco UCS fabric Interconnect and I am using Posh-SSH to achieve it.

    However I am not able to run any command using Invoke-SSHCommand. It is hanging but New-SSHShellStream is running properly.

    I will try the code you shared and let you know the result. Thanks Again.



  • 10.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Feb 02, 2023 01:21 PM

    No LucD. I am still not getting the output.



  • 11.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Feb 02, 2023 01:25 PM

    Works for me. This is what is returned


    LucD_0-1675344306748.png

    How do you run the script?
    Is it exactly the same as I posted?



  • 12.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Feb 02, 2023 02:26 PM

    As mentioned I am using posh-SSH to connect my UCS fabric-interconnect (switch) over SSH and run the command to check the uptime of the IOM modules. The 1st command is "connect iom 1" which will be the below output from which I need only the "Attaching to FEX 1 ..." portion.

    Attaching to FEX 1 ...
    To exit type 'exit', to abort type '$.'
    Bad terminal type: "dumb". Will assume vt100.
    fex-1#

    2nd command is "sh system uptime" and it gives the following output:

    System start time: Sat Sep 12 12:45:21 2021
    System uptime: 101 days, 0 hours, 11 minutes, 32 seconds
    Kernel uptime: 101 days, 0 hours, 22 minutes, 11 seconds
    fex-1#

    I need the 2 lines in the middle to be saved on variable from the above output

    Lastly exit command to come out from fex-1# prompt

    This way it should print for the uptime of all 10 IOM modules present in the UCSM. 

       



  • 13.  RE: Get output from New-SSHShellStream in an ESXi shell session

    Posted Feb 02, 2023 02:35 PM

    Sorry, I don't have access to a UCS platform.

    You will have to experiment yourself I'm afraid