Sunday, March 17, 2013

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#, 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


[string] $myExpect = "#"
[string] $myExpect2 = "]?"
[string] $myConfig = "running-config"
$myConfig = "startup-config"

$cred = @{}
$cred.username = "$username@$ipAddress"
$cred.Password = "$password"
      $SshSession = new-object Tamir.SharpSsh.SshShell `
 #Build SSH Session
[string] $Myhostname = $SshSession.Expect("$myExpect")
    $SshSession.RemoveTerminalEmulationCharacters = $true
    $SshSession.Writeline("copy " + $myConfig + " tftp://" + $ipTFTP + $Myhostname + ".txt")

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.