#!/usr/bin/expect -f
#
# Language: Expect
#
# Original Author:   Michael McNamara
#
# Date:     May 6, 2003
#
# Changes:
#
#   Sept 29, 2006: cleaned up script/updated documenation
#    Dec 30, 2005: added command line arguements for portability
#    Mar 18, 2005: added file logging for troubleshooting and monitoring
#    May 20, 2003: fine tuned script removing a great many "expect" commands.
#    May  6, 2003: original Expect script generated from auto_expect
#
# Notes:
#        Command Line Reference;
#          ./8600dump.exp <switch> <username> <password>
#
# This Expect script should run on just about any system so long as there
# is a "telnet" binary available. There are no requirement for SNMP mibs
# or other external files. All you should need is Expect and a telnet app.
#

set force_conservative 0  ;# 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
	}
}

#
# Declare Global Variables
#
set SSH "/usr/bin/ssh"

######################################################################
# proc usage
#
# Purpose: display the usage information to the enduser.
######################################################################
proc usage {} {
	send_user "\n"
	send_user "ERROR: command line paramaters incorrect\n"
	send_user "\n"
	send_user "Usage: ./cisco_backup <mode> <switch> <tftp address> <ssh username> <ssh key> <logfile> <tftpdir>\n"
	send_user "\n"
	send_user "    mode 		either choose 'backup' or 'get_version'\n"
	send_user "    switch		the DNS or IP address of switch	\n"
	send_user "    tftp address	the DNS or IP address of the TFTP server (not needed for get_version)\n"
	send_user "    ssh username	the username for login to the switch	\n"
	send_user "    ssh key		the path to the ssh key of the ssh username\n"
	send_user "    logfile		the path to the log file used for printing messages from the script\n"
	send_user "    tftpdir          directory on the TFTP server to place the files\n"
	send_user "\n"
	send_user "\n"
	exit
}
#######################################################################

#
# Assign Command Line Variablbes
#
set MODE [lindex $argv 0]
set SWITCH [lindex $argv 1]
if { $MODE == "backup" } {
	set tftp_address [lindex $argv 2]
	set SSH_USER [lindex $argv 3]
	set SSH_KEY [lindex $argv 4] 
	set log_file [lindex $argv 5]
	set tftpdir [lindex $argv 6]
	if {[llength $argv] != 7 } usage
} elseif { $MODE == "get_version" } {
	set SSH_USER [lindex $argv 2]
	set SSH_KEY [lindex $argv 3]
	set log_file [lindex $argv 4]
	if {[llength $argv] != 5 } usage
} else {
    usage
}

#
# Time Date Stamp
#
set TODAY [timestamp -format %y%m%d ]
set WEEKDAY [timestamp -format %a ]
set DATE [timestamp -format %c ]

set send_human {.1 .3 1 .05 2}


#######################################################################
# M A I N    P R O G R A M
#######################################################################

# log_file $LOG_PATH/$SWITCH.dump.log
log_user 0      # Disable logging to STDOUT
#log_user 1     # Enable logging to STDOUT

# Useful information out to logfile
send_log "***************************************************************\r\n"
send_log "* STARTING LOGFILE FOR $SWITCH ON $DATE \r\n"
send_log "***************************************************************\r\n"

# timeout
set timeout 10

# Launch session
spawn $SSH -l $SSH_USER -i $SSH_KEY $SWITCH

match_max 100000

expect "#" {
    if { $MODE == "backup" } {
        send -- "copy running-config tftp://$tftp_address/$tftpdir/$SWITCH.conf\r"
        set MSG "configuration generation to $tftpdir/$SWITCH.conf"
    } else {
        send -- "show version\r"
        set MSG "version retrieval"
    }
    sleep 5
    expect "#" {
        send_log "\r\n$MSG completed successful\r\n"
    }
}
send -- "exit\r"
expect eof

#######################################################################
# E N D    P R O G R A M
#######################################################################
