Advertisement
upz

CCNPv7 lab config builder

upz
Jan 30th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function format-conf
  2. {
  3. PARAM
  4. (
  5.     $in    
  6. )
  7.  
  8.     [string]$return = ""
  9.        
  10.     $line = $in.split('#')[-1].trim()
  11.    
  12.     if ($line -match "show" -or $line -match 'ping')
  13.     {
  14.         $Return += $line.tostring().insert(0,"do ").trim()
  15.     }
  16.     else
  17.     {
  18.         $Return += $line.trim()
  19.     }
  20.    
  21.     if ($line -eq 'exit' -or $line -eq 'no shutdown')
  22.     {
  23.         $Return += "`r`n"
  24.     }
  25.    
  26.     if ($line -match "\?")
  27.     {
  28.         $return = ""
  29.     }
  30.    
  31.     return $return    
  32. }
  33.  
  34. while ($true)
  35. {
  36.     clear
  37.     #-----------------------------------+
  38.     # Get info from conf from word file |
  39.     #-----------------------------------+
  40.    
  41.     $file = Get-ChildItem -Path "$($env:USERPROFILE)\Downloads" -Filter CCNP*.doc*
  42.     $list = @()
  43.     for ($i = 0; $i -lt $file.count;$i++)
  44.     {
  45.         $prob = @{
  46.         'ID' = [int]$i;
  47.         'File' = $file[$i].BaseName
  48.         'Dir' = $file[$i].FullName
  49.         }
  50.         $obj = New-Object -TypeName Psobject -Property $prob
  51.         $list += $obj
  52.     }
  53.    
  54.     Write-Host -ForegroundColor Yellow "Press q to exit`n`r"
  55.    
  56.     Write-Host -ForegroundColor DarkGray "ID    File"
  57.     Write-Host -ForegroundColor DarkGray "--    ----"
  58.  
  59.     for ($i = 0; $i -lt $list.Count;$i++)
  60.     {
  61.         if ($i % 2)
  62.         {
  63.             Write-Host -ForegroundColor darkgray "$($list[$i].ID.tostring("00")) :: $($list[$i].File)"
  64.         }
  65.         else
  66.         {
  67.             Write-Host "$($list[$i].ID.tostring("00")) :: $($list[$i].File)"
  68.         }
  69.     }
  70.  
  71.    
  72.     $l = 0.. ($list.Length - 1)
  73.     $l += 'q'
  74.     $choice = $null
  75.  
  76.     do
  77.     {
  78.         $choice = Read-Host "Vælg fil"
  79.     } while ($l -notcontains $choice -or $choice -eq "")
  80.    
  81.     if ($choice -ne 'q')
  82.     {
  83.         $file = $list[$choice].dir
  84.        
  85.         Write-Host -ForegroundColor Yellow "Building conf from $($list[$choice].Dir)"
  86.        
  87.         $Word = New-Object -ComObject Word.application
  88.        
  89.         $doc = $Word.Documents.Open($file)
  90.         $conf = $doc.Paragraphs | foreach {$_.range.text}
  91.        
  92.         foreach ($line in $conf)
  93.         {
  94.             [regex]::Replace($line,"^\W.?","") | Out-Null
  95.         }
  96.        
  97.         $conf = $conf | where {($_ -match "#" -or [regex]::Match($_,"Step").success -or [regex]::Match($_,"Chapter").success) -and $_ -notmatch "Seq#"}
  98.        
  99.         $doc.Close()
  100.        
  101.         $devices = @()
  102.         $basic = @(" ","enable","conf t")
  103.        
  104.        
  105.         #--------------+
  106.         #Build Devices |
  107.         #--------------+
  108.        
  109.         foreach ($line in $conf)
  110.         {
  111.             if ($line -ne " ")
  112.             {
  113.                 if ([regex]::Match($line,".*?(?=\()"))
  114.                 {
  115.                     if (!($devices.contains([regex]::Match($line,".*?(?=\()").value)))
  116.                     {
  117.                         $devices += [regex]::Match($line,".*?(?=\()").value.tostring().trim()
  118.                     }            
  119.                 }
  120.                
  121.                 if ([regex]::Match($line,".*?(?=\#)"))
  122.                 {
  123.                     if(!($devices.Contains([regex]::Match($line,".*?(?=\#)").value)))
  124.                     {
  125.                         $devices += [regex]::Match($line,".*?(?=\#)").value.tostring().trim()
  126.                     }            
  127.                 }    
  128.             }
  129.         }
  130.        
  131.         $devices = $devices | where {$_ -ne "" -and $_ -ne 'Router' -and [regex]::Match($_,"^[a-zA-Z0-9_.-]*$") -and $_ -notmatch 'config'} | Sort-Object
  132.                
  133.         #---------------+
  134.         # Build Objects |
  135.         #---------------+
  136.        
  137.         $Cisco_dev = @()
  138.         foreach ($device in $devices)
  139.         {
  140.             $prob = @{
  141.             'Device' = $device;
  142.             'Text' = $null
  143.             }
  144.        
  145.             $obj = New-Object -TypeName Psobject -Property $prob
  146.             $Cisco_dev += $obj
  147.         }
  148.        
  149.        
  150.         #----------------------------------------+
  151.         # Create Conf in text proberty of object |
  152.         #----------------------------------------+
  153.        
  154.         foreach ($item in $Cisco_dev)
  155.         {
  156.             foreach ($line in $conf)
  157.             {
  158.                 # Add Basic Conf content and Hostname Based on device
  159.                 if ([regex]::Match($line,"Chapter").success)
  160.                 {
  161.                     $item.Text += [array]"!$($line.trim())"
  162.                     $item.Text += $basic + "`r`nHostname $($item.Device)"
  163.                     $chapter = $line.trim()
  164.                 }      
  165.             }
  166.        
  167.             foreach ($line in $conf | where {[regex]::Match($_,"^$($item.Device)").success -or [regex]::Match($_,"Router").success -or [regex]::Match($_,"Step").success})
  168.             {
  169.                 # ADD Chapter Descriptions and Conf
  170.                 if ([regex]::Match($line,"Step").success)
  171.                 {
  172.                     $border = ""
  173.        
  174.                     for($i = 0; $i -lt $line.Length + 1;$i++)
  175.                     {
  176.                         $border += [string]"-"
  177.                     }
  178.                    
  179.                     $item.Text += "`r`n`r`n" + "!" + $border + "+"
  180.                     $item.Text += "! " + $line.trim() + " |"
  181.                     $item.Text += "!" + $border + "+"
  182.                 }
  183.                 # Add Configurations to conf file
  184.                 else
  185.                 {
  186.                     $item.Text += format-conf -in $line
  187.                 }
  188.             }
  189.         }
  190.        
  191.        
  192.         #-----------------+
  193.         # Out file to txt |
  194.         #-----------------+
  195.        
  196.         $path = "$($env:USERPROFILE)\desktop\CCNP\$chapter"
  197.        
  198.         if (!(Test-Path $path))
  199.         {
  200.             New-Item -Path $path -ItemType Directory | Out-Null
  201.         }
  202.        
  203.         foreach ($item in $Cisco_dev)
  204.         {
  205.             $item.Text | where {$_ -ne ""} | Out-File -FilePath "$path\$($item.Device).txt" -Force
  206.         }
  207.     }
  208.     else
  209.     {
  210.         break;
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement