Advertisement
upz

Cisco IOS config SSH

upz
Mar 6th, 2018
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Requires Posh-SSH module
  2.  
  3. $ip = Read-Host "Enter device IP"
  4.  
  5.  
  6. #----------------------+
  7. # Get Conf from Device |
  8. #----------------------+
  9.  
  10. do
  11. {
  12.     $c = 0
  13.     $credentials = Get-Credential
  14.    
  15.     try
  16.     {
  17.         $c = 1
  18.         $Session = New-SSHSession -ComputerName $ip -Credential $credentials -AcceptKey -Force -ErrorAction Stop              
  19.     }
  20.     Catch
  21.     {
  22.         Write-Host -ForegroundColor Red "Wrong credentials"
  23.         $c = 0
  24.     }
  25. } until ($c -eq 1)
  26.  
  27. $stream = $Session.Session.CreateShellStream("Cisco", 0, 0, 0, 0, 10000)
  28.  
  29. $stream.Write("term len 0`r")
  30. Start-Sleep 1
  31. $stream.write("sh run`r")
  32. Write-Host -ForegroundColor Yellow "Building conf"
  33. Start-Sleep 12
  34. $output = $stream.Read()
  35. Write-Host -ForegroundColor Yellow "Conf finished"
  36.  
  37. $Session.Disconnect()
  38.  
  39.  
  40. #---------------+
  41. # Format Config |
  42. #---------------+
  43.  
  44. $f = $output -Split '[\r\n]'
  45. $content = $f | where {$_ -ne ""}
  46. $content = $content | where {$_ -notlike "*!" -and $_ -notlike " --more*"}
  47.  
  48. #Skip Version etc.
  49. $c = 0
  50. do
  51. {    
  52.     $c++
  53. } until ($content[$c] -match 'version')
  54. $content = $content | select -Skip ($c + 1)
  55.  
  56.  
  57. # Build command objects with sub-commands
  58. $conf = @()
  59.  
  60. $e = -1
  61. foreach($line in $content)
  62. {
  63.     if($line -notlike " *")
  64.     {
  65.         $prob = @{
  66.         'Name' = $line
  67.         'Commands' = @()
  68.         }
  69.  
  70.         $obj = New-Object -TypeName Psobject -Property $prob
  71.         $conf += $obj
  72.         $e++
  73.     }
  74.     else
  75.     {
  76.         $conf[$e].Commands += $line
  77.     }    
  78. }
  79.  
  80. $int = $conf | where {$_.name -like "interface*" -and $_.commands -ne ""}
  81.  
  82. #Filter "AutoQos-4 Accesslists
  83. $conf = $conf | where {$_.name -notlike "interface*" -and $_.name -notlike "*AutoQos-4*"} | Sort-Object -Property name
  84. $conf = $conf + $int
  85.  
  86.  
  87. #-------------------------------+
  88. # Build formatted Print version |
  89. #-------------------------------+
  90.  
  91. $print = @()
  92. for ($i = 0; $i -lt $conf.Count;$i++)
  93. {
  94.     if ($conf[$i - 1].Commands.count -gt 0 -or $conf[$i].Commands.count -gt 0)
  95.     {
  96.         $print += ""
  97.     }
  98.    
  99.     $print += $conf[$i].Name
  100.    
  101.     if ($conf[$i].Commands.count -gt 0)
  102.     {
  103.         foreach ($cmd in $conf[$i].Commands)
  104.         {
  105.             $print += " " + $cmd
  106.         }
  107.     }    
  108. }
  109.  
  110. $name = ($print | where {$_ -match "#"} | select -Last 1).split('#')[0]
  111. $print | Out-File -FilePath "$($env:USERPROFILE)\desktop\$($name).txt" -Encoding utf8 -Force
  112. Write-Host -ForegroundColor Yellow "Config for $($name) created - Path $($env:USERPROFILE)\desktop\"
  113. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement