Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Requires Posh-SSH module
- $ip = Read-Host "Enter device IP"
- #----------------------+
- # Get Conf from Device |
- #----------------------+
- do
- {
- $c = 0
- $credentials = Get-Credential
- try
- {
- $c = 1
- $Session = New-SSHSession -ComputerName $ip -Credential $credentials -AcceptKey -Force -ErrorAction Stop
- }
- Catch
- {
- Write-Host -ForegroundColor Red "Wrong credentials"
- $c = 0
- }
- } until ($c -eq 1)
- $stream = $Session.Session.CreateShellStream("Cisco", 0, 0, 0, 0, 10000)
- $stream.Write("term len 0`r")
- Start-Sleep 1
- $stream.write("sh run`r")
- Write-Host -ForegroundColor Yellow "Building conf"
- Start-Sleep 12
- $output = $stream.Read()
- Write-Host -ForegroundColor Yellow "Conf finished"
- $Session.Disconnect()
- #---------------+
- # Format Config |
- #---------------+
- $f = $output -Split '[\r\n]'
- $content = $f | where {$_ -ne ""}
- $content = $content | where {$_ -notlike "*!" -and $_ -notlike " --more*"}
- #Skip Version etc.
- $c = 0
- do
- {
- $c++
- } until ($content[$c] -match 'version')
- $content = $content | select -Skip ($c + 1)
- # Build command objects with sub-commands
- $conf = @()
- $e = -1
- foreach($line in $content)
- {
- if($line -notlike " *")
- {
- $prob = @{
- 'Name' = $line
- 'Commands' = @()
- }
- $obj = New-Object -TypeName Psobject -Property $prob
- $conf += $obj
- $e++
- }
- else
- {
- $conf[$e].Commands += $line
- }
- }
- $int = $conf | where {$_.name -like "interface*" -and $_.commands -ne ""}
- #Filter "AutoQos-4 Accesslists
- $conf = $conf | where {$_.name -notlike "interface*" -and $_.name -notlike "*AutoQos-4*"} | Sort-Object -Property name
- $conf = $conf + $int
- #-------------------------------+
- # Build formatted Print version |
- #-------------------------------+
- $print = @()
- for ($i = 0; $i -lt $conf.Count;$i++)
- {
- if ($conf[$i - 1].Commands.count -gt 0 -or $conf[$i].Commands.count -gt 0)
- {
- $print += ""
- }
- $print += $conf[$i].Name
- if ($conf[$i].Commands.count -gt 0)
- {
- foreach ($cmd in $conf[$i].Commands)
- {
- $print += " " + $cmd
- }
- }
- }
- $name = ($print | where {$_ -match "#"} | select -Last 1).split('#')[0]
- $print | Out-File -FilePath "$($env:USERPROFILE)\desktop\$($name).txt" -Encoding utf8 -Force
- Write-Host -ForegroundColor Yellow "Config for $($name) created - Path $($env:USERPROFILE)\desktop\"
- pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement