Solved

Script Help needed

Posted on 2013-11-04
3
279 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS- KeepAlived notify script not working 23 81
High Bandwidth Usage 6 56
how can I get powershell to show the history of commands used ? 10 56
Rate limit for DNS queries 7 75
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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.

770 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