VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   loop in vbs script (https://forums.vandyke.com/showthread.php?t=12113)

benitocamela 09-29-2015 02:07 PM

loop in vbs script
 
I'm trying to make a loop to check the machine status after throwing some commands, ex:


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

Const ForReading = 1
Const ForWriting = 2

Sub Main

'Dim logfile
'logfile = "C:\TEMP\mysession.log"
'crt.Session.LogFileName = logfile
Dim fso, file, str
Set fso = CreateObject("Scripting.FileSystemObject")
'crt.Session.Log true
' Note: A runtime exception will be generated if 'input.txt' doesn't exist.
'
Set file = fso.OpenTextFile("C:file.txt", ForReading, False)
crt.Screen.Synchronous = True
crt.Screen.Clear

Do while file.AtEndOfStream <> True

str = file.Readline
' Send the line with an appended CR
crt.Screen.Send str & Chr(13)



crt.Screen.WaitForString "assword:",2
' Send your password followed by a carriage return
crt.Screen.Send "password" & VbCr

crt.Screen.WaitForString "@"
crt.Screen.Send "command-1" & VbCr
crt.Screen.WaitForString "@"

crt.Screen.Send "command-2" & VbCr
crt.Screen.WaitForString "@"

After that, i need to launch a command to check the router status.

If the result of this is OK, continuos the script, with a write command for example.

If the result is not OK, the command to check must be launched with a one minute interval until the router is ok,

Do not know how this loop, in the first loop.

Can you help me.

Thanks

rtb 09-29-2015 02:18 PM

Hi benitocamela,

Thanks for the post. We can help answer any specific questions you might have.

We already have an example script that illustrates how you might accomplish your goal. You may need to modify it to meet your specific needs. Here is the location of the script:
https://forums.vandyke.com/showthrea...7294#post37294
Does this help continue to write your script?

benitocamela 10-01-2015 12:49 PM

Thanks rtb
Im looking the way to make the loop repeat the command, while the equipment status is not ok.
But when this loop is finished, the script not continuous with the rest:

Quote:


# $language = "VBScript"
# $interface = "1.0"
' This script demonstrates how to open a text file and it line by
' line to a server.
' Constants used by OpenTextFile()

Const ForReading = 1
Const ForWriting = 2

Sub Main

'Dim logfile
'logfile = "C:\TEMP\mysession.log"
'crt.Session.LogFileName = logfile
Dim fso, file, str
Set fso = CreateObject("Scripting.FileSystemObject")


'crt.Session.Log true
' Note: A runtime exception will be generated if 'input.txt' doesn't exist.
'
Set file = fso.OpenTextFile("C:\TEMP\mysession.log", ForReading, False)

crt.Screen.Synchronous = True
crt.Screen.Clear

Do while file.AtEndOfStream <> True

Str = file.Readline
' Send the line with an appended CR

crt.Screen.Send str & VbCr
crt.sleep 1000

crt.Screen.WaitForStrings "assword:",2
' Send your password followed by a carriage return
crt.Screen.Send "PASWORD" & VbCr

crt.Screen.WaitForString "@"
crt.Screen.Send "COMMAND-1" & VbCr
crt.Screen.WaitForString "@"

crt.Screen.Send "CHECK-STATUS-COMMAND" & VbCr

Do While crt.Screen.WaitForString ("busy" & VbCr)
crt.sleep 20000
crt.Screen.Send "CHECK-STATUS-COMMAND" & VbCr
Loop


crt.Screen.WaitForString "@"
crt.Screen.Send "exit" & VbCr
crt.Screen.WaitForString "@"


Loop
End Sub



which may be the fault?

bgagnon 10-01-2015 03:02 PM

Hi benitocamela,

Todd (rtb) is not available.

Your WaitForString() call will never return if "busy" is not found, because you are not providing a timeout. :)

You will want to add a timeout parameter that will cause WaitForString() to exit if it does not find "busy" in 5 or 10 seconds.

For example:

Code:

nTimeout = 5
Do While crt.Screen.WaitForString ("busy" & VbCr, nTimeout)
    crt.sleep 20000
    crt.Screen.Send "CHECK-STATUS-COMMAND" & VbCr
Loop


benitocamela 10-02-2015 04:13 PM

The scripts works fine.

You solved my problem !!!!

Thank you Brenda!!!

The final script works like this:

Quote:

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

Const ForReading = 1
Const ForWriting = 2

Sub Main

Dim logfile
logfile = "C:\TEMP\mysession.log"
crt.Session.LogFileName = logfile

Dim fso, file, str
Set fso = CreateObject("Scripting.FileSystemObject")
'crt.Session.Log true
' Note: A runtime exception will be generated if 'input.txt' doesn't exist.
'
Set file = fso.OpenTextFile("C:\MY_ROUTERS.txt", ForReading, False)
crt.Screen.Synchronous = True
crt.Screen.Clear

Do while file.AtEndOfStream <> True

str = file.Readline
' Send the line with an appended CR
crt.Screen.Send str & VbCr
crt.sleep 1000


crt.Screen.WaitForString "assword:",2
' Send your password followed by a carriage return
crt.Screen.Send "My_PASSWORD" & VbCr

crt.Screen.WaitForString ">"
crt.Screen.Send "COMMAND_1" & VbCr
crt.Screen.WaitForString ">"

crt.Screen.Send "CHECK_STATUS" & VbCr

nTimeout = 5
Do While crt.Screen.WaitForString ("busy", nTimeout)
crt.sleep 60000
crt.Screen.Send "CHECK_STATUS" & VbCr
Loop
crt.Screen.Send VbCr
crt.Screen.WaitForStrings ">"
crt.Screen.Send "exit" & VbCr

crt.Screen.WaitForString "~]$"

Loop

End sub

bgagnon 10-05-2015 07:08 AM

Hi benitocamela,

Great, thanks for posting an update! :)


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