#1
|
|||
|
|||
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.(.....) 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 Thanks, Last edited by Hang; 07-24-2018 at 01:57 PM. |
#2
|
|||
|
|||
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)
__________________
Thanks, --Eric VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
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, |
#4
|
|||
|
|||
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
__________________
Thanks, --Eric VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
![]() |
Tags |
lost connection , network issue , reconnect during script |
Thread Tools | |
Display Modes | Rate This Thread |
|
|