Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #31  
Old 02-27-2012, 08:40 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for the screenshots. The connection string is empty. This is why there is no connection and the script is failing on line 48.

Can you confirm that the path to the list of devices is valid?

Would you post or send the script you are using when you see this issue?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #32  
Old 02-27-2012, 11:09 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
Yes, the file path is there (attached screenshot). And the router device is up on the network and is accessible via telnet.

Below is the code:

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"
                ' Don't wait for connection to complete before moving on because
                ' we are going to handle authentication after connecting.                
                bWait = False
            Case "SSH2"
                cnxnString = "/SSH2 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP
                ' Wait for connection to complete before moving on.
                bWait = True
            Case "SSH1"
                cnxnString = "/SSH1 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP
                ' Wait for connection to complete before moving on.
                bWait = True
            End Select	
        'Handle possible errors when connecting
        crt.Dialog.MessageBox "Connect string:" & vbcrlf & vbtab & cnxcString &_
            vbcrlf & "Should I wait:" & vbcrlf & vbtab & bWait
        On Error Resume Next
            crt.Session.Connect cnxnString, bWait
            strErr = Err.Description
            nError = Err.Number
        On Error Goto 0
        
        ' Enable Synchronous which is necessary for not 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
    Wend
    ' Close device file
    fil.Close 
End Sub
Reply With Quote
  #33  
Old 02-27-2012, 11:16 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
I forgot the attached.
Attached Images
File Type: jpg FileLocation.JPG (125.7 KB, 447 views)
Reply With Quote
  #34  
Old 02-27-2012, 01:18 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

Thanks for the screenshots and code.

I have an idea to try and ferret out the cause of the issue.

Please add an Else case to the Select Case statement (I included a few lines above and below where it needs to be placed):
Code:
.
.
.
            Case "SSH1"
                cnxnString = "/SSH1 /L " & strUsername & " /PASSWORD " & strPassword & " " & strIP
                ' Wait for connection to complete before moving on.
                bWait = True
            Case Else
                ' We should never get here, but if we do, we need to know.
                crt.Dialog.MessageBox _
                    "strProtocol contains no recognized protocol:" & vbcrlf &_
                    vbtab & strProtocol
            End Select	
.
.
.
If you see a pop-up with the string, what is the protocol?

Does the protocol exactly match one of the valid cases in the Select Case statement?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #35  
Old 02-27-2012, 01:36 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
I inserted the additional script. I've attached the two popups (attached; popup-1.jpg and popup-2.jpg) that I received. the second point to line 53. My line 53 is:

nIndex = crt.Screen.WaitForStrings("login:", "Password:")
Attached Images
File Type: jpg PopUp-1.JPG (12.3 KB, 468 views)
File Type: jpg PopUp-2.JPG (21.8 KB, 499 views)
Reply With Quote
  #36  
Old 02-27-2012, 02:12 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

I found a typo in the code I posted previously. Lines 39 and 40 should be:
Code:
        crt.Dialog.MessageBox "Connect string:" & vbcrlf & vbtab & cnxnString &_
            vbcrlf & "Should I wait:" & vbcrlf & vbtab & bWait
If you correct the variable for the connection string to be spelled correctly, what does the pop-up indicate?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #37  
Old 02-27-2012, 06:07 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
After inserting the two lines of script. The following errors (attached telnet-1.jpg and telnet.jpg-2) involving telnet.
Attached Images
File Type: jpg telnetError-1.JPG (15.6 KB, 466 views)
File Type: jpg telnetError-2.JPG (22.4 KB, 445 views)
Reply With Quote
  #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
  #39  
Old 02-28-2012, 11:56 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
I loaded the script. I've attached the screenshots (there were six, but there's a limit for five). But its a repetitive error.
Attached Images
File Type: jpg TryTest-1.JPG (12.9 KB, 435 views)
File Type: jpg TryTest-2.JPG (21.0 KB, 461 views)
File Type: jpg TryTest-3.JPG (12.9 KB, 467 views)
File Type: jpg TryTest-4.JPG (19.5 KB, 469 views)
File Type: jpg TryTest-5.JPG (12.7 KB, 456 views)
Reply With Quote
  #40  
Old 02-28-2012, 01:12 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

I have edited the script example in post #38. Would you test this and let me know the results?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #41  
Old 02-28-2012, 02:19 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
That's what the five screenshots were from post#38 script run.
Reply With Quote
  #42  
Old 02-28-2012, 02:36 PM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,305
Hi stuartX,

I actually made changes to the script in post #38.

There was a typo and I made some changes to the connection strings that may be affecting you, but not me in my test environment.

Would you run the example again and let me know what your results are with the changes?
__________________
--Todd

VanDyke Software
Technical Support
support@vandyke.com
505-332-5730
Reply With Quote
  #43  
Old 02-29-2012, 05:23 AM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
I loaded the modified script and something interesting happend. The script went through telneted to the first device (attachment line38-1.jpg shows this) and performed all three commands successfully. Then opened a session with second device (attached line38-2.jpg), got prompted with username but it seems like the script sent a carriage return then sent the username (shown in line38-3.jpg (password crossed out in red)). After that it sat there (also shown in line38-3.jpg) I then cancelled which showed stopped at line 52.
Attached Images
File Type: jpg line38-1.JPG (13.6 KB, 476 views)
File Type: jpg line38-2.JPG (13.5 KB, 475 views)
File Type: jpg line-38-3.JPG (57.1 KB, 475 views)
Reply With Quote
  #44  
Old 02-29-2012, 11:47 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,636
Hello stuartX,

Todd is out of the office.

When I test that section of the code posted in #38, it works as expected. Just to verify, the data you redacted (in line38-3.jpg) was the password, not the username?

Also, have you plugged in the correct Username, Password and Prompt on lines 4-5-6 of the example script?

In case anything has been changed, would you post the current rendition of script "Logging Script-Forum-4modified.vbs"?

Redact sensitive data as necessary.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #45  
Old 02-29-2012, 12:56 PM
stuartX stuartX is offline
Registered User
 
Join Date: Feb 2012
Posts: 33
As I mentioned before, the script works on the first device (login, commands, logout), but on the second device it opens a session, but seems like it does a carriage return then sends the username.
Please keep in mind that the username password that I send in the script is not the actual username password used on the three Nexus routers. I did that so that I would not reveal my password on the forum. Also, keep in mind that all three devices have the same username and password. And, if i separately telnet into each one the credentials work.
My actual username is "admin". Below is the code, I'm using:

Code:
# $language = "VBScript"
# $interface = "1.0"
Sub Main
    strUsername = "user" 'not real username
    strPassword = "password" 'not real 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 = "S:\Riggs\" & 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
Reply With Quote
Reply

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 12:44 PM.