-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchOSPFStats.ps1
52 lines (45 loc) · 2.94 KB
/
FetchOSPFStats.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
$ConfigFile = '.\CiscoOSPF-Config.xml'
$ConfigParams = [xml](get-content $ConfigFile)
# Initialize configuration variables from config xml file
$WorkingDir = $ConfigParams.configuration.backups.folder.value
$SwitchList = $ConfigParams.configuration.backups.switchlist.value
$PlinkExe = $ConfigParams.configuration.backups.plink.value
$SaveHostKey = $ConfigParams.configuration.backups.savehostkeycmd.value
$AuthUser = $ConfigParams.configuration.auth.user.value
$AuthPass = $ConfigParams.configuration.auth.password.value
$Today = (Get-Date).ToString("yy-MM-dd")
$TodaysBackupFolder = $WorkingDir + '\' + $Today
# If the OSPF output directory ($WorkingDir) does not exist, create it
if (-not(Test-Path -Path $WorkingDir -PathType Container)){New-Item -Path $WorkingDir -ItemType Directory}
# Create today's OSPF results sub-folder
if (-not(Test-Path -Path $TodaysBackupFolder -PathType Container)){New-Item -Path $TodaysBackupFolder -ItemType Directory}
# Open the switches.txt file
$SwitchFile = Get-Content $SwitchList
# Process each line (IP) in the switches.txt file
foreach($Switch in $SwitchFile){
$ConfigFile = $TodaysBackupFolder + '\' + $Switch + '.txt'
if (Test-Path -Path $ConfigFile -PathType Leaf){
Write-Host "SKIPPING: Today's backup already exists for $Switch in $ConfigFile" -ForegroundColor Yellow
# OSPF neighbor detail file already exists, skip. Could be a duplicate IP in the switch list.
}
else{ # No existing OSPF neighbor detail file in backup path. Proceed.
# Ensure the SSH host key has been saved/trusted
& $SaveHostKey $PlinkExe $Switch *> $null
$PlinkArgs = '-ssh -batch -l ' + $AuthUser + ' -pw ' + $AuthPass + ' ' + $Switch + ' show ip ospf neighbor detail'
Write-Host "SAVING: $Switch neighbor details to $ConfigFile" -ForegroundColor Green
# Execute the OSPF neighbor detail command, saving a date stamped configuration backup file
Start-Process -FilePath $PlinkExe -WorkingDirectory $WorkingDir -ArgumentList $PlinkArgs -PassThru -Wait -RedirectStandardOutput $ConfigFile
}
$RIDFile = $TodaysBackupFolder + '\' + $Switch + '-RID.txt'
if (Test-Path -Path $RIDFile -PathType Leaf){
Write-Host "SKIPPING: Today's backup already exists for $Switch in $RIDFile" -ForegroundColor Yellow
# Backup file already exists, skip. Could be a duplicate IP in the switch list.
}
else{ # No existing config file in backup path. Proceed.
$PlinkArgs = '-ssh -batch -l ' + $AuthUser + ' -pw ' + $AuthPass + ' ' + $Switch + ' show ip ospf database router'
Write-Host "SAVING: $Switch router DB to $RIDFile" -ForegroundColor Green
# Execute the backup command, saving a date stamped configuration backup file
Start-Process -FilePath $PlinkExe -WorkingDirectory $WorkingDir -ArgumentList $PlinkArgs -PassThru -Wait -RedirectStandardOutput $RIDFile
}
}
& .\EmailOSPFChanges.ps1