Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-21-2012, 02:08 PM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
Writing Configs to Text Files

I am very new to writing scripts and I have been taking sample configs provided by VanDyke and customizing them. My latest script is rather complicated (in my mind). I have written a script that will allow me to do a sh run in a switch and it will write the config to a text file.

Now, I wrote a script that will pull a name from one text file and an IP from another text file (next step is to learn how to pull info from two columns in a spreadsheet), telnet to the IP, read the config from the switch and write it to another text file. I ran into troubles like it was not exiting from one switch before going to the next. Also, if a device was removed and the name says "VACANT," it will skip that.

I am sure my work-arounds are very sloppy to the experienced script writer.

The problem I have is it will telnet into the switch, create the file, but it does not read the config and write it. I end up with empty text files.

Where did I go wrong?

# $language = "VBScript"
# $interface = "1.0"


Const ForReading = 1
Const ForWriting = 2


Sub Main

Dim fs1, fs2, fs3, file1, file2, file3
Set fs1 = CreateObject("Scripting.FileSystemObject")
Set fs2 = CreateObject("Scripting.FileSystemObject")
Set fs3 = CreateObject("Scripting.FileSystemObject")

Set file1 = fs1.OpenTextFile("C:\Documents and Settings\jacobda\My Documents\Knology\Configs\RAPD\MRV\MRV Names.txt", ForReading, False)
Set file2 = fs2.OpenTextFile("C:\Documents and Settings\jacobda\My Documents\Knology\Configs\RAPD\MRV\MRV IPs.txt", ForReading, False)

Do
While file2.AtEndOfStream <> True

name = file1.Readline
ipadd = file2.Readline

Set file3 = fs3.OpenTextFile("C:\Documents and Settings\jacobda\My Documents\Knology\Configs\RAPD\MRV\" & name & ".txt", ForWriting, True)

crt.Screen.Synchronous = True

crt.Screen.Send "telnet " & ipadd & chr(13)
crt.Screen.WaitForString "login:"
crt.Screen.Send "admin" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "m1ck3y" & chr(13)
crt.Screen.WaitForString ">"
crt.Screen.Send "en" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "m0u53" & chr(13)
crt.Screen.Send "no cli-paging " & Chr(10)
crt.Screen.Send "sh run" & chr(10)
crt.Screen.WaitForString Chr(10)
crt.Screen.WaitForString Chr(10)

Dim waitStrs
waitStrs = Array( Chr(10), "#" )

Dim row, screenrow, readline, items
row = 1

Do
While True

result = crt.Screen.WaitForStrings( waitStrs )

If result = 2 Then
Exit Do
End If

screenrow = crt.screen.CurrentRow - 1
readline = crt.Screen.Get(screenrow, 1, screenrow, 80 )

file.Write readline & vbCrLf
Wend
Loop

crt.Screen.WaitForString "#"
crt.Screen.Send "q" & chr(10)
crt.Screen.WaitForString "djacoby]#"
crt.Screen.Send chr(13)

Wend
Loop

crt.screen.synchronous = false

End Sub
Reply With Quote
  #2  
Old 03-21-2012, 02:29 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi djacoby,

Thanks for the question. First off, you don't need to get a new handle to the file system object for each file you want to use. You could use the following :
Code:
Set fso = CreateObject("Scripting.FileSystemObject")

Set objFile1 = fso.OpenTextFile("C:\Documents and Settings\jacobda\My Documents\Knology\Configs\RAPD\MRV\MRV Names.txt", ForReading, False)
Set objFile2 = fso.OpenTextFile("C:\Documents and Settings\jacobda\My Documents\Knology\Configs\RAPD\MRV\MRV IPs.txt", ForReading, False)
Set objFile3 = fso.OpenTextFile("C:\Documents and Settings\jacobda\My Documents\Knology\Configs\RAPD\MRV\" & name & ".txt", ForWriting, True)
The problem you are having writing a file seems to be the variable you are using to reference the file object handle. Here is your line of code:
Code:
file.Write readline & vbCrLf
I don't see in your code where you have defined the file object.

I suspect that you need to use the following:
Code:
file3.Write readline & vbCrLf
Another suggestion would be to follow every Send() with a WaitForString(). This is a best practice to avoid sending data before the remote device is ready to receive data.

Does this help to resolve the issue?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 03-21-2012, 02:38 PM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
I fixed the file object, but it is still doing the same thing. Actually, I had it the right way before, but just before I gave up and asked for help, I copied the piece of the script I had that worked on a single config retrieval.

Anyway, it still is not working.
Reply With Quote
  #4  
Old 03-21-2012, 04:31 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi djacoby,

Thanks for the update. If you message box the contents of the readline variable, do you see what you expect to see?

For example:
Code:
screenrow = crt.screen.CurrentRow - 1
readline = crt.Screen.Get(screenrow, 1, screenrow, 80 )
crt.Dialog.MessageBox readline
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 03-22-2012, 07:43 AM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
I used MessageBox to help me understand what is going on. When I put it where you suggested, nothing happened. I changed and moved it to tell me the value of "result" just before the IF/Then statement and it already equals at that point, causing the script to exit the Do Loop before it had a chance to read anything.

By the way, I've been rude and not thanking you for your help. So...

Thank you very much for you help.
Reply With Quote
  #6  
Old 03-22-2012, 08:19 AM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
OK... I got it working.

Using the MessageBox, I figured out that the last command sent before the "reading" loop was "crt.Screen.Send "no cli-paging " & Chr(10)". Since the array looks for chr(10), I changed the Chr(10) to Chr(13) for that and the "sh run".

Now that I got this working, is there a sample config I can look at that will show me how to read cells from a spreadsheet? The script I was using read from two text files I created by copying two columns out of a spreadsheet and putting each column in a separate text file.

Thanks again.
Reply With Quote
  #7  
Old 03-23-2012, 03:02 PM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
We got it working. rtb was a big help. I attached a copy of the script to show the result of our hard work (mostly his).
Reply With Quote
  #8  
Old 03-24-2012, 10:54 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi djocoby,

Thanks for posting your working script. I am happy to help. Hopefully it will help others.

When you get your script reading from and writing to Excel spreadsheets/workbooks, feel free to post that as well. There seem to be many calls in the forums for scripts that use Excel rather than flat text files.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 05:05 AM.