Python Disconnect will make the command unfinished
I have a script that auto connect to ip address which is in a file "SessionList.txt". One system for one tab. Then, we have a list of commands that will sent to each tab and enable logging.
However, if I disable the logging or disconnect in the end, I will miss output of some of my commands. Would you please help to solve this?
It will take about one seconds with running the command except "show log". The "show log" command will flushing the screen. And I could not determine how long it will take to complete. Anyway, it may take more than 3 seconds.
Even I change the tab.Screen.WaitForString("#",2) to "20" but no help. I still get my commands unfinished with the disconnect code.
It is also weird that the MessageBox will pop up even my commands have not finished. It normally pop up when the "show log" is printing. Why would it happen?
Thank you very much.
Here is my script:
LOG_DIRECTORY = os.path.join(
os.getcwd(), 'Xunjian') ##This needs to change the location?
COMMANDS = [
"show ip route",
"show ha log",
"show cable modem summary total"
if not os.path.exists(LOG_DIRECTORY):
if not os.path.isdir(LOG_DIRECTORY):
"Log output directory %r is not a directory" % LOG_DIRECTORY)
##You can delete the bellowing def and disable the definition of the above if you already have tabs connected.
######If you don't want to manual enter the user/pwd name. Then you can enable the AutoConnect def.
###Confirm if it is safe to input commands
if not SCRIPT_TAB.Screen.WaitForCursor(1):
#####Get the tab, and send commands to the tabs
for i in range(1, Count+1):
tab = crt.GetTab(i)
##Get the hostname
strCmd = "show run | in ^hostname\r"
tab.Screen.WaitForStrings([strCmd + "\r", strCmd + "\n"])
host = tab.Screen.ReadString(["\r", "\n"])
if "" in host:
Host = host.split()
Host = "unknown-hostname"
##Identify the filename and enable the logging
tab.Session.LogFileName = os.path.join(LOG_DIRECTORY, hostname + '.txt')
tab.Session.Log(True) ##Save the session log
###Send the commands to the tab
for command in COMMANDS:
else: ##Check if there is any skippedTabs
tab.Session.Log(False) ##I will miss my last two commands here.
tab.Session.Disconnect() ##If I add "#" with the two commands. All the commands could be finished.
##Go back to the original one
skippedTabs="\n\nThe following tabs did not receive the command:"+skippedTabs
crt.Dialog.Message("We have skippedTabs:" + str(skippedTabs))
crt.Dialog.MessageBox("The following command was sent to all connected tabs:" + str(Count))
def CreateObjTab(user, password,session):
objNewTab = crt.Session.ConnectInTab("/TELNET %s 23" % session)
objNewTab.Screen.Send(user + "\r")
objNewTab.Screen.Send(password + "\r")
def AutoConnectTab(file): ###Auto connect sessions from a txt
####This is to Open the "SessionList.txt" and get a list of the ip address
if not os.path.exists(file):
sessionFile = open(file, "r")
sessionArray = 
for line in sessionFile:
session = line.strip()
# Receive variable
user = crt.Dialog.Prompt("Enter user name:", "Login", "", False)
password = crt.Dialog.Prompt("Enter password:", "Login", "", True)
for session in sessionArray[0:]:
if not SCRIPT_TAB.Session.Connected:
One thing that jumps out at me immediately is that I don't see where you have ever set Synchronous to True:
tab.Screen.Synchronous = TrueEven though it's a VBScript manual, Scripting Essentials explains in chapter 4 why this is so vital to be sure you don't miss data (section with Avoid "Missing" Data with Screen.Synchronous = True as the title).
Also, a minor thing, in one spot you are using \n with the Send() instead of \r:
###Send the commands to the tab for command in COMMANDS: try: tab.Screen.Send(command+"\n")
I have a synchronized code at the very beginning. Sorry I did not paste entirely.
SCRIPT_TAB.Screen.IgnoreEscape = True
I solved this issue with adding one command in the command list. Then filter the key word in the last word to decide whether we stop the tab.
Add one more "show version" in the COMMAND list. Then add these at the end:
if tab.Screen.WaitForStrings("Running Image"):
Though it works....but seems not so perfectly...
|command , diconnect , loggging , tab|
|Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)|
|Display Modes||Rate This Thread|