2

I created a bash function to "automagically" connect on our switches and retrive their startup-config using the expect command. I have to use expect because this switch does not accept the ssh user@host fashion and ask me again for the User and Password tuple.

This is the function that i created to manage those backups

main_pc3548(){
        /usr/bin/env expect <<-END3548
        spawn ssh -o StrictHostKeyChecking=no -o LogLevel=quiet $IP
        expect "User Name:"
        send "$USER\r"
        expect "Password:"
        send "$PASS\r"
        expect "*# "
        send "copy startup-config tftp://$SERVER/$SWNAME.cfg.bkp\r"
        sleep 8
        END3548
}

This block of code will separate my switch types, and call main_pc3548() when my switch list have this switch model:

egrep -v '(^#|^\s*$|^\s*\t*#)' $LISTA_SWITCHES | while read IP SWNAME SERVER TIPO
do
        if [ "$TIPO" = core  ]; then
                main_pc6248
        elif [ "$TIPO" = dep ]; then
                main_pc3548
        else
                echo "$(date "+%d/%m/%Y-%T") - Switch $SWNOME Have a martian type of switch" >> $LOG_FILE
        fi
done

The rest of the script reads a pretty lengthy file with information about the Switch IP, the TFTP ip address, the Switch name, and waiting 8 seconds each switch consumes a lot of time. This sleep is needed to avoid slow connections to break the tftp copy so, here it comes my question:

Is there a easy way to "multithread" this function to gain performance?

  • 2
    Execute the functions in the background? – devnull Apr 9 '14 at 19:28
  • Wow. Too obvious that i didn´t even tried ¬¬. It worked, changing to main_pc6248 & and main_pc3548 & did the trick – user34720 Apr 9 '14 at 19:31
  • Not that this answers your question.. If you haven't already looked into it you may want to take a look at RANCID. May save you a whole lot of effort reinventing the wheel. – yoonix Apr 9 '14 at 21:33
  • Thanks @yoonix. Ill take a look at Rancid, but i developed this script for a really specific issue: Connecting on the switches on a centralized way(from our firewall at the main office) and taking backups on tftp servers that are located on the firewalls of those regional offices. I don´t want to replicate every version of this script to each office, neither manage separated switch list files. ;) – user34720 Apr 10 '14 at 11:14
0

The solution was the tip that @devnull gave at the comments: Execute each funcion on background

# Trata comentários na lista de switches
egrep -v '(^#|^\s*$|^\s*\t*#)' $LISTA_SWITCHES | while read IP SWNOME SERVER TIPO
do
        if [ "$TIPO" = core  ]; then
                pc6248 &
        elif [ "$TIPO" = dep ]; then
                pc3548 &
        elif [ "$TIPO" = rfs ]; then
                rfs6000 &
        else
                echo "$(date "+%d/%m/%Y-%T") - Switch $SWNOME possui tipo marciano de switch" >> $LOG_FILE
        fi
done

Now, after 20 seconds about 50 switches have the backup finished :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy