-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkConfigurationAuditor.ps1
365 lines (317 loc) · 11 KB
/
NetworkConfigurationAuditor.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#Requires -Version 5.1
#Requires -Modules Posh-SSH
<#
.SYNOPSIS
Advanced Cisco network device configuration auditing and compliance system.
.DESCRIPTION
This script provides comprehensive network device configuration analysis:
- Connects to Cisco devices via secure SSH
- Audits interface configurations and statuses
- Validates VLAN assignments and trunking
- Analyzes routing table configurations
- Compares against best practices templates
- Identifies security vulnerabilities
- Generates detailed audit reports
- Validates ACL configurations
- Ensures compliance standards
- Provides remediation recommendations
.NOTES
Author: 13city
Compatible with: Windows Server 2016-2022, Windows 10/11
Requirements:
- PowerShell 5.1 or higher
- Posh-SSH module installed
- Network connectivity to devices
- Valid device credentials
- Write access to script directory
- CSV and JSON parsing capabilities
.PARAMETER DeviceList
Path to CSV file with device information
Required: Yes
Format: CSV with headers (IPAddress,Username,Password,Type)
Example: "C:\Scripts\devices.csv"
Must contain valid device credentials
.PARAMETER TemplateFile
Path to JSON best practices template
Required: Yes
Format: JSON configuration template
Example: "C:\Scripts\best_practices.json"
Defines configuration standards
.EXAMPLE
.\NetworkConfigurationAuditor.ps1 -DeviceList "devices.csv" -TemplateFile "best_practices.json"
Basic audit execution:
- Processes all devices in CSV
- Uses specified template
- Generates standard reports
- Default compliance checks
.EXAMPLE
.\NetworkConfigurationAuditor.ps1 -DeviceList "C:\Network\core_switches.csv" -TemplateFile "C:\Templates\core_template.json"
Custom configuration audit:
- Specific device list
- Custom template file
- Full compliance checking
- Detailed reporting
.EXAMPLE
$devices = ".\production_devices.csv"
$template = ".\enterprise_standards.json"
.\NetworkConfigurationAuditor.ps1 -DeviceList $devices -TemplateFile $template
Production environment audit:
- Uses production device list
- Enterprise standards template
- Comprehensive analysis
- Security validation
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$DeviceList,
[Parameter(Mandatory = $true)]
[string]$TemplateFile
)
# Default best practices if template file not found
$defaultBestPractices = @{
VLANs = @{
Management = 1
Native = 1
Voice = "100-110"
Data = "111-999"
Reserved = "1000-4094"
}
Interfaces = @{
TrunkPorts = @{
AllowedVlans = "1-999"
Mode = "trunk"
NativeVlan = 1
}
AccessPorts = @{
Mode = "access"
PortSecurity = $true
StormControl = $true
}
}
ACLs = @{
InboundRules = @(
"permit tcp any any established"
"deny ip any any log"
)
OutboundRules = @(
"permit ip any any"
)
}
}
# Function to establish SSH connection
function Connect-NetworkDevice {
param (
[string]$IpAddress,
[string]$Username,
[string]$Password
)
try {
$sshSession = New-SSHSession -ComputerName $IpAddress -Credential (
New-Object System.Management.Automation.PSCredential(
$Username, (ConvertTo-SecureString $Password -AsPlainText -Force)
)
) -AcceptKey
return $sshSession
}
catch {
Write-Error "Failed to connect to $IpAddress : $_"
return $null
}
}
# Function to execute command and get output
function Invoke-NetworkCommand {
param (
[object]$Session,
[string]$Command
)
try {
$result = Invoke-SSHCommand -SSHSession $Session -Command $Command
return $result.Output
}
catch {
Write-Error "Failed to execute command '$Command': $_"
return $null
}
}
# Function to get interface status
function Get-InterfaceStatus {
param (
[object]$Session
)
$output = Invoke-NetworkCommand -Session $Session -Command "show interfaces status"
$interfaces = @{}
foreach ($line in $output) {
if ($line -match "^(\S+)\s+(.+?)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(.+)$") {
$interfaces[$matches[1]] = @{
Status = $matches[3]
Vlan = $matches[4]
Duplex = $matches[5]
Speed = $matches[6]
Type = $matches[7]
}
}
}
return $interfaces
}
# Function to get VLAN information
function Get-VlanInfo {
param (
[object]$Session
)
$output = Invoke-NetworkCommand -Session $Session -Command "show vlan brief"
$vlans = @{}
foreach ($line in $output) {
if ($line -match "^(\d+)\s+(\S+)\s+(\w+)\s+(.*)$") {
$vlans[$matches[1]] = @{
Name = $matches[2]
Status = $matches[3]
Ports = $matches[4] -split ","
}
}
}
return $vlans
}
# Function to get routing table
function Get-RoutingTable {
param (
[object]$Session
)
$output = Invoke-NetworkCommand -Session $Session -Command "show ip route"
$routes = @{}
foreach ($line in $output) {
if ($line -match "^([A-Z*])\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/(\d{1,2})\s+.*$") {
$routes[$matches[2]] = @{
Type = $matches[1]
Prefix = $matches[3]
}
}
}
return $routes
}
# Function to compare configuration with best practices
function Compare-Configuration {
param (
[hashtable]$CurrentConfig,
[hashtable]$BestPractices
)
$discrepancies = @{
VLANs = @()
Interfaces = @()
ACLs = @()
}
# Compare VLANs
foreach ($vlan in $CurrentConfig.VLANs.Keys) {
$vlanNum = [int]$vlan
# Check if VLAN is in allowed ranges
$inRange = $false
foreach ($range in $BestPractices.VLANs.Values) {
if ($range -match "(\d+)-(\d+)") {
$start = [int]$matches[1]
$end = [int]$matches[2]
if ($vlanNum -ge $start -and $vlanNum -le $end) {
$inRange = $true
break
}
}
elseif ($range -eq $vlanNum) {
$inRange = $true
break
}
}
if (-not $inRange) {
$discrepancies.VLANs += "VLAN $vlan is outside recommended ranges"
}
}
# Compare Interface configurations
foreach ($interface in $CurrentConfig.Interfaces.Keys) {
$config = $CurrentConfig.Interfaces[$interface]
if ($config.Mode -eq "trunk") {
# Check trunk port configuration
if ($config.AllowedVlans -ne $BestPractices.Interfaces.TrunkPorts.AllowedVlans) {
$discrepancies.Interfaces += "Interface $interface: Allowed VLANs mismatch"
}
if ($config.NativeVlan -ne $BestPractices.Interfaces.TrunkPorts.NativeVlan) {
$discrepancies.Interfaces += "Interface $interface: Native VLAN mismatch"
}
}
else {
# Check access port configuration
if (-not $config.PortSecurity) {
$discrepancies.Interfaces += "Interface $interface: Port security not enabled"
}
if (-not $config.StormControl) {
$discrepancies.Interfaces += "Interface $interface: Storm control not configured"
}
}
}
return $discrepancies
}
# Main execution block
try {
# Load best practices template
$bestPractices = if (Test-Path $TemplateFile) {
Get-Content $TemplateFile | ConvertFrom-Json -AsHashtable
}
else {
Write-Warning "Template file not found. Using default best practices."
$defaultBestPractices
}
# Load device list
$devices = Import-Csv $DeviceList
foreach ($device in $devices) {
Write-Host "`nAuditing device: $($device.IPAddress)" -ForegroundColor Cyan
# Connect to device
$session = Connect-NetworkDevice -IpAddress $device.IPAddress -Username $device.Username -Password $device.Password
if ($session) {
try {
# Gather configuration
$currentConfig = @{
Interfaces = Get-InterfaceStatus -Session $session
VLANs = Get-VlanInfo -Session $session
Routes = Get-RoutingTable -Session $session
}
# Compare with best practices
$discrepancies = Compare-Configuration -CurrentConfig $currentConfig -BestPractices $bestPractices
# Report findings
if ($discrepancies.VLANs.Count -eq 0 -and
$discrepancies.Interfaces.Count -eq 0 -and
$discrepancies.ACLs.Count -eq 0) {
Write-Host "No discrepancies found." -ForegroundColor Green
}
else {
Write-Host "Discrepancies found:" -ForegroundColor Yellow
if ($discrepancies.VLANs.Count -gt 0) {
Write-Host "`nVLAN Issues:" -ForegroundColor Yellow
$discrepancies.VLANs | ForEach-Object { Write-Host "- $_" }
}
if ($discrepancies.Interfaces.Count -gt 0) {
Write-Host "`nInterface Issues:" -ForegroundColor Yellow
$discrepancies.Interfaces | ForEach-Object { Write-Host "- $_" }
}
if ($discrepancies.ACLs.Count -gt 0) {
Write-Host "`nACL Issues:" -ForegroundColor Yellow
$discrepancies.ACLs | ForEach-Object { Write-Host "- $_" }
}
}
# Export results to file
$reportPath = Join-Path -Path $PSScriptRoot -ChildPath "NetworkAudit_$($device.IPAddress)_$(Get-Date -Format 'yyyyMMdd_HHmmss').json"
@{
DeviceIP = $device.IPAddress
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Configuration = $currentConfig
Discrepancies = $discrepancies
} | ConvertTo-Json -Depth 10 | Out-File -FilePath $reportPath
Write-Host "`nDetailed report saved to: $reportPath" -ForegroundColor Cyan
}
finally {
# Cleanup
Remove-SSHSession -SSHSession $session | Out-Null
}
}
}
}
catch {
Write-Error "Script execution failed: $_"
exit 1
}