Solved

Script Help needed

Posted on 2013-11-04
3
271 Views
Last Modified: 2013-11-07
Wrote this script on a Fedora 14 Server. SSH into my Cisco ASA 5505 router.
Have TFTP server running on another Windows 2008 Server.
Manually the commands work.

The script I want to automate this process.

The SSH part works.

The copy command fail because the script never sends the ENABLE and password.
For the Copy command to work you need ENABLE and password.

Script is below.


#!/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";
# interact
send "enable"
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
0
Comment
Question by:Thomas Grassi
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
Comment Utility
Can you add a line like

expect "prompt" (put actual expected prompt)

before the send enable

you may also use autoexoect to create the script. Please see link below on autoexpect

http://expect.sourceforge.net/example/autoexpect.man.html
0
 
LVL 23

Author Comment

by:Thomas Grassi
Comment Utility
Thanks that worked.

I ran it but at the end the last few lines in the script did not run

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



The files were copied to my tftp server folder.  That's great

The spawn  expect and send seems to have not cloased and pass control back to the bash commands.

Am I missing a command in the script?  I thought CLOSE would do it.

Trying to email my self after the process so I know it runs.

Look at script in my first postings.

made these changes to get it to work

expect "MY-ASA#"
send "enable\r";

Thanks
0
 
LVL 23

Author Closing Comment

by:Thomas Grassi
Comment Utility
that helped

added/changed

expect "MY-ASA#"
send "enable\r";

thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…

762 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

8 Experts available now in Live!

Get 1:1 Help Now