Link to home
Start Free TrialLog in
Avatar of Member_2_6492660_1
Member_2_6492660_1Flag for United States of America

asked on

Script Help needed

Wrote this script on a Fedora 14 Server. SSH into my Cisco ASA 5505 router.
Have TFTP server running on another Windows 2008 Server.
Manually the commands work.

The script I want to automate this process.

The SSH part works.

The copy command fail because the script never sends the ENABLE and password.
For the Copy command to work you need ENABLE and password.

Script is below.


#!/usr/bin/expect
set timeout 20
set ip 192.168.1.1
set user admin
set password secret
# I will put the date at the end of the file name
set mydate [timestamp -format %Y%m%d]
set file1 /running_save_$mydate
set file2 /startup_save_$mydate
set host 192.168.1.18
spawn ssh "$user\@$ip"
expect "Password:"
send "$password\r";
# interact
send "enable"
expect "Password:"
send "$password\r";
# copy the first file to the ftp server
send "copy system:/running-config tftp://$host/$file1\r"
#for all question just send back an ENTER
expect "Source filename"
send "\r"
expect "Address or name of remote host"
send "\r"
expect "Destination filename "
send "\r"
expect "secs"
expect "#"
# now copy the second file to the ftp server:
send "copy startup-config tftp://$host/$file2\r"
expect "Address or name of remote host"
send "\r"
expect "Destination filename "
send "\r"
expect "secs"
send "exit\r"
close
wait
rm /var/production/log/dl.txt
ls /var/tftproot | cat>/var/production/log/dl.txt
# Send Email w/attachment
/usr/bin/printf "Cisco Weekly Config Backup" | /bin/mailx -send -r root -s "Backup ASA505 Config Report" -a /var/production/log/dl.txt support@mydom.com

exit 0
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_6492660_1

ASKER

Thanks that worked.

I ran it but at the end the last few lines in the script did not run

-bash-4.1# ./ciscossh.sh
spawn ssh admin@192.168.1.1
admin@192.168,1.1's password:
Type help or '?' for a list of available commands.
MY-ASA> enable
Password: ********
MY-ASA# copy system:/running-config tftp://192.168.1.18//running_save_20$

Source filename [running-config]?

Address or name of remote host [192.168.1.18]?

Destination filename [running_save_20131106]?
Cryptochecksum: b2393727 f5a08a0b 9a3e4306 2f001aa4
!!!
8223 bytes copied in 1.410 secs (8223 bytes/sec)
MY-ASA# copy startup-config tftp://192.168.1.18//startup_save_20131106

Address or name of remote host [192.168.1.18]?

Destination filename [startup_save_20131106]?
!!!
8224 bytes copied in 0.10 secs
invalid command name "rm"
    while executing
"rm /var/production/log/dl.txt"
    (file "./ciscossh.sh" line 39)



The files were copied to my tftp server folder.  That's great

The spawn  expect and send seems to have not cloased and pass control back to the bash commands.

Am I missing a command in the script?  I thought CLOSE would do it.

Trying to email my self after the process so I know it runs.

Look at script in my first postings.

made these changes to get it to work

expect "MY-ASA#"
send "enable\r";

Thanks
that helped

added/changed

expect "MY-ASA#"
send "enable\r";

thanks