Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script Help needed

Posted on 2013-11-04
3
Medium Priority
?
292 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
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.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

886 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