darkoperator / darkoperator/Posh-SSH

Unusual inconsistent behavior

Open
#345 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PowerShell
Stars
1.1k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

My code is listed below. I have an unusual situation where all but one of the devices complete the script as expected. The one failing device executes the script, but the $stream.read() only returns about 20% of the output (configuration).

I have verify the steps at a Powershell command prompt and via two ssh clients. All tests have completed all devices as expected.

The "failing" device is somewhat consistent in its behavior. It is not the longest configuration, it's not the slowest connection, and I can find no other reason why it would behave as it does.

I'm looking for suggests or ideas to improve the script or he logic to address the issue.

// script follows

Import-Module Posh-SSH

$credential = Get-Credential

$elevatedPrompt = "#.$"
$configPrompt = "(config)#.$"
$objectPrompt = "object)#.$"

$backupDirectory = "D:\Patrick\Configs\With"

foreach ($device in Get-Content $args[0]) {
$session = New-SSHSession -Computername $device -Credential $credential -AcceptKey:$true -ConnectionTimeout 600

if ($session) {
	Write-Output "Backing up $device to $backupDirectory"
	$stream = New-SSHShellStream -SSHSession $session -Height 25000 -BufferSize 25000

	Invoke-SSHStreamShellCommand -ShellStream $stream -Command "terminal pager 0" -PrompPattern $elevatedPrompt | Out-Null
	$stream.Read() | Out-Null

	Invoke-SSHStreamShellCommand -ShellStream $stream -Command "show hostname" -PrompPattern $elevatedPrompt | Out-Null
	$CommandResult = $stream.read()
	$CommandLines = $CommandResult -split "`r`n"
	$hostname = $CommandLines[1]

	Invoke-SSHStreamShellCommand -ShellStream $stream -Command "more system:running-config" -PrompPattern $elevatedPrompt | Out-Null
	Start-Sleep -s 120
	$fullConfig = $stream.Read()
	Out-File -FilePath ($backupDirectory + $hostname + ".txt") -InputObject $fullConfig

	$stream.Close()
	Remove-SSHSession -SSHSession $session | Out-Null
	}
else {
	Write-Output "Unable to connect to $device - skipping"
	}

}

Write-Output ""
Write-Output ""

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the script's New-SSHShellStream, Invoke-SSHStreamShellCommand, and $stream.Read() calls, then reproduce the behavior against the failing device while comparing the returned output with the other devices. Done means identifying a consistent cause for the truncated configuration or documenting the missing information needed to diagnose it.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.