Skip to content

Instantly share code, notes, and snippets.

@darkoperator
Created June 23, 2017 11:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkoperator/93fc457ca1c2de14c3a62a55d98020eb to your computer and use it in GitHub Desktop.
Save darkoperator/93fc457ca1c2de14c3a62a55d98020eb to your computer and use it in GitHub Desktop.
Function for Interacting with VyOS using Posh-SSH
<#
.Synopsis
Execute commands against VyOS.
.DESCRIPTION
Execute commands against VyOS using a SSHShellStream.
#>
function Invoke-VyOSCommand
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param (
# SSH stream to use for command execution.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[Renci.SshNet.ShellStream]
$Stream,
# Command to execute on VyOS router.
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[string]
$Command
)
Begin {
$promptRegEx = [regex]'[\$%#>] $'
}
Process {
# Discard any banner or previous command output
do {
$stream.read() | Out-Null
} while ($stream.DataAvailable)
$stream.writeline($Command)
#discard line with command entered
$Stream.ReadLine() | Out-Null
Start-Sleep -Milliseconds 500
$out = ''
# read all output until there is no more
do {
$out += $stream.read()
} while ($stream.DataAvailable)
$outputLines = $out.Split("`n")
foreach ($line in $outputLines) {
if ($line -notmatch $promptRegEx) {
$line
}
}
}
End{}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment