Category Archives: scripting-brocade

Automating Config / Zone backups for Brocade Switches

I was looking for an easy way to make backups of our fabric and report the status of the backup on our internal web page. I wrote a script that will remotely run configupload and pull the script config file from all the switches via FTP, move the previous config file to an archive location, create a report of the output and copy it to a web page folder. I run the bash script using cygwin on our internal IIS server, and it’s scheduled to run daily.

The script uses ssh, so you could either set up ssh keys or use an opensource package called sshpass (http://sourceforge.net/projects/sshpass). In this example script I’m using sshpass to avoid having to type in a password for each command when the script runs.

I figured out that you must connect to a new switch at least once manually without using sshpass, as it supresses the output that asks you to confirm adding it as a known host. Below is the output, I simply ran a ‘zoneshow’ as the initial command to set it up.

$ ssh USERID@brocade_switch_1 zoneshow
 The authenticity of host ‘brocade_switch_1 (10.0.0.9)’ can’t be established.
 DSA key fingerprint is 3b:cd:98:62:4a:67:99:28:c4:41:f3:19:8d:f1:7d:a0.
 Are you sure you want to continue connecting (yes/no)? yes
 Warning: Permanently added ‘brocade_switch_1,10.0.0.9’ (DSA) to the list of known hosts.

My original script is set up to run for multiple geographical locations which is why I have separate lists set up.   This sample script is set up for two separate theoretical locations, it could easily be expanded or reduced based on your environment.

#Set Environment 
TODAY=`date`
TIMESTAMP=`date +”%Y%m%d%H%M”`
LOCALPATH=”/cygdrive/c/scripts/brocade”
WEBPATH=”/cygdrive/c/inetpub/wwwroot”
FTPHOST=”10.0.0.1″
FTPUSER=”ftpuser”
FTPPATH=”/brocade”
FTPPASSWORD=”password”

#Add timestamp to top of report
echo $TODAY > $WEBPATH/brocade_backup_report.txt
echo ” ” >> $WEBPATH/brocade_backup_report.txt

#Clear data from last run
>$LOCALPATH/brocade_backup_report_1.txt
>$LOCALPATH/brocade_backup_report_2.txt

#Move yesterday’s backups to an archive location
mv $WEBPATH/brocade/*.txt /cygdrive/e/archive/brocade

#List of Switches to be backed up
SWITCHLIST1=”switch1siteA switch2siteA switch3siteA switch4siteA”
SWITCHLIST2=”switch1siteB switch2siteB switch3siteB switch4siteB switch5siteB switch6siteB”

for x in $SWITCHLIST1
do
echo “$x”: “$FTPPATH/$x.$TIMESTAMP” >> $LOCALPATH/brocade_backup_report_1.txt
sshpass -p ‘password’ ssh admin@$x configupload -ftp $FTPHOST,$FTPUSER,$FTPPATH/$x.$TIMESTAMP.txt,$FTPPASSWORD >> $LOCALPATH/brocade_backup_report_1.txt
echo ” ” >> $LOCALPATH/brocade_backup_report_1.txt
done

for x in $SWITCHLIST2
do
echo “$x”: “$FTPPATH/$x.$TIMESTAMP” >> $LOCALPATH/brocade_backup_report_2.txt
sshpass -p ‘password’ ssh USERID@$x configupload -ftp $FTPHOST,$FTPUSER,$FTPPATH/$x.$TIMESTAMP.txt,$FTPPASSWORD >> $LOCALPATH/brocade_backup_report_2.txt
echo ” ” >> $LOCALPATH/brocade_backup_report_2.txt
done

# This last section creates the report for the web page.
cat $LOCALPATH/brocade_backup_report_1.txt.txt $LOCALPATH/brocade_backup_report_2.txt.txt >> $WEBPATH/brocade_backup_report.txt

The report output looks like this:

Thu Sep 12 06:00:01 CDT 2013
 
switch1siteA: /brocade/switch1siteA.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch2siteA: /brocade/switch2siteA.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch3siteA: /brocade/switch3siteA.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch4siteA: /brocade/switch4siteA.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch1siteB: /brocade/switch1siteB.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch2siteB: /brocade/switch2siteB.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch3siteB: /brocade/switch3siteB.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch4siteB: /brocade/switch4siteB.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch5siteB: /brocade/switch5siteB.201309120600
configUpload complete: All selected config parameters are uploaded
 
switch6siteB: /brocade/switch6siteB.201309120600
configUpload complete: All selected config parameters are uploaded

Scripting Alias and Zone creation for Brocade Switches

Whenever a new array is added in our environment we usually have to add hundreds and hundreds of new zones to our core brocade fabric.  It’s a very tedious job so I started investigating ways to script the process.

Here is the syntax for running an ssh command remotely (telnet is disabled on all of our switches):

ssh userid@switchname [command]

So, if I wanted to show all of the zones on a switch the command would look like this:

ssh admin@10.0.0.1 zoneshow

While those commands work perfectly and could be added in to a bash script as-is, the caveat is that the password must be typed in for every command.  That’s not very practical if there are hundreds of lines in the script.  You could set up ssh keys on every switch, but I was looking for a quicker, easier solution.  I found that quick solution with an opensource package called sshpass (http://sourceforge.net/projects/sshpass).  It allows you to enter the password on the command line.  I use Cygwin on a windows server for all my bash scripts and it was a very simple installation in that environment, and I’m sure would be just as easy on Linux.  Download it to a folder,  uncompress it, run “./configure”, “make”, and “make install” and you’re all done.

Once sshpass is installed, the syntax for showing the zones on a brocade switch would look like this:

sshpass –p “[password]” ssh admin@10.0.0.1 zoneshow

Now that the commands can be run very easily from a remote shell, I needed to make creating the script much easier.  That’s where a spreadsheet application helps tremendously.  Divide the script line into different cells on the spreadsheet, making sure to separate any part of the command that’s going to change.  In the last cell on the line, you concatenate all of the previous cells together to create the complete command.  Below is how I did it. Here’s the syntax output I want for creating a zone:

sshpass -p 'password' ssh [User_ID]@[Switch_Name] zonecreate "[Zone_Name]","[Host_Alias]","[SP_Alias]"

Here’s how I divide the command up in the spreadsheet:

A1          sshpass  -p ‘password’ ssh

B1          [User_ID]

C1          @

D1          [switch_name]

E1           Zonecreate “

F1           [Zone_Name]

G1           “,”

H1           [Host_Alias]

I1            “,”

J1            [Clarrion/VNX_SP_Alias]

K1           “

L1           =concatenate(A1,B1,C1,D1,E1,F1,G1,H1,J1,K1)

Now you can copy line 1 of the spreadsheet to as many lines as you need, change the User ID, Switch Name, Zone_Name, Host Alias, and Clariion/VNX SP Alias as needed, and the L column will have all the completed commands for you that you can cut and paste into a bash script.  Create a blank script file with ‘touch file.sh’, do a ‘chmod +X file.sh’ and ‘chmod 777 file.sh’ on it, use vi to copy and paste in the script data, then run it with ‘./file.sh’ from the prompt.

The same thing can be done for creating the initial aliases, here’s the syntax of the command for that:

sshpass -p 'password' ssh [User_ID]@[Switch_Name] alicreate "[Alias_Name]","[WWN]”

And finally, here’s what it looks like entered into a spreadsheet:BrocadeScript