SAN config Upload Expect

#!/usr/bin/expect

#Andrew Stringer, 09/09/2010
#Purpose is to automate saving SAN switch to backup02 server
#This is to be run from within the backup shell script
#variables are set in the scell script.
#Completely rewritten to work with both firmware revisions

#Sample transcript of the session to automate

#Older firmware
#[backupsan@monitor01 ~]$ ssh -l admin gy-fcs1-01
#admin@gy-fcs1-01's password:
#gy-fcsw1-01:admin> configupload
#Protocol (scp or ftp) [ftp]: scp
#Server Name or IP Address [host]: gy--mon01
#User Name [user]: backupsan
#File Name [config.txt]: /home/backupsan/backupfiles/ajs11.txt
#backupsan@gy--mon01's password:
#Upload complete
#gy-fcs1-01:admin> exit
#logout
#Connection to gy-fcs1-01 closed.
#[backupsan@gy--mon01 ~]$


#Newer firmware
#[backupsan@monitor01 ~]$ ssh -l admin gy-fcs1-04
#admin@gy-fcs1-04's password:
#
#-----------------------------------------------------------------
#gy-fcs1-04:FID128:admin> configupload
#Protocol (scp, ftp, local) [ftp]: scp
#Server Name or IP Address [host]: gy--mon01
#User Name [user]: backupsan
#Path/Filename [<home dir>/config.txt]: /home/backupsan/backupfiles/ajs12.txt
#Section (all|chassis|FID# [all]): all
#backupsan@gy--mon01's password:
#
#configUpload complete: All selected config parameters are uploaded
#gy-fcs1-04:FID128:admin>


set force_conservative 1  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}


#
# Process the configupload commands
set SWITCH [lindex $argv 0]
set SWITCHUSER [lindex $argv 1]
set SWITCHPASS [lindex $argv 2]
set SWITCHLOGIN [lindex $argv 3]
set SWITCHPROMPT [lindex $argv 4]
set BACKUPHOST [lindex $argv 5]
set BACKUPUSER [lindex $argv 6]
set BACKUPPASS [lindex $argv 7]
set BACKUPFILE [lindex $argv 8]

set timeout 15
spawn $env(SHELL)
match_max 100000


#Login
send -- "ssh -l ${SWITCHUSER} ${SWITCH}\r"

expect {
        default {exit 0}
        "Permission denied" {exit 1}
        "password:" {send -- "${SWITCHPASS}\r"}
        }
#send -- "${SWITCHPASS}\r"

#get prompt and start upload
#gy-fcs1-01:admin>
#or
#gy-fcs1-04:FID128:admin>
expect  "admin>"
send -- "configupload\r"


#Choose scp from options
#Protocol (scp or ftp) [ftp]:
#or
#Protocol (scp, ftp, local) [ftp]: scp
expect  "scp"
send -- "scp\r"


#host name for sending config file
expect  "host"
send -- "${BACKUPHOST}\r"


#Remote User name
expect  "user"
send -- "${BACKUPUSER}\r"


#send file and path remote
expect  "config.txt"
send -- "${BACKUPFILE}.conf\r"

#Only on later firmware, earlier firmware does not ask
#Section (all|chassis|FID# [all]): all
expect  "all" {send -- "all\r"}

#send remote password
expect {
        default {exit 0}
        "failed" {exit 2}
        "password:" {send -- "${BACKUPPASS}\r"}
        }



#All done, time to quit!
expect {
        default {exit 3}
        "complete"
}
send -- "exit\r"

exit 0
 
rb/sanconfigupload-exp.txt · Last modified: 10/10/2013 14:21 by 127.0.0.1