Solved

Script Help Needed

Posted on 2013-11-07
14
458 Views
Last Modified: 2013-11-08
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?
0
Comment
Question by:Thomas Grassi
  • 7
  • 7
14 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 39630983
Did you try to spawn commands?

e.g.

spawn rm /var/production/log/dl.txt
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39631109
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?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39632430
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
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39632447
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?
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39632459
you don't need close and wait after each spawn
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39632504
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?
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39633704
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.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 40

Expert Comment

by:omarfarid
ID: 39633541
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"
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39633674
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
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39633705
please do not delete, rather accept your own solution, so that others can benefit from this
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 39633816
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.
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 39634420
Ok, it could be due to "" nesting. I replaced the inner " with ' as below, try it

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"
0
 
LVL 23

Author Closing Comment

by:Thomas Grassi
ID: 39634524
omarfarid

that worked thanks a lot.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39634592
welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now