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

Running this script on Fedora 14 Server. SSH into my ASA-5505 to tftp copy the running config. The TFTP server runs on my Windows 2008 Server.  
This all works great.

My only problem is that the last couple of lines of code do not run I get this

-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.69.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.69.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)



This is my script.

#!/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";
expect "MY-ASA#"
send "enable\r";
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


I thought the close would put me back to the bash script so I could run the RM and send an email when this script completes.

How to I do this?
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Did you try to spawn commands?

e.g.

spawn rm /var/production/log/dl.txt
Avatar of Member_2_6492660_1

ASKER

Should I spawn the following also?

spawn /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

How can I do the spawn with the last three lines of code?
I think you need to spawn all shell commands

e.g.

spawn rm /var/production/log/dl.txt
spawn ls /var/tftproot | cat>/var/production/log/dl.txt
# Send Email w/attachment
spawn /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
I tried that and the commands are not working.

I think the first spawn on the ssh command is not closing or something like that.

I created a small test.sh I am using telnet in place of ssh for this test

The telnet part works connects and I quit no problem just next bash command fails.


#!/usr/bin/expect
set timeout 20
spawn telnet 192.168.1.26 25
expect "220"
send "quit\r";
close
wait
spawn rm /var/production/log/dl.txt
close
wait
spawn ls -al /mnt/WindowsTFTP >> /var/production/log/dl.txt
close
wait
# Send Email w/attachment
spawn /usr/bin/printf "Cisco Weekly Config Backup" | /bin/mailx -send -r root -s "Back
up ASA505 Config Report" -a /var/production/log/dl.txt support@mydomt.com

exit 0

Any ideas?
you don't need close and wait after each spawn
I tried that also and the commands are not working

I am SSH'd in to the fedora server using Cygwin on my Windows 7 computer

Could that have anything to do with it?
I've requested that this question be deleted for the following reason:

Resolved by myself.

Can not run more than one spawn in script according to my findings, Or do not know how to do that. I took the commands out of the script with the spawn command and created another bash script. Then executed that script at the end of the spawn script. Also had to add EXPECT EOF to the spawn script to close out expect.  All Works now Below are revised and second script.

+++++++++++++++++++++++++ Script++++++++++++++++++++++++++++++++++++++

#!/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";
expect "MY-ASA#"
send "enable\r";
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"
expect eof
exec ./ciscobackup.sh
exit 0

+++++++++++CISCOBACKUP.SH+++++++++++++++


#!/bin/bash
rm /var/production/log/dl.txt -v
ls -al /mnt/WindowsTFTP >> /var/production/log/dl.txt
# send Mail 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


Hope this helps others.
I am not switch expert, but does "exit" command let you exit the enable mode or logs you out of the remote session?

please add

expect eof

before the next spawn

You may also use system in place of spawn. e.g.

system  "rm /var/production/log/dl.txt"
system "ls /var/tftproot | cat>/var/production/log/dl.txt"
# Send Email w/attachment
system "/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"
Thanks for responding again.

The send "exit\r"   ends the SSH session.

I tried the system statement you suggested that worked except for the last statement
see below.

quit
221 2.0.0 Service closing transmission channel
Connection closed by foreign host.
removed `/var/production/log/dl.txt'
extra characters after close-quote
    while executing
"system "/usr/bin/printf "Cisco Weekly Config Backup" | /bin/mailx -send -r root -s "Backup ASA5505 Config Report" -a /var/production/log/dl.txt supor..."
    (file "./test.sh" line 20)


My test.sh

#!/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 telnet 192.168.1.26 25
expect "220"
send "quit\r";
expect eof
#close
#wait
system "rm /var/production/log/dl.txt -v"
system "ls -al /mnt/WindowsTFTP >> /var/production/log/dl.txt"
# send Mail w/attachment
system "/usr/bin/printf "Cisco Weekly Config Backup" | /bin/mailx -send -r root -s "Backup ASA5505 Config Report" -a /var/production/log/dl.txt support@mydom.com"
exit 0
please do not delete, rather accept your own solution, so that others can benefit from this
omarfarid

Sounds good.

Did you see my pot about the system statement that is in error?

Any thoughts?

Would rather give points if we can solve this.
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
omarfarid

that worked thanks a lot.
welcome