View Single Post
  #38  
Old 02-28-2012, 11:33 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

I have a modified example that should handle problems with connecting to remote hosts.

What happens if you run the following example?
Code:
# $language = "VBScript"
# $interface = "1.0"
Sub Main
    strUsername = "user" 
    strPassword = "password" 
    strPrompt = "#"
    Set fso = CreateObject("Scripting.FileSystemObject")
    strDeviceList = "s:\riggs\Test-router-1.txt"
    Set fil = fso.OpenTextFile(strDeviceList)
    ' Start loop to iterate over all devices defined in file
    While Not fil.AtEndOfStream
        ' Process a line at a time
        vLine = Split(fil.ReadLine, ";")
        strHostname = vLine(0)
        strIP = vLine(1)
        strProtocol = vLine(2)
        ' Generate a connection string
        Select Case strProtocol
            Case "Telnet"
                cnxnString = "/TELNET " & strIP & " 23"
            Case "SSH2"
                cnxnString = "/SSH2 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP & ", True"
            Case "SSH1"
                cnxnString = "/SSH1 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP & ", True"
        End Select	
        'Handle possible errors when connecting
        crt.Dialog.MessageBox "Connect string:" & vbcrlf & vbtab & cnxnString 
        On Error Resume Next
            crt.Session.Connect cnxnString
            strErr = Err.Description
            nError = Err.Number
        On Error Goto 0        
        If crt.Session.Connected = False Then 
            crt.Dialog.MessageBox "The following host cannot be reached: " &_
            vbcrlf & vbtab & strHostname & vbcrlf & vbcrlf &_
            "With error: " & vbcrlf & vbtab & strErr
        Else
            ' Enable Synchronous which is necessary to avoid missing data from
            ' remote
            crt.Screen.Synchronous = True
            
            ' Handle Telnet authentication
            If strProtocol = "Telnet" Then
                crt.Screen.Send vbcr
                nIndex = crt.Screen.WaitForStrings("login:", "Password:")
              
                  
                ' Handle authentication starting at username
                If nIndex = 1 Then 
                    crt.Screen.Send strUsername & vbcr
                    crt.Screen.WaitForString "Password:"
                    crt.Screen.Send strPassword & vbcr
                    
                ' Handle authentication starting at password
                Else
                    crt.Screen.Send strPassword & vbcr
                End If
            End If
            ' Confirm that remote is ready
            crt.Screen.WaitForString strPrompt
            ' Start logging to new file
            strLogFile = "C:\Switches\" & strHostname & "-%Y-%M-%D--%h-%m-%s.%t.txt"
            crt.Session.LogFileName = strLogFile
            crt.Session.Log True
            ' Turn off paging
            crt.Screen.Send "term length 0" & vbcr	
            crt.Screen.WaitForString strPrompt
            ' Send commands
            crt.Screen.Send "show proc cpu | inc CPU" & vbcr
            crt.Screen.WaitForString strPrompt
            crt.Screen.Send "show version" & vbcr 
            crt.Screen.WaitForString strPrompt
            ' Stop logging
            crt.Session.Log False
            ' Disconnect from device
            If crt.Session.Connected Then
                crt.Session.Disconnect
            End If
        End If
    Wend
    ' Close device file
    fil.Close 
End Sub
Please note that the example is confirming that the connection is successful prior to moving on to interacting with the device.
__________________
--Todd

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

Last edited by rtb; 02-28-2012 at 01:13 PM.
Reply With Quote