Solved

Script Help Needed

Posted on 2013-11-07
14
455 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

13 Experts available now in Live!

Get 1:1 Help Now