Created
June 22, 2018 20:56
-
-
Save JustinGrote/25ded39e7365515b8940c8c5e5febb19 to your computer and use it in GitHub Desktop.
Get the running configuration from a network device using Powershell. Supports Cisco and JunOS
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
| #requires -module Posh-SSH | |
| param ( | |
| [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)][String]$Computername, | |
| [ValidateSet("Cisco","JunOS")] | |
| [Parameter(ValueFromPipelineByPropertyName)][String]$DeviceType = "Cisco", | |
| [String]$RepositoryPath = $home\desktop, | |
| [PSCredential]$Credential | |
| ) | |
| begin { | |
| function Invoke-CiscoCommand { | |
| <# | |
| .SYNOPSIS | |
| Issues a Cisco command and returns the result | |
| #> | |
| [Cmdletbinding(SupportsShouldProcess)] | |
| param ( | |
| #The Cisco command to run, e.g. "show version" | |
| [parameter(Mandatory)]$Command, | |
| #How long to wait for the command to complete, in seconds. Default is 5 seconds. | |
| [int]$Timeout = 5 | |
| ) | |
| [timespan]$timeout = new-timespan -seconds ($timeout) | |
| [regex]$ciscoEnablePrompt = '([\w-]+)#$' | |
| $shell.writeline($Command) | |
| $expectResult = $shell.expect($ciscoEnablePrompt,$timeout) | |
| if ($expectResult) { | |
| #Comes back as a character array, make it into lines so we can use it | |
| $expectresult = $expectResult -split '\r\n' | |
| #Discard the first line as it's just an echo of our command | |
| $firstLineResult = $expectresult[0] | |
| if ($firstLineResult -notmatch $Command) {write-error "Shell did not echo back the command we sent, this is probably a bug"; continue} | |
| $expectresult = $expectResult | Select-Object -skip 1 | |
| #Discard the last line (shell prompt) | |
| $expectresult = $expectResult | Select-Object -skiplast 1 | |
| #Output the result | |
| if (-not $expectResult) {write-verbose "Command $Command completed successfully"} else {$expectResult} | |
| } else { | |
| write-debug "Failed Command $shellCommand Result: $expectResult" | |
| write-error "No enable prompt returned after $($timeout.totalseconds) seconds, the command likely failed, paging wasn't disabled. Consider increasing the timeout command" | |
| continue | |
| } | |
| } | |
| } | |
| process { | |
| write-verbose "Getting running config for $computername" | |
| write-progress "Getting running config for $computername" | |
| #Build SSH session | |
| $sshSession = New-SSHSession -ComputerName $computername -Credential $credential -AcceptKey | |
| if ($deviceType -match 'Cisco') { | |
| [regex]$ciscoEnablePrompt = '([\w-]+)#$' | |
| $timeout = new-timespan -seconds 5 | |
| $shell = New-SSHShellStream $sshsession | |
| $ciscoPromptResult = $shell.expect($ciscoEnablePrompt,$timeout) | |
| if ($ciscoPromptResult -match $ciscoEnablePrompt) { | |
| write-verbose "This is a privileged session, skipping enable step" | |
| $deviceName = $matches[1]} | |
| else { | |
| write-error "Didn't get a privileged prompt. TODO: Enable logic" | |
| continue | |
| } | |
| Invoke-CiscoCommand "terminal length 0" | |
| $result = Invoke-CiscoCommand "show running-config" -timeout 30 | |
| } | |
| if ($deviceType -match 'JunOS') { | |
| $null = invoke-sshcommand -command 'set cli screen-length 0' -sshsession $sshSession | |
| $null = invoke-sshcommand -command 'set cli screen-width 0' -sshsession $sshSession | |
| $result = (invoke-sshcommand -command 'show configuration | display omit' -sshsession $sshSession).output | |
| #This regex extracts the hostname from a configuration | |
| $junOSDeviceNameRegex = '^\ +host-name\ ([\w-]+);$' | |
| #TODO: CHANGE ME | |
| $devicename = ($result | where {$PSItem -match $junOSDeviceNameRegex}) -replace $junOSDeviceNameRegex,'$1' | |
| } | |
| $deviceConfigPath = Join-Path $repositorypath "$deviceName`_$(get-date -format 'yyyyMMdd-HHmmss').config" | |
| if ($result) { | |
| write-verbose "Saving config to $deviceConfigPath" | |
| $result > $deviceConfigPath | |
| } else { | |
| write-error "Unable to retrieve config for $deviceName ($computername)" | |
| continue | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Justin
What do you mean by "TODO: Enable Legic"