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 07-24-2018, 02:49 PM
Hang Hang is offline
Registered User
 
Join Date: Jul 2016
Posts: 18
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,

Last edited by Hang; 07-24-2018 at 02:57 PM.
Reply With Quote
  #2  
Old 07-24-2018, 05:30 PM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 559
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?
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 07-26-2018, 04:33 PM
Hang Hang is offline
Registered User
 
Join Date: Jul 2016
Posts: 18
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,
Reply With Quote
  #4  
Old 07-27-2018, 09:36 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 559
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?
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
Reply

Tags
lost connection , network issue , reconnect during script


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 03:56 AM.