Expect Script and config downloads

I have an expect script I'm using to download my router configs automatically via telnet...I've ran into a problem where I want to download the configs in different directories instead of one big directory. The script is below...


#This is an expect script that telnets to all the Cisco routers
#found in the "routerlist" file and captures/downloads the configuration
#of the router and "show tech" data to a specified file.
#
#This program is set to run every Monday and Friday 12:30am est using
#a crontab file located under /var/spool/cron/crontabs/router_scriptcron
#
#
#
#
#Author:Anthony Davis

################################################################################

#
set logpath "/export/home/anthony/routerdownloads/"
set routerlistname "~/etc/routerlist.txt"
#
#
#Get Passwords from User
#
#stty -echo
#send_user "Login Password:"
#expect_user -re "(.*)\n"
#send_user "\n"
#set password $expect_out(1,string)
#send_user "Enable Password:"
#expect_user -re "(.*)\n"
#send_user "\n"
#set Enapwd $expect_out(1,string)
#
#Passwords below are for automated cron jobs:
set username "test"  
set password ""   
set Enapwd ""
#
 
set ciscohost "dummy_data"
set datestring [exec date +%Y%m%d]
set timeout 20
 
#create directory (if it doesn't exist)
if {!([file exists $logpath/cisco.$datestring])} then {
     set MakeDir [exec mkdir $logpath/cisco.$datestring]}
 
#telnet to Cisco's router or routers -- get configuration:
set hostlist [open $routerlistname r]
while {[gets $hostlist ciscohost] > 0} {
#
#Telnet to the Cisco router or routes:
     spawn telnet test@$ciscohost
     expect {
           "password:"     {
#              send "$username\r"
               send "$password\r"
               expect ">" 
               send "enable\r$Enapwd\r"
               expect "#"
               send "term len 0\r"
 
#Logged into the router:
#Now get the configuration:
               set logfile "$logpath/cisco.$datestring/$ciscohost.$datestring.co
nf"
               log_file -noappend $logfile
               expect "#"  
               send "wr t\r"
               expect "#"
               log_file
               send "\r"
               expect "#"
#Now get the "show tech" information:
               set logfile "$logpath/cisco.$datestring/$ciscohost.$datestring.te
ch"
               log_file -noappend $logfile
               send "show tech\r\r\r"
               expect "#"
               expect "#"
               log_file
               close
               wait
               }
          timeout {
               close
               wait
               }}
}                      
close $hostlist
ritruAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Jan SpringerConnect With a Mentor Commented:
Yes, you can do it like you want.  

You will need an lindex of 0, 1 and 2 and a variable for each.

while {[gets $hostlist cisco_info] > 0} {
   set location [lindex [split $cisco_info :] 0]
   set ciscohost [lindex [split $cisco_info :] 1]  
   set ciscohost2 [lindex [split $cisco_info :] 2]  

duplicate each $ciscohost request with a $ciscohost2 request.

or you do it the 2nd way and i can write a you a quick script (if you're not familiar with perl) to output a file similar to what i mentioned -- then your expect script can remain short.
0
 
Jan SpringerCommented:
That's relatively easy.  Do you want one directory for each device?
0
 
ritruAuthor Commented:
If I'm reading your response right.....Yes I want a directory for each device....The directory has already been created...For example

sun# pwd
//routers
sun#
router1
router2
router3
router4

Each device will correspond to the directory...What modification can use for the expect script to accomplish this task
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
Jan SpringerCommented:
set logfile "$logpath/cisco.$datestring/$ciscohost/$ciscohost.$datestring.co
set logfile "$logpath/cisco.$datestring/$ciscohost/$ciscohost.$datestring.te

presuming that this is the explicit path that you want.
0
 
ritruAuthor Commented:
ok I see...the $ciscohost is the directory that will be created under the logpath "/router_configs/whatever the directory..Is that correct.....
0
 
Jan SpringerCommented:
Yes and is exactly how I did it (but using perl with expect module).

The name of the router went into a directory of its own name.
0
 
ritruAuthor Commented:
I just looked at the directories and all the routers...It is specific but the routers are labeled for a site and site has many routers for example...

/routers/Georgia
geo_r1.06.24.08
geo_r23.06.24.08

/routers/Florida
florida_r1.06.24.08
03345i_florida_r2

And so on.... Sorry for the confusion of not explaining it properly..Would I need to create second script for the me to call...
0
 
Jan SpringerCommented:
No.

Change routerlist.txt:

Georgia,geo_r1.06.24.08
Georgia,geo_r23.06.24.08

Florida,florida_r1.06.24.08
Florida,03345i_florida_r2

Where you have this:
While {[gets $hostlist ciscohost] > 0} {

Change to this:
while {[gets $hostlist cisco_info] > 0} {
   set location [lindex [split $cisco_info :] 0]
   set ciscohost [lindex [split $cisco_info :] 1]  

And:
set logfile "$logpath/cisco.$datestring/$location/$ciscohost.$datestring.co

Does this sound about right?
0
 
ritruAuthor Commented:
Sounds good...But the routerlist is huge....I usually have a list of ip's under the routerlist for it to pull from and the script pulls the name from the router is self....

Could I just put the directory in the routerlist and then the ip's next to the intended directory..
0
 
Jan SpringerCommented:
If you mean:

Georgia,192.168.1.11
Florida,10.10.100.4

yes, adjust the variable names accordingly.

if you mean something else, can you mock up an example for me?
0
 
ritruAuthor Commented:
Can I do it like this...

georiga,10.4.1.61,10.4.1.62,10.4.1.63,10.4.1.34
florida, 1.1.1.1,2.2.2.2

Or I have to do it like this
georiga,10.4.1.62
georiga,10.4.1.63
georiga,10.4.1.34
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.