In network management, regularly retrieving and saving switch configurations is a key task that is often neglected 😉
This PowerShell script automates the process using Plink. With minimal adjustments, you can fetch the configuration from multiple switches simultaneously and save each in a text file.
The text document is always named after the switch's IP address.
Prerequisites
Before using the script, ensure that:
plink.exeis available (e.g., from the PuTTY tools)- SSH is enabled on the switches
- A text file with the switch IP addresses exists (one IP per line)
- Valid SSH credentials are available
Script Overview
The following script reads IP addresses from a text file, establishes an SSH connection to each switch, executes a command to display the running configuration, and saves the results in a separate file for each IP address.
# Path to plink.exe and switche.txt
$plinkPath = ".\plink.exe" # Path to the plink.exe executable used for SSH connections
$switchListPath = ".\switche.txt" # Path to the text file containing the list of switch IP addresses
# SSH credentials
$username = "SSH-USER" # Username for SSH login
$password = "SSH-PASSWORT" # Password for SSH login
# Convert password to SecureString
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
# Read IP addresses from switche.txt
$switchIPs = Get-Content $switchListPath
foreach ($ip in $switchIPs) {
# Command to fetch the configuration
$command = "show running-config"
# Filename based on IP address
$outputFileName = "$ip.txt"
# Plink command to establish SSH connection and retrieve the configuration
$plinkCommand = "& $plinkPath -ssh $username@$ip -pw $password -batch $command"
# Execute the plink command and save the output
Invoke-Expression $plinkCommand | Out-File -FilePath $outputFileName -Encoding utf8
Write-Host "Configuration from $ip saved to $outputFileName"
}
Detailed Explanation
Path Assignment
The script starts by defining paths to Plink and the file with the switch IP addresses:
$plinkPath = ".\plink.exe"
$switchListPath = ".\switche.txt"
Make sure these paths are correct or adjust them to your environment.
Credentials
SSH login credentials are defined in plain text and then converted into a SecureString:
$username = "SSH-USER"
$password = "SSH-PASSWORT"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
Each IP address is then used in a loop to establish the SSH connection.
Retrieval and Storage of Configuration
For each IP address, a Plink command is generated and executed to retrieve the running configuration of the switch. The output is saved to a file named after the IP address.
foreach ($ip in $switchIPs) {
$command = "show running-config"
$outputFileName = "$ip.txt"
$plinkCommand = "& $plinkPath -ssh $username@$ip -pw $password -batch $command"
Invoke-Expression $plinkCommand | Out-File -FilePath $outputFileName -Encoding utf8
Write-Host "Configuration from $ip saved to $outputFileName"
}
Adjustments for Other Switches
The command executed on the switch can be easily modified. Simply change:
$command = "show running-config"
to, for example:
$command = "show version"
to retrieve the software version of the switches instead.
Conclusion
This script allows efficient automated backup and archiving of switch configurations. By simply adjusting the command to be executed, it can be flexibly adapted to different requirements. Automation saves time and reduces the risk of manual errors during configuration backup.