Solved

Script Help Needed

Posted on 2013-11-07
14
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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