Brocade Zoning helper script

This is a simple script which takes a CSV input file (plus a few arguments) and updates the zoning config. The script

  • Checks that the config name already exists

  • Checks that the specified storage alias already exists

  • automatically saves the config

  • automatically enables the config --> note - this might not be desired in tightly controlled environments, can comment out this section

  • basic regex validation for WWPNs.

    !/usr/bin/env bash

    This script will create Brocade zoning configuration using supplied csv files

    It is assumed that SSH keys for the FC switch have already been exchanged to

    allow passwordless login

    display_usage()
    {
    echo ""
    echo "Usage: "
    echo " ${0} -h|--help"
    echo " ${0} -S {switch_hostname} -a {alias_csv_file}
    -t {target_alias_name} -c {zone_config}"
    echo ""
    echo " "
    echo "e.g."
    echo "# ${0} -S switch01 -a /home/joebloggs/aliases.csv -t storage_hba01
    -c fabric_A"
    echo ""
    echo ""
    exit 1
    }

    if [ $# = 0 ]
    then
    display_usage
    fi

    while [ $# -gt 0 ]
    do
    opt=$1

    case "$opt" in
    -h|--help)
    display_usage
    ;;
    -S)
    shift
    switch_hostn=$1
    ssh_cmd="ssh -l admin ${switch_hostn}"
    ;;
    -a)
    shift
    if [ ! -f $1 ]
    then
    echo ""
    echo "CSV Source file $1 either does not exist or is not accessible"
    echo ""
    display_usage
    fi
    if ! cat $1 | grep '[1]{1,64},([a-f0-9]{2})(:[a-f0-9]{2}){7}$' > /dev/null
    then
    echo ""
    echo "data in CSV Import ${1} is not valid"
    echo ""
    display_usage
    fi
    alias_csv=$1
    ;;
    -t)
    shift
    target_alias=$1
    ;;
    -c)
    shift
    zoning_config=$1
    ;;
    *)
    # unknown option
    echo "unknown option specified"
    display_usage
    ;;
    esac
    shift # past argument or value
    done

    if [[ ! $(${ssh_cmd} cfgshow | grep ${target_alias}) ]]
    then
    echo "target alias not found, exiting"
    exit 1
    fi

    cfglist=echo \$ssh_cmd cfgshow|grep cfg:|awk '{print $2}'`\ `
    if [[ ! "$cfglist" =~ "$zoning_config " ]]
    then
    echo zoning config ${zoning_config} was not found, exiting
    exit 1
    fi

    alilist=echo \$ssh_cmd cfgshow|grep alias|awk '{print $2}'`\ `

    for line in cat ${alias_csv}
    do
    initiator=(${line/,/ })
    echo ${initiator[0]}
    echo ${initiator[1]}
    # ${ssh_cmd} alicreate "${initiator[0]}", "${initiator[1]}"
    if [[ ! "$alilist" =~ "$initiator " ]]
    then
    echo "adding initiator alias"
    ${ssh_cmd} "alicreate "${initiator[0]}", "${initiator[1]}" ;
    echo y | cfgsave"
    fi
    # ${ssh_cmd} "alicreate "${initiator[0]}", "${initiator[1]}" ;
    echo "creating zone and enabling config"
    ${ssh_cmd} "zonecreate "${target_alias}to${initiator[0]}", "${target_alias}; ${initiator[0]}" ;
    cfgadd "${zoning_config}", "${target_alias}to${initiator[0]}" ;
    echo y | cfgsave"

    ${ssh_cmd} zonecreate "${target_alias}to${initiator[0]}", "${target_alias}; ${initiator[0]}"

    ${ssh_cmd} cfgadd "${zoning_config}", "${target_alias}to${initiator[0]}"

    sleep 0.5
    done
    echo yes | ${ssh_cmd} cfgenable "${zoning_config}"


  1. a-z0-9-_ ↩︎