VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   error handling during crt.session.connect (https://forums.vandyke.com/showthread.php?t=1274)

felix 02-16-2006 07:09 AM

error handling during crt.session.connect
 
Hello,

we want to capture the errors during the login process (wrong password or
router is not reachable).
The prefered solution we found in the documentations are:

On Error Resume Next
Dim sess
sess = crt.Session
sess.Connect "/s server"
if Err.Number <> 0 Then
MsgBox "An error occurred: " & Err.Description
' handle the error here
...
End If

We insered these lines but receive the error messages (object not found).
Normaly the script runs without problems. We are using CRT 3.4, 4.1 (test) and 5.x (trial). Are there ather solutions?

tnygren 02-16-2006 10:51 AM

Hi Felix,

I think I see what is happening.

crt.session is an object in CRT scripting but the script is defining crt.session as a string instead of an object.

To define an object, the 'set' key word must be used.

I have modified the script with the 'set' key word.

Does this information help?

Code:

On Error Resume Next
Dim sess
set sess = crt.Session
sess.Connect "/s server"
if Err.Number <> 0 Then
MsgBox "An error occurred: " & Err.Description
' handle the error here
...
End If


felix 02-16-2006 01:20 PM

Ok you are rigth. The set parameter now solves the problem.
You should change this in your help file too. :)

My basic problem does it not solve. I got the same authentication window
as before. When i run this script and someone else has entered a wrong username or password (located in a file) there is a popup window which stops
the whole scrip: Enter correct userdata..
How can i disable it?

tnygren 02-16-2006 02:02 PM

Hi Felix,

I've searched through the help file but I am not certain as to which section contains the incorrect information.

Could you tell me which section it is in?

Which version contains the incorrect scripting information (the latest official release (5.0.5), the beta release of 5.1 or an older version)?

As for the pop-up box, is it the dialog requesting the correct username and password or a different dialog?

felix 02-20-2006 03:21 AM

In Chapter 10.2 are the wrong informations. (Handling Script Errors) ->V 3.4.8
In Chapter Active X -> Handling Script Errors -> 5.0.5
We dont using the Beta release.

As for the pop-up box, is it the dialog requesting the correct username and password or a different dialog?
It is the dialog requesting the correct login informations. This one have to be handled by our script. Because
this dialog breaks the script flow.

tnygren 02-20-2006 08:48 AM

Hi Felix,

Thanks!

I see that the information is incorrect.

I have let the product manager know so that the help file can be updated for the next release.

felix 02-21-2006 08:17 AM

And how can i solve my problem?

tnygren 02-21-2006 08:42 AM

Hi Felix,

Currently, when a pop-up box comes up, the script will stop as there isn't a method to access the box to enter the correct information.

We have had requests to have this information appear in the main windows instead of a pop-up and are investigating this for possible inclusion in a future version.

I have added this forum post to this request so that a post can be made here if a version of CRT is released with the option.

If you would prefer to be contacted via email, please send a message to support@vandyke.com with a subject of ATTN: Teresa Forum Thread 1274.

Maureen 06-05-2008 11:16 AM

An option that supports entering authentication information in the session window (instead of a dialog) for SSH1/SSH2 sessions has been added to SecureCRT 6.1, which is in pre-beta testing. If you'd like to try it, please send an e-mail to me at Maureen.Jett@vandyke.com.

Maureen

feng shweez 06-25-2008 10:06 PM

I've actually been having a problem with crt.session.connect as well.

My script is supposed to log into a bunch of systems (by looking at a hostfile) and run a number of commands on each. If it can't find a host (no such hostname, timeout) it should just skip it. My problem is that I'm getting "Connection failed" errors after the following line when the hostname can't be found:

sess.Connect("/telnet " & HostName)

The relevant code is here.
Code:

On Error Resume Next

...

Function ConnectSession

  ' Connect Session.
  Err.clear
  ConnectSession = False
 
  sess.Connect("/telnet " & HostName)
  If Err.Number <> 0 Then
      Exit Function
  End If

  text.WaitForString "Username:"
  text.Send BSRUname & vbCr
  text.WaitForString "Password:"
  text.Send BSRPasswd & vbCr
  text.WaitForString "A#"
  ConnectSession = True

End Function

And the entire script is here.
Code:

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

Option Explicit
On Error Resume Next

Dim BSRUname, BSRPasswd, Mask
Dim HostName, HostNum
Dim HostFile
Dim text, sess, pausetime, result

BSRUname = "xyz"
BSRPasswd = "abc"

pausetime = 2000

Set text = crt.Screen
Set sess = crt.Session
text.synchronous = true
Mask = "**************************************************************"

HostFile = "C:\Scripts\test.txt"

Sub Main
  Dim ReadString
  Dim fs, f

  If sess.Connected <> 0 then
      crt.Dialog.MessageBox("Session must not be connected.")
      Exit Sub
  End If

  HostNum = 0
  Set fs = CreateObject("Scripting.FileSystemObject")
  Set f = fs.OpenTextFile(HostFile, 1)
 
  Do While f.AtEndOfStream <> True

    'Extract HostName from file
      ReadString = f.readLine

      If left(ReadString,1) <> "#" Then
        If ReadString <> "" Then
           
                        Hostname = ReadString
            HostNum = HostNum + 1

            If ConnectSession Then

                                ExecuteCommands
                                ExitSession

            End If
        End If
      End if
  Loop
  f.close
  crt.Sleep pausetime

' Restore the window to active if it was minimized
  If crt.Window.State = 0 Or crt.Window.State = 2 Then
      crt.Window.Show 1
  End If

  result = crt.Dialog.MessageBox("Script Finished!")
  text.synchronous = false
 
End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function ConnectSession

  ' Connect Session.
  Err.clear
  ConnectSession = False
 
  sess.Connect("/telnet " & HostName)
  If Err.Number <> 0 Then
      Exit Function
  End If

  text.WaitForString "Username:"
  text.Send BSRUname & vbCr
  text.WaitForString "Password:"
  text.Send BSRPasswd & vbCr
  text.WaitForString "A#"
  ConnectSession = True

End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ExecuteCommands

  ' Show Chassis Status
  text.Send "show chassis status" & vbCr
  text.WaitForString "A#"

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ExitSession

  ' Exit Router
  text.Send "exit" & vbCr
  crt.Sleep pausetime

End Sub

What am I doing wrong? How can I get the error to be properly ignored?

I'm using SecureCRT 5.5 (though I plan on upgrading to 6.0).

Thanks!

kbarnette 06-26-2008 09:17 AM

Hi feng shweez,

If you place an 'On Error Resume Next' line at the top of the 'ConnectSession' function, are you able to see the behavior you desire?

feng shweez 06-26-2008 04:15 PM

That seemed to work. Thanks!

I didn't realize "On error resume next" wouldn't apply to the entire script. Where can I find more information on the scope of those sort of options?

kbarnette 06-26-2008 04:31 PM

Hi feng shweez,

More information is available on the On Error Resume Next option can be found here.

Specifically I would direct your attention to the following paragraph taken from the above-referenced web page:

Quote:

Originally Posted by http://msdn.microsoft.com/en-us/library/53f3k80h.aspx
An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine. When a procedure is exited, the error-handling capability reverts to whatever error-handling was in place before entering the exited procedure.

Feel free to let us know if you have any other questions specific to SecureCRT scripting.

Maureen 12-20-2010 05:04 PM

The ability to supress pop-up error messages when the Connect function is called has been added to a pre-release version of SecureCRT. If you would be interested in trying it, please send e-mail to me at Maureen.Jett@vandyke.com.

Maureen

CAPflyboy 08-05-2019 01:03 PM

I'm using a version of this script with the hopes to loop through a IP Address list of multiple Cisco switches and update the IOS on each stack.
I'm having issues with the loop part of the process and I'm not sure how to debug the steps.

The script runs successfully the first time, but either skips the next IP address or or bypasses the Hostfile. I can't figure out which it is exactly.
If I add a MsgBox just after the hostfile, which stops the process, then it seems to work, but requires me to hit a key to continue the script.

Can anyone help me figure out what I'm doing wrong?


Here is my version of the script (in its entirety).

Code:

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

Option Explicit
On Error Resume Next

Dim BSRUname, BSRPasswd, Mask
Dim HostName, HostNum
Dim HostFile
Dim crtSc, sess, pausetime, result
Dim CRTPath
Dim szConfigFile, szCurrentLine, vElements, MonthDayYear, DayMonth

Set WScript = CreateObject("WScript.Shell")
' WScript.Sleep 2000 'Sleeps for 2 seconds

' This is the config file to keep from embedding username & password in a vbs file
szConfigFile = "E:\FSO\MultiHostCopyLogConfig.txt"

BSRUname = "xyz"
BSRPasswd = "abc"

pausetime = 10000

Set crtSc = crt.Screen
Set sess = crt.Session
' Mask = "**************************************************************"

HostFile = "E:\FSO\IOSUpgdCisco.txt"
'  IOSUpgdCisco.txt is a simple text file
'  with IP addresses on each line. 
'  I use "#" to comment out the ones I don't want to use.
'

Sub Main
  Dim ReadString
  Dim fs, f
  crtSc.synchronous = True

  If sess.Connected <> 0 then
    crt.Dialog.MessageBox("Session must not be connected.")
    Exit Sub
  End If

  HostNum = 0
  Set fs = CreateObject("Scripting.FileSystemObject")
  Set f = fs.OpenTextFile(HostFile, 1)
 
  Do While f.AtEndOfStream <> True

    'Extract HostName from file
      ReadString = f.readLine

      If left(ReadString,1) <> "#" Then
        If ReadString <> "" Then
           
                        Hostname = ReadString
            HostNum = HostNum + 1

            MsgBox "Hostname " & HostName

            ConnectSession
           
                        ExecuteCommands
                       
                        ExitSession
                        pausetime = 5000

        End If
      End if
  Loop
 
End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Function ConnectSession
On Error Resume Next
' MsgBox "ConnectSession"

  ' Connect Session.
  Err.Clear
'  ConnectSession = False

  sess.Connect "/SSH2 /L " & BSRUname & " /PASSWORD " & BSRPasswd & " " & HostName

  If Err.Number <> 0 Then
      Exit Function
  End If
          crt.Screen.WaitForString "#"

'  ConnectSession = True

End Function

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ExecuteCommands
' On Error Resume Next

crtSc.synchronous = true

          crt.screen.send "sh clock" & vbCr
 '        crt.Screen.WaitForString "#"
         
          CaptureDateTime
         
          crt.Screen.Send "archive download-sw /overwrite tftp://10.143.8.5/c2960x-1524E8/c2960x-universalk9-tar.152-4.E8.tar"
          crt.Screen.Send vbCr
          ' Step through the process, waiting for the screen to change
          crt.Screen.WaitForString "Loading c2960x-1524E8/c2960x-universalk9-tar.152-4.E8.tar"
          crt.Screen.WaitForString "[OK - 30320640 bytes]"
          crt.Screen.WaitForString "Loading c2960x-1524E8/c2960x-universalk9-tar.152-4.E8.tar from"
          crt.Screen.WaitForString "examining image."
          crt.Screen.WaitForString "extracting c2960x-universalk9-mz.152-4.E8/info"
          crt.Screen.WaitForString "Stacking Version Number: "
          crt.Screen.WaitForString "FRU Module Version: "
          crt.Screen.WaitForString "Old image for switch 1: "
          crt.Screen.WaitForString "Deleting `flash:/c2960x-universalk9-mz."
          crt.Screen.WaitForString "Warning: Unable to allocate memory to display"
          crt.Screen.WaitForString "Deleting old files from dc profile dir"
'
'          crt.Screen.WaitForString "Deleting old files "
        crt.Screen.WaitForString "All software images installed"
        crt.Sleep 5000
        crt.Screen.WaitForString "#"
        crt.Screen.Send "write"
        crt.Screen.Send vbCr
        crt.Screen.WaitForString "[OK]"
        crt.Screen.Send vbCr
        crt.Screen.Send vbCr
        crt.Sleep 5000       
        crt.Screen.Send "reload at 22:55 " & DayMonth & " IOS update 152-4.E8"
        crt.Screen.Send vbCr
        crt.Screen.WaitForString "Proceed with reload?"
        crt.Screen.Send vbCr
        crt.screen.send "sh clock" & vbCr
'          crt.Screen.WaitForString "EDT"


    crtSc.synchronous = False

End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub CaptureDateTime()
On Error Resume Next

crtSc.synchronous = True

'  Capturing the date / time so it can be used later"

    ' Look for specific output
    crt.Screen.WaitForString "2019"

    ' Once specific output is found, capture the line
    ' of text on which the "EDT" string
    ' was located.
    szCurrentLine = crt.Screen.Get(crt.Screen.CurrentRow, _
                                  0, _
                                  crt.Screen.CurrentRow, _
                                  crt.Screen.Columns)

    ' Trim off any leading or training spaces
    szCurrentLine = Trim(szCurrentLine)
        crt.Sleep 1000
    MonthDayYear = Right(szCurrentLine, 11)
        crt.Sleep 1000
    DayMonth = Left(MonthDayYear, 6)


End Sub


'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub ExitSession
crtSc.synchronous = True


  ' Exit Router
  crtSc.Send "logo" & vbCr
 
crtSc.synchronous = False

End Sub


I'm using SecureCRT Version 8.3.3 (x64 build 1646).

Thanks![/QUOTE]


All times are GMT -6. The time now is 12:35 PM.