VanDyke Software Forums

VanDyke Software Forums (https://forums.vandyke.com/index.php)
-   Scripting (https://forums.vandyke.com/forumdisplay.php?f=14)
-   -   Continuing/Reconnect a script after a network interruption (https://forums.vandyke.com/showthread.php?t=13190)

Hang 07-24-2018 01:49 PM

Continuing/Reconnect a script after a network interruption
 
Hi,

Is there a way to reconnect a connection and continue with the script after a network interruption that causes a session to disconnect?

For example:

If I am running a script and I am sending text on the screen every 3 seconds. Say in between the 3 seconds I have a network glitch and I lost connection, when the 3 seconds is up when it try to send the next command, I would get an error message saying, see below, and my script stop.

Code:

Script Error 
Error: Screen.Send: not connected

File: C:\....
Line 231

Screen.Send.(.....)

Is there a way to config SecureCRT to reconnect and continue on with the script, if a connection is lost?

OR is there a way to ignore this "Error: Screen.Send: not connected" and let the script run, not stop the script completely? I ask because I have implemented a check connection routine at the beginning of the loop to reconnect if the connection got disconnected.

Here is my script:

Code:


checkConn = 0
connAlive = 1
while(1):
 checkConn += 1
 if checkConn != connAlive:
  crt.Session.Connect("/telnet xxx.xxx.xxx.xxx", true, true) 
  checkConn = 0
  connAlive = 0
 
 crt.Screen.Send("hello world\r")
 crt.sleep(3)
 crt.Screen.Send("hello world2\r")
 
 crt.Screen.Send("alive\r")
 if waitfor("alive"):
  connAlive += 1

Say if connection is lost between the hello world and hello world2, I want the script to move on, when it repeat at the top it will know a connection has been lost and will reconnect.

Thanks,

ekoranyi 07-24-2018 04:30 PM

Hi Hang,

I think the key is going to be using try/except to wrap the commands that may result in an exception. I've included some example code below that should help demonstrate this.

Code:

# $language = "Python"
# $interface = "1.0"

objTab = crt.GetScriptTab()
objScreen = objTab.Screen

# Loop forever
while True:
   
    # If the session is not connected attempt
    # to connect
    if not objTab.Session.Connected:
        try:
            objTab.Session.Connect()
        except:
            pass
   
    # If the session is connected run the below
    # commands
    try:
        objScreen.Send("hello world\r")
        objScreen.Send("hello world\r")
        objScreen.Send("alive\r")
        objScreen.WaitForString("alive")
    except:
        pass

    # Sleep 3 seconds between each iteration of
    # the while loop.
    crt.Sleep(3000)

Does this help you get the behavior you're looking for?

Hang 07-26-2018 03:33 PM

Hi ekoranyi,

That works, however, my code in the "try body" is longer than the simple example I give.

I find that when I incorporate my code in your example code, I find that it depends on when I reconnect the connection it will sometimes start at the middle of my code in the "try body". Is there a way to make the script to always start at the beginning of the "try body" when it recover from a lost connection?

Thanks,

ekoranyi 07-27-2018 08:36 AM

Hang,

One change that comes to mind would be add an If statement that only allows the try block to run if you are currently connected. this would look something like the below code.

Code:

if objTab.Session.Connected:
    try:
        objScreen.Send("hello world\r")
        objScreen.Send("hello world\r")
        objScreen.Send("alive\r")
        objScreen.WaitForString("alive")
    except:
        pass

Does making this change impact the behavior you see?


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