Solved

Script Help Needed

Posted on 2013-11-07
14
459 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php ssh2_scp_send 1 61
Install Predefined Certificate on Ubunto 4 39
issue in getting eth0 IP in oracale virtual box Linux VM 4 54
LINUX backups with VEEAM 8 82
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" …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

809 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