Powershell Module to Backup configs

One of my FAVORITE tools is powershell.  It’s quick, easy to write and easy to reuse.
The esteemed Mr @BlakeKrone was asking for a script to backup configs off of a Cisco Switch.

To use this module, you have to run over and grab SharpSSH, a .Net implementation of JSCH.  I’ve used this in projects as far back as 2008.  It’s a great library and very easy to implement in C#, VB.net or Powershell.  Just throw it in the same folder with the module and update the path in the script.


#Powershell Module for Backing up a switch


Function BackupCiscoSwitchConfig

{

param(

[Parameter(Mandatory=$true)]

[string]$ipAddress,

[Parameter(Mandatory=$true)]

[string]$ipTFTP,

[Parameter(Mandatory=$true)]

[string]$username,

[Parameter(Mandatory=$true)]

[string]$password,

[switch]$StartupConfig)


[void][reflection.assembly]::LoadFrom(“C:\Users\jake\Documents\WindowsPowerShell\Modules\Tamir.SharpSSH.dll”)


[string] $myExpect = “#”

[string] $myExpect2 = “]?”

[string] $myConfig = “running-config”

if($StartupConfig) 

{

$myConfig = “startup-config”

}


$cred = @{}

$cred.username = “$username@$ipAddress”

$cred.Password = “$password”

      $SshSession = new-object Tamir.SharpSsh.SshShell `

                    $ipAddress,

$username,

$Password

 #Build SSH Session

    $SshSession.Connect()

[string] $Myhostname = $SshSession.Expect(“$myExpect”)

Write-Host($Myhostname)

    $SshSession.RemoveTerminalEmulationCharacters = $true

    $SshSession.Writeline(“copy ” + $myConfig + ” tftp://” + $ipTFTP + $Myhostname + “.txt”)

$SshSession.Expect(“$myExpect2”)

$SshSession.Writeline(“”)

$SshSession.Expect(“$myExpect2”)

$SshSession.Writeline(“”)

$SshSession.Expect($myExpect)

   

One of the nice things about this, is that it’s easily used in other scripts.  For example if you had a spreadsheet of all the switches in your network, you could easily pipe that into this script to perform a mass backup of all devices.

You can also use the same concept to log into switches and confirm all the configs are written, change PSK or guest usernames on a WLC.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s