Solved

Expect Script and config downloads

Posted on 2008-06-25
11
2,093 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
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 28

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 28

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 28

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 28

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 28

Accepted Solution

by:
Jan Springer earned 400 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Tired of waiting for your show or movie to load?  Are buffering issues a constant problem with your internet connection?  Check this article out to see if these simple adjustments are the solution for you.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

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