Created
June 23, 2017 11:12
-
-
Save darkoperator/93fc457ca1c2de14c3a62a55d98020eb to your computer and use it in GitHub Desktop.
Function for Interacting with VyOS using Posh-SSH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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