?
Solved

Expect Script and config downloads

Posted on 2008-06-25
11
Medium Priority
?
2,102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 29

Expert Comment

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

Author Comment

by:ritru
ID: 21868698
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 29

Expert Comment

by:Jan Springer
ID: 21868835
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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

Author Comment

by:ritru
ID: 21868998
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 29

Expert Comment

by:Jan Springer
ID: 21869067
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
 

Author Comment

by:ritru
ID: 21869107
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 29

Expert Comment

by:Jan Springer
ID: 21869268
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
ID: 21869469
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 29

Expert Comment

by:Jan Springer
ID: 21869646
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
ID: 21877261
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 29

Accepted Solution

by:
Jan Springer earned 1200 total points
ID: 21878089
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Hello , This is a short article on how would you go about enabling traceoptions on a Juniper router . Traceoptions are similar to Cisco debug commands but these traceoptions are implemented in Juniper networks router . The following demonstr…
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…
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…
Suggested Courses

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