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 09-23-2015, 07:35 AM
jbeasley jbeasley is offline
Registered User
 
Join Date: Sep 2015
Posts: 1
Error on crt.Session.Connect cnxnString

Hello,

I've written/modified a script to connect to a list of devices, issue one command, log the output, and then exit. I am getting the following error on the line: crt.Session.Connect cnxnString

CRT Scripting Runtime Error
Error: Session.Connect: Already Connected
Line 97

I am asked for my username, password, and drive letter for the share which contains the devices file, but I get the error above every time. If I remove the line, the connections persist, but commands are not being passed properly. Does anyone see any glaring errors in the syntax?




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

Sub Main
'========================================================================================
'Obtain username and password
'========================================================================================
	strUserName = crt.Dialog.Prompt (_
		"Please enter your username:", _
		"Username?")
	If strUserName = "" Then Exit Sub

	strPassword = crt.Dialog.Prompt(_
		"Please enter your password:", _
		"Password?", _
		"", _
		True)
	If strPassword = "" Then Exit Sub
	
'========================================================================================
'--------------------------------->Constants<---------------------------------
'========================================================================================
'========================================================================================
'Device File
'========================================================================================
	
	Dim ip
	Dim name
	Dim protocol
	Dim line
	Dim cnxnString
 '========================================================================================
 'Regular Expressions
 '========================================================================================
	Dim reDriveLetterChk	
	Set reDriveLetterChk = New RegExp
	With reDriveLetterChk
		.Pattern = "[a-z]"
		.IgnoreCase = True
	End With
'========================================================================================
'
'----------------------------->Device File Decision<--------------------------
'
'========================================================================================
'========================================================================================
'Learn network share mapping - Map to the path below before running the script and note the drive letter
'========================================================================================
	Dim strDriveLetter
		strDriveLetter = crt.Dialog.Prompt (_
			"Please enter the drive letter for \\files.com\vbscript", _
			"Drive Letter?")
	If strDriveLetter = "" Then
		MsgBox "Drive Letter can not be blank", _
		vbExlamation, _
		"Error!"
		Exit Sub
	End If	

'-----------------------------------------------------------------------------
'Open Devices File
'-----------------------------------------------------------------------------
 
	Dim strDrivePathIOS
		strDrivePathIOS = strDriveLetter & ":\devices-ios.txt"
	
	Dim fsoIOS
	Set fsoIOS = CreateObject("Scripting.FileSystemObject")
				
	Dim filIOS
	Set filIOS = fsoIOS.OpenTextFile(strDrivePathIOS)
'-----------------------------------------------------------------------------
'Connection Methods based from devices file
'-----------------------------------------------------------------------------
	While Not filIOS.AtEndOfStream
		line = filIOS.ReadLine
		
		name = Split(line, ",")(0)
		ip = Split(line, ",")(1)
		protocol = Split(line, ",")(2)
		
		Select Case protocol
			Case "Telnet"
				cnxnString = "/TELNET " & ip & " 23"
			Case "SSH2"
				cnxnString = "/SSH2 /L " & strUsername & " /PASSWORD " & strPassword & " /C 3DES /M MD5 " & ip
			Case "SSH1"
				cnxnString = "/SSH1 /L " & strUsername & " /PASSWORD " & strPassword & " /C 3DES /M MD5 " & ip
		End Select		

'-----------------------------------------------------------------------------
'Turn on sub-routine error handling
'-----------------------------------------------------------------------------
		On Error Resume Next

		crt.Screen.Synchronous = True
		crt.Session.Connect cnxnString
		If protocol = "Telnet" Then
			Dim indexIOS
				index1 = crt.Screen.WaitForStrings("Username:", "Password:")
			
			If indexIOS = 1 Then 
				crt.Screen.Send strUsername & vbCr	
				crt.Screen.WaitForString "Password:"
				crt.Screen.Send strPassword & vbCr
			ElseIf indexIOS = 2 Then
				crt.Screen.Send strUsername & vbCr	
				crt.Screen.WaitForString "Password:"
				crt.Screen.Send strPassword & vbCr
			End If
		End If

'-----------------------------------------------------------------------------
'Collect connect error info if any
'-----------------------------------------------------------------------------
		nError = Err.Number
		strErr = Err.Decscription	

		On Error Goto 0
		If nError <> 0 Then
'-----------------------------------------------------------------------------
'Connection Error Message
'-----------------------------------------------------------------------------
			MsgBox "Error connecting with info: " & name & _
				vbcrlf & vbcrlf & strErr
		Else	
		crt.Screen.Synchronous = True
		crt.Session.Connect cnxnString

		'Name Log File
		crt.Session.LogFileName = name & ".txt"

		If protocol = "Telnet" Then
			Dim index
			index = crt.Screen.WaitForStrings("Username:", "Password:")
			
			If index = 1 Then 
				crt.Screen.Send strUsername & vbCr	
				crt.Screen.WaitForString "Password:"
 				crt.Screen.Send strPassword & vbCr
			ElseIf index = 2 Then
				crt.Screen.Send strUsername & vbCr	
				crt.Screen.WaitForString "Password:"
 				crt.Screen.Send strPassword & vbCr
			End If
		End If

' Start Log
			crt.Session.Log True

' show running configuration, including all lines with 'logging' in them
			crt.Screen.Send "show run | inc logging" & vbCr
			crt.Screen.WaitForString "#"

' Stop Log
			crt.Session.Log False

			crt.Screen.Synchronous = False
			crt.Session.Disconnect
		End if
	Wend
	
 	fil.Close 
End Sub




THANKS!!!

JB

Last edited by rtb; 09-23-2015 at 08:44 AM.
Reply With Quote
  #2  
Old 09-23-2015, 09:00 AM
rtb rtb is offline
VanDyke Technical Support
 
Join Date: Aug 2008
Posts: 4,306
Hi JB,

Thanks for the post.

You can't establish a new connection in a tab if an active connection exists. This is the reason for the message you are getting.

Typically, one will confirm that the tab doesn't have an active connection prior to trying to connect using the Disconnect() method of the Session object, or establish the new connection in a new tab using the ConnectInTab() method of the Session object.

If you use the Disconnect() method, you may also want to enter a loop that doesn't exit until you have confirmed that the tab doesn't have an active connection. This can be done using the Connected property of the Session object.

Does this help you get your script working?
__________________
--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 11:22 AM.