VanDyke Software Forums

VanDyke Software Forums (
-   Scripting (
-   -   Variable from File likely csv (

aohareuri 08-20-2016 11:40 AM

Variable from File likely csv

I'm looking to create a script, preferring VB, that will read variables from a CSV file and insert them into the script in a loop.

File content example

There are about 300 rows in the document, the below is a sample and not actual IPs.

Looking to have the script (once already connected to a jump server) cycle through each host in the TelnetTo column.

HTML Code:

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

crt.Screen.Synchronous = True

Sub Main
Set fso = CreateObject("Scripting.FileSystemObject")

Set hosts = fso.OpenTextFile("SomePath\SomeFile.csv")

MyVar = Split(hosts.ReadAll, vbcrlf)


For Each thing In MyVar
        crt.Screen.Send "telnet " & thing & chr(13)
        If crt.Screen.WaitForStrings ("NA-US-R3-JUMPsvr105#", "username: ") = 2 then
                crt.Screen.Send "user" & chr(13)
                crt.Screen.WaitForString "password: "
                crt.Screen.Send "pass" & chr(13)
                crt.Screen.WaitForString ">"
                crt.Screen.Send "en" & chr(13)
                crt.Screen.Send "pass" & chr(13)
                crt.Screen.WaitForString "#"
                crt.Screen.Send "conf t" & chr(13)
                crt.Screen.WaitForString "(config)#"
                crt.Screen.Send "interface " & thing1 & chr(13)
                crt.Screen.Send "ip address " & thing2 & "" & chr(13)
                crt.Screen.WaitForString "(config)#"
                crt.Screen.Send "end" & chr(13)
                crt.Screen.WaitForString "#"
                crt.Screen.Send "wr" & chr(13)
                crt.Screen.WaitForString "#"
                crt.Screen.Send "exit" & chr(13)
                crt.Screen.WaitForString "NA-US-R3-JUMPsvr105#"
                crt.Screen.Send chr(13)
                crt.Screen.WaitForString "NA-US-R3-JUMPsvr105#"
        End If
End Sub

jdev 08-22-2016 09:59 AM

Here are the steps I would suggest you take to create such a script:
  1. Handle secondary host logins correctly/reliably. Take a look at the example script "JumpHost-HandleSecondaryHostConnectionAttempt" found on the script examples "sticky". On line 32, you'll be editing that to perform a 'telnet' command instead of an 'ssh' command.
  2. Rename 'thing' to be more useful for you knowing exactly what it is (something like "strCurLine", for instance)
  3. Use another Split() call within your main 'For Each thing in MyVar' loop that separates out each component into tokens separated by comma characters. For example vTokens = Split(strCurLine, ",")
  4. Use appropriately-named variables to peel out the various tokens for use within your script. For example:

    strIP = vTokens[0]
    strWAN1 = vTokens[1]
    strWAN1IP = vTokens[2]
    '... etc.

    ...instead of thing1, and thing2 because this will help you to remember/know exactly what the variable means.

Also, take a look through the script examples available on the Script Examples "sticky". There are lots of examples that show techniques to perform tasks and there's code already there for the learning. For example, if you already have a spreadsheet with this information in it, there's an example of how to use the information directly from the spreadsheet to connect to a host (even from a gateway/jumphost scenario) and perform a series of commands -- see the "ExcelSpreadsheets-SearchingAndLookup-GatewayLogins" code in the Integrating with Microsoft Excel script example.


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