Solved

Expect Script and config downloads

Posted on 2008-06-25
11
2,080 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:ritru
  • 6
  • 5
11 Comments
 
LVL 28

Expert Comment

by:Jan Springer
Comment Utility
That's relatively easy.  Do you want one directory for each device?
0
 

Author Comment

by:ritru
Comment Utility
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
 
LVL 28

Expert Comment

by:Jan Springer
Comment Utility
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
 

Author Comment

by:ritru
Comment Utility
ok I see...the $ciscohost is the directory that will be created under the logpath "/router_configs/whatever the directory..Is that correct.....
0
 
LVL 28

Expert Comment

by:Jan Springer
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:ritru
Comment Utility
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
 
LVL 28

Expert Comment

by:Jan Springer
Comment Utility
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
 

Author Comment

by:ritru
Comment Utility
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
 
LVL 28

Expert Comment

by:Jan Springer
Comment Utility
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
 

Author Comment

by:ritru
Comment Utility
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
 
LVL 28

Accepted Solution

by:
Jan Springer earned 400 total points
Comment Utility
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In the world of WAN, QoS is a pretty important topic for most, if not all, networks. Some WAN technologies have QoS mechanisms built in, but others, such as some L2 WAN's, don't have QoS control in the provider cloud.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

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

11 Experts available now in Live!

Get 1:1 Help Now