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 04-19-2012, 03:00 PM
ddnllc ddnllc is offline
Registered User
 
Join Date: Apr 2012
Posts: 7
Empty files using objFile.Write

Hello,

I have edited one of the scripts found on this site to save configs from a Cisco device. It works fine " most of the time". The problem I am having is that some of the text files saved have no data, resulting in empty files. But it isnt consistent. It also does not happen for the same device twice. If I have 15 devices in file used for reading, 12 may work correctly while the other 3 may have no data in the written file. Normally with other scripts I have used, I have the crt.Screen.Synchronous = True set. But with this script, when it is enabled, all files are empty, meaning no data is written. Because of this, I am assuming the problem I am having has something to do with the synchronous data being intermittently missed. Below is a sample copy of the script. Please review and let me know if there is something misconfigured that is causing my problem. Thanks.

Note: I have commented out a message box line and the synchronous line that I use for testing.


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

Sub Main

' crt.Screen.Synchronous = True

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1
Const ForWriting = 2

Dim strSwIP

Dim strSwName

Set objFile1 = fso.OpenTextFile("C:\switches.txt", ForReading, False)

Dim fil

Set fil = fso.OpenTextFile("C:\switches.txt")

While Not fil.AtEndOfStream

line = fil.ReadLine

strSwIP = Split(line, ";")(0)

strSwName = Split(line, ";")(1)

Set objFile3 = fso.OpenTextFile("C:\" & strSwName & ".txt", ForWriting, True)


crt.Screen.Send strSwIP & VbCr
crt.Screen.WaitForString "Password:"
crt.Screen.Send "password" & VbCr
If (crt.Screen.WaitForString(">", 3)) Then
crt.Screen.Send "en" & VbCr
crt.Screen.WaitForString "Password:"
crt.Screen.Send "password" & VbCr
else
End If


crt.Screen.Send "term len 0" & VbCr
crt.Screen.WaitForString "#"
crt.Screen.Send "sh run" & VbCr
crt.Screen.WaitForString VbCr
strResult = crt.Screen.ReadString(strSwName & "#")
' MsgBox strResult
objFile3.Write strResult

crt.Screen.Send "exit" & VbCr
crt.Screen.WaitForString "closed by foreign host"


Wend

fil.Close


End Sub
Reply With Quote
  #2  
Old 04-19-2012, 03:55 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi ddnllc,

I don't see anything particularly wrong with the script other than the not setting Synchronous equal to True.

If you include the following line prior to the first Send() statement, and uncomment the line where you message box the results of the ReadString() line, do you see anything in the message box?
PHP Code:
crt.Screen.Synchronous True 
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #3  
Old 04-19-2012, 04:13 PM
ddnllc ddnllc is offline
Registered User
 
Join Date: Apr 2012
Posts: 7
Quote:
Originally Posted by rtb View Post
Hi ddnllc,

I don't see anything particularly wrong with the script other than the not setting Synchronous equal to True.

If you include the following line prior to the first Send() statement, and uncomment the line where you message box the results of the ReadString() line, do you see anything in the message box?
PHP Code:
crt.Screen.Synchronous True 
When I add the line, I dont see any text for any of the devices. But when it is commented out, it works most of the time. During my testing before I would either have "MsgBox strResult" or "objFile3.Write strResult", not both. This time I left them both uncommented and noticed something odd. When the write fails or there is no text in the message box, the message box is displayed immediately after the sh run command is executed. When there is box content and the write is successfuly, it seems that the script is waiting for the "sh run" to complete. I'm not sure if I am explaing this clearly, so let me know if you need further details.
Reply With Quote
  #4  
Old 04-19-2012, 05:02 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi ddnllc,

Thanks for the update. I think I understand what you are saying, but I don't have your devices to test against, so this is something that you will need to troubleshoot.

With that said, I do have some suggestions for troubleshooting this issue.

Line 4 has a Send() which is not followed by a WaitForString(). This may work most of the time, but can fail.
PHP Code:
crt.Screen.Synchronous True
crt
.Screen.Send strSwIP VbCr
crt
.Screen.WaitForString "Password:"
crt.Screen.Send "password" VbCr 
This block of code could be problematic. It is likely better to handle authentication in a case statement before doing any work.
PHP Code:
If (crt.Screen.WaitForString(">"3)) Then
    crt
.Screen.Send "en" VbCr
    crt
.Screen.WaitForString "Password:"
    
crt.Screen.Send "password" VbCr
End 
If 
PHP Code:
crt.Screen.Send "term len 0" VbCr 
Confirm that you are waiting for a unique string. Is it possible that this string appears in output of the previous command prior to finding the prompt? Perhaps you should use strSwName here like you do in the ReadString().
PHP Code:
crt.Screen.WaitForString "#"
crt.Screen.Send "sh run" VbCr
crt
.Screen.WaitForString VbCr
strResult 
crt.Screen.ReadString(strSwName "#")
MsgBox strResult
objFile3
.Write strResult

crt
.Screen.Send "exit" VbCr
crt
.Screen.WaitForString "closed by foreign host" 
Does this information help you find the cause of the issue?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #5  
Old 04-20-2012, 08:08 PM
ddnllc ddnllc is offline
Registered User
 
Join Date: Apr 2012
Posts: 7
Thank you!!!

Quote:
Originally Posted by rtb View Post
Hi ddnllc,

Confirm that you are waiting for a unique string. Is it possible that this string appears in output of the previous command prior to finding the prompt? Perhaps you should use strSwName here like you do in the ReadString().
PHP Code:
crt.Screen.WaitForString "#"
crt.Screen.Send "sh run" VbCr
crt
.Screen.WaitForString VbCr
strResult 
crt.Screen.ReadString(strSwName "#")
MsgBox strResult
objFile3
.Write strResult

crt
.Screen.Send "exit" VbCr
crt
.Screen.WaitForString "closed by foreign host" 
Does this information help you find the cause of the issue?
Thank you very much! It seems it as exactly as you had suggested. The wait string needed to be unique. I edited it to be "crt.Screen.WaitForString strSwName & "#". It now works perfectly. This forum is absolutely underated. I hope you forum admins are appreciated (and compensated) appropriately by Vandyke. I expect to see the scripting forum grow. Thanks again.
Reply With Quote
  #6  
Old 04-21-2012, 02:08 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
You are welcome ddnllc. I am glad I could help.

I wanted to point you to one additional suggestion. I mentioned using a case statement to handle authentication. Here is an example that may help you make your script more robust:
http://forums.vandyke.com/showpost.p...63&postcount=2
__________________
--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 01:21 AM.