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-22-2012, 09:32 AM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
UGH!!!

I added one more thing to my script to basically skip the rest of the process if the attempt to telnet times out and now it is not copying the config again. Result = 2, killing the loop before it starts.

crt.Screen.Send "telnet " & ipadd & chr(13)

Timeout = crt.Screen.WaitForStrings( "login:" )

If Timeout = 1 Then

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


I put the End If at the end of the Do Loop at the bottom of the script.
Reply With Quote
  #8  
Old 03-22-2012, 09:34 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
You are welcome djacoby! I am happy to help.

Thanks for the updates. I am glad to hear that you have your script working for multiple hosts.

Excellent use of the Message Box debugging technique.

I have another suggestion. Unless there is some specific reason that necessitates reading the configuration and write it a line at a time, the it would likely be better to send the command and read the config in two steps. For example, you could replace the following lines of code with just a few that would be less prone to error:
Code:
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 "#"
Here are the few lines that would replace the code above:
Code:
crt.Screen.Send "no cli-paging " & vbcr
crt.Screen.WaitForString "#"
crt.Screen.Send "sh run" & vbcr
crt.Screen.WaitForString vbcr
strResult = crt.Screen.ReadString "#"
objFile.Write strResult
I will address the Excel information in a subsequent post.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #9  
Old 03-22-2012, 09:47 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi djacoby,

Would you post your complete script as you have it now, and let me know what you are attempting to accomplish with the new code that you added? I think that we need to get the script working before moving on to the Excel question.

I would also suggest making the changes to how the script reads the data and writes the data prior to posting unless there is a reason that you need to read and write a line at a time. If there is a reason for reading and writing a line at a time, I am curious to know what it helps you accomplish (I am not a network engineer).
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #10  
Old 03-22-2012, 10:25 AM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
Here is the complete script. I am trying to tell it to run the commands only when it is connected to a switch. If it tries to telnet to an IP that times out, my script stops. I am trying to get it to detect when the telnet times out and bypass the rest:

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


Const ForReading = 1
Const ForWriting = 2


Sub Main

Dim fso
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)

Do
While objfile2.AtEndOfStream <> True

name = objfile1.Readline
ipadd = objfile2.Readline

If name <> "VACANT" Then

Set objFile3 = fso.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)

Timeout = crt.Screen.WaitForStrings( "login:" )

If Timeout = 1 Then

crt.Screen.Send "admin" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "*****" & chr(13)
crt.Screen.WaitForString ">"
crt.Screen.Send "en" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "*****" & chr(13)
crt.Screen.Send "no cli-paging " & Chr(13)
crt.Screen.Send "sh run" & chr(13)
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 )

objfile3.Write readline & vbCrLf
Wend
Loop

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

End If
End If

Wend
Loop

crt.screen.synchronous = false

End Sub
Reply With Quote
  #11  
Old 03-22-2012, 10:31 AM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
I tried your simplified script and got an error message:

When I run the script, I get "Expecting end of statement"
Line 48

strResult = crt.Screen.ReadString "#"


Here is the whole script:

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


Const ForReading = 1
Const ForWriting = 2


Sub Main

Dim fso
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)

Do
While objfile2.AtEndOfStream <> True

name = objfile1.Readline
ipadd = objfile2.Readline

If name <> "VACANT" Then

Set objFile3 = fso.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)

Timeout = crt.Screen.WaitForStrings( "login:" )

If Timeout = 1 Then

crt.Screen.Send "admin" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "*****" & chr(13)
crt.Screen.WaitForString ">"
crt.Screen.Send "en" & chr(13)
crt.Screen.WaitForString "Password:"
crt.Screen.Send "*****" & chr(13)

crt.Screen.Send "no cli-paging " & vbcr
crt.Screen.WaitForString "#"
crt.Screen.Send "sh run" & vbcr

crt.Screen.WaitForString vbcr
strResult = crt.Screen.ReadString "#"
objFile3.Write strResult

End If
End If

Wend
Loop

crt.screen.synchronous = false

End Sub
Reply With Quote
  #12  
Old 03-22-2012, 11:10 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi djacoby,

I think that I would like to have you begin with a script that I know works, and we can add to it as you find features that you want to add. I have attached a script that illustrates how to handle authentication in the terminal window when connecting through a jump host. It has a Case...Statement that handles all possible responses from the remote host as defined in an array. In the script it handles SSH and Telnet, and you could pair it down if you want, but it should run even if you are just using Telnet.

Here is how you would accomplish your config writing goal in the script example:
Code:
            Case 1,2
                ' Found "-->" or "$" which in our example indicate successful
                ' authentications.
                
                ' Do work we need to complete on host including sending the exit
                ' command which will disconnect us from the secondary host
                ' allowing us to loop up to the top and connect to the next
                ' host.
                crt.Screen.Send "no cli-paging " & vbcr
                crt.Screen.WaitForString "#"
                crt.Screen.Send "sh run" & vbcr

                crt.Screen.WaitForString vbcr
                strResult = crt.Screen.ReadString "#"
                objFile3.Write strResult
                
                ' Now that we have done the necessary work (including waiting
                ' for an indication that the last command we sent above has
                ' completed, send "exit" to disconnect from secondary host.
                crt.Screen.Send "exit" & vbcr
                
                ' Wait for an indication that the exit command was successful
                ' before attempting to connect to next host.
                crt.Screen.WaitForString g_strMainPrompt
                
                ' Exit inner Do..Loop since we are done with the success case.
                Exit Do
You will need to customize the possible prompts you might see in your environment, and you will need open your files rather than use the array of defined hosts.

Does this help you get a running script?

If so, let me know and I will move to the Excel code.
Attached Files
File Type: txt JumpHost-HandleSecondaryHostConnectionAttempts.vbs.txt (7.0 KB, 293 views)
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #13  
Old 03-22-2012, 11:43 AM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
I removed all of my extras and put in your piece and I get the forementioned error. I attached the screen print of that error.

Do you want to continue working on this in the public forum or work on it another way and post the final results when it's working?
Attached Images
File Type: png Error Message.png (34.8 KB, 334 views)
Reply With Quote
  #14  
Old 03-22-2012, 11:51 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi djacoby,

We can continue to work this in the forum, or we can move it to e-mail. It may be easier to work on this via e-mail. If you would like to move to e-mail, please send a message to support@vandyke.com with a subject of Attn: Todd - Forum thread #10359. Please include the script you are using that has the error you posted.

If you want to keep this in the forums, have you downloaded the attached example and modified it to use in your environment, or are you still using your original script?

If you are still using the original, please begin using the new example. It illustrates how one can deal with the authentication/connection problems you are seeing. If you need more guidance in customizing the example I attached, please let me know specifically what you don't understand and I will help either in this forum or via e-mail.
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #15  
Old 03-22-2012, 12:03 PM
djacoby djacoby is offline
Registered User
 
Join Date: Nov 2011
Posts: 14
Taking to e-mail.
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 09:57 AM.