Solved

Script Help needed

Posted on 2013-11-04
3
282 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
ID: 39623427
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
ID: 39627760
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
ID: 39630227
that helped

added/changed

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

thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
ifconfig 4 90
error log using ftp 7 75
Syslog-ng works. Now what? How to filter and manage? 8 88
Unix Help with:  mailx -s 17 57
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Fine Tune your automatic Updates for Ubuntu / Debian
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…
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.

679 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