Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Invoke-SSHShellStreamCommand: Looking for a way to determine if a remote command has completed before executing Remove-SSHSession

Open RobatFTI opened this issue 6 years ago • 7 comments

I'm using Invoke-SSHShellStreamCommand to install SAMBA on some remote servers, here is a really simple code snippet that illustrates what I'm doing. This code does work, but it is not consistent or efficient since the SAMBA install may take more or less than 30 seconds to complete.

#---Begin-----------------------------------------------------------------------------# #-- Establish the Session and set up the stream. --# New-SSHSession -ComputerName SERVERFQDN -Port 22 -Credential MyCredObject -KeyFile TheKeyFile -ConnectionTimeout 120 -OperationTimeout 120 -AcceptKey $stream = New-SSHShellStream -Index 0

#-- Run the command --# Invoke-SSHStreamShellCommand -ShellStream $stream -Command "sudo yum install samba samba-client cifs-utils -y"

#-- 30 second pause before removing the session - this is the line I'd like to remove/replace: --# sleep -s 30

#-- Close the Session --# Remove-SSHSession -SessionId 0 #---End-------------------------------------------------------------------------------#

I'd like to eliminate the "sleep" line in this example. Is there any way to determine if the remote command executed using Invoke-SSHStreamCommand has completed before the Remove-SSHSession takes place.

Any suggestions or guidance you can provide would be appreciated.

Thanks!

-Rob

RobatFTI avatar Apr 18 '19 12:04 RobatFTI

Look for the command prompt, it should only appear at the end of execution

Sent from my iPhone

On Apr 18, 2019, at 8:46 AM, Rob Farkas [email protected] wrote:

I'm using Invoke-SSHShellStreamCommand to install SAMBA on some remote servers, here is a really simple code snippet that illustrates what I'm doing. This code does work, but it is not consistent or efficient since the SAMBA install may take more or less than 30 seconds to complete.

#---Begin-----------------------------------------------------------------------------#

Establish the Session and set up the stream.

New-SSHSession -ComputerName -Port 22 -Credential -KeyFile -ConnectionTimeout 120 -OperationTimeout 120 -AcceptKey $stream = New-SSHShellStream -Index 0

Run the command

Invoke-SSHStreamShellCommand -ShellStream $stream -Command "sudo yum install samba samba-client cifs-utils -y"

30 second pause before removing the session - this is the line I'd like to remove/replace:

sleep -s 30

Close the Session

Remove-SSHSession -SessionId 0 #---End-------------------------------------------------------------------------------#

I'd like to eliminate the "sleep" line in this example. Is there any way to determine if the remote command executed using Invoke-SSHStreamCommand has completed before the Remove-SSHSession takes place.

Any suggestions or guidance you can provide would be appreciated.

Thanks!

-Rob

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

darkoperator avatar Apr 18 '19 14:04 darkoperator

Thanks a lot - reading the stream in a while loop seems to have worked for this command, and this updated section of the script now functions as I'd expect: #---Begin-----------------------------------------------------------------------------# #-- Establish a variable for the current server hostname --# $curHostname = CurrentServerHostname

#-- Establish the Session and set up the stream. --# New-SSHSession -ComputerName SERVERFQDN -Port 22 -Credential MyCredObject -KeyFile TheKeyFile -ConnectionTimeout 120 -OperationTimeout 120 -AcceptKey $stream = New-SSHShellStream -Index 0

#-- Run the command --# Invoke-SSHStreamShellCommand -ShellStream $stream -Command "sudo yum install samba samba-client cifs-utils -y" $cmdOut = $stream.Read() while ($cmdOut -notlike "$curHostname ~]$") { Write-Host "Sleeping 1" Start-Sleep -s 1 $cmdOut = $stream.Read() }

#-- Close the Session --# Remove-SSHSession -SessionId 0 #---End-------------------------------------------------------------------------------#

However, I am seeing an issue when trying to read the stream after some other commands, for example this is a similar block of code that creates a directory on the remote server. This section of the script does not behave as I'd expect, and it appears the stream read is always "blank" in this case: #---Begin-----------------------------------------------------------------------------# #-- Establish a variable for the current server hostname --# $curHostname = CurrentServerHostname

#-- Establish the Session and set up the stream. --# New-SSHSession -ComputerName SERVERFQDN -Port 22 -Credential MyCredObject -KeyFile TheKeyFile -ConnectionTimeout 120 -OperationTimeout 120 -AcceptKey $stream = New-SSHShellStream -Index 0

#-- Run the commands --# Invoke-SSHStreamShellCommand -ShellStream $stream -Command "sudo yum install samba samba-client cifs-utils -y" $cmdOut = $stream.Read() while ($cmdOut -notlike "$curHostname ~]$") { Write-Host "Sleeping 1" Start-Sleep -s 1 $cmdOut = $stream.Read() }

Invoke-SSHStreamShellCommand -ShellStream $stream -Command "sudo mkdir /mnt/ScriptDir" $cmdOut = $stream.Read() Write-Host "Making the Directory" while ($cmdOut -notlike "$curHostname ~]$") { Write-Host "$cmdOut" Write-Host "Sleeping 1" Start-Sleep -s 1 $cmdOut = $stream.Read() }

#-- Close the Session --# Remove-SSHSession -SessionId 0 #---End-------------------------------------------------------------------------------#

When the mkdir code runs, the stream read never matches the command prompt string - writing it out to screen, it looks like the value for the stream read is always blank.

I'm not sure why this would be happening. Is there a reason why they stream read after the yum install would behave "correctly", and the stream read after the mkdir would remain blank?

Thanks again - any suggestions are appreciated.

-Rob

RobatFTI avatar Apr 19 '19 10:04 RobatFTI

uhmm Invoke-SSHStreamShellCommand already reads the output until it sees a prompt and returns it, why are you reading the stream separately? it even has a default regex for identifying the prompt.

darkoperator avatar May 05 '19 00:05 darkoperator

@darkoperator , could you help me figuring out the Invoke-SSHStreamShellCommand. There are no examples nor detailed documentation on this command, and I cant figure out why it does not catch the prompt. Thanks!

AnythingOverIP avatar Jan 13 '20 00:01 AnythingOverIP

Against what type of device are you running it against? I tested it against Busybox, CentOS and Ubuntu.

On Jan 12, 2020, at 8:08 PM, thefreakquency [email protected] wrote:

@darkoperator https://github.com/darkoperator , could you help me figuring out the Invoke-SSHStreamShellCommand. There are no examples nor detailed documentation on this command, and I cant figure out why it does not catch the prompt. Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/darkoperator/Posh-SSH/issues/268?email_source=notifications&email_token=AAD7IHT3HRQSALNF22H4OKLQ5OWJPA5CNFSM4HG4SDZ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIXIGWI#issuecomment-573473625, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD7IHUTWPAXEX3M3JNUS63Q5OWJPANCNFSM4HG4SDZQ.

darkoperator avatar Jan 13 '20 00:01 darkoperator

It's aggainst a set of Motorola's GGM8000 router. Probably not working due to numbering. Each console prompt is numbered:

[1]hostname_here# set sl = n
[2]hostname_here# cd
Current file source changed to A:/primary/
[3]hostname_here# sh -sys sm
...
[11]hostname_here# sh -po vp

In the meantime, while using the Invoke-SSHStreamShellCommand I added a stop-wait to ensure it's all good... just takes longer.

AnythingOverIP avatar Jan 13 '20 16:01 AnythingOverIP

In that case you will need to play with the promp regex in the promppatter option to one that will match that unique one. Yes do need to document better that one

Sent from my iPhone

On Jan 13, 2020, at 12:58 PM, thefreakquency [email protected] wrote:

 It's aggainst a set of Motorola's GGM8000 router. Probably not working due to numbering. Each console prompt is numbered:

[1]hostname_here# set sl = n [2]hostname_here# cd Current file source changed to A:/primary/ [3]hostname_here# sh -sys sm ... [11]hostname_here# sh -po vp

In the meantime, while using the Invoke-SSHStreamShellCommand I added a stop-wait to ensure it's all good... just takes longer.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

darkoperator avatar Jan 13 '20 19:01 darkoperator