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 06-19-2018, 05:03 AM
winniec winniec is offline
Registered User
 
Join Date: Jun 2018
Posts: 5
Question 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:

import os
LOG_DIRECTORY = os.path.join(
os.getcwd(), 'Xunjian') ##This needs to change the location?
COMMANDS = [
"show version",
"show system",
"show cpuinfo",
"show meminfo",
"show ip route",
"show envm",
"show log",
#"show tech",
"show ha log",
"show cable modem summary total"
]


def main():

if not os.path.exists(LOG_DIRECTORY):
os.mkdir(LOG_DIRECTORY)

if not os.path.isdir(LOG_DIRECTORY):
crt.Dialog.MessageBox(
"Log output directory %r is not a directory" % LOG_DIRECTORY)
return

##You can delete the bellowing def and disable the definition of the above if you already have tabs connected.
AutoConnectTab("SessionList.txt")
######If you don't want to manual enter the user/pwd name. Then you can enable the AutoConnect def.
#AutoConnect("SessionList.txt")

###Confirm if it is safe to input commands
while True:
if not SCRIPT_TAB.Screen.WaitForCursor(1):
break

#####Get the tab, and send commands to the tabs
skippedTabs=""
Count=crt.GetTabCount()

for i in range(1, Count+1):
tab = crt.GetTab(i)
tab.Activate()
##Get the hostname
strCmd = "show run | in ^hostname\r"
tab.Screen.Send(strCmd)
tab.Screen.WaitForStrings([strCmd + "\r", strCmd + "\n"])
host = tab.Screen.ReadString(["\r", "\n"])
if "" in host:
Host = host.split()[1]
hostname=Host[1:-1]
else:
Host = "unknown-hostname"
hostname=Host

##Identify the filename and enable the logging
tab.Session.LogFileName = os.path.join(LOG_DIRECTORY, hostname + '.txt')

if tab.Session.Connected==True:
tab.Session.Log(True) ##Save the session log
tab.Screen.Send("page-off\r")

###Send the commands to the tab
for command in COMMANDS:
try:
tab.Screen.Send(command+"\n")
tab.Screen.WaitForString("#",2)
except:
return

else: ##Check if there is any skippedTabs
if skippedTabs=="":
skippedTabs=str(i)
else:
skippedTabs=skippedTabs+","+str(i)

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
#SCRIPT_TAB.Activate()


if skippedTabs!="":
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))


LaunchViewer(LOG_DIRECTORY)

def LaunchViewer(filename):
try:
os.startfile(filename)
except AttributeError:
subprocess.call(['open', filename])


def CreateObjTab(user, password,session):
objNewTab = crt.Session.ConnectInTab("/TELNET %s 23" % session)
objNewTab.Screen.WaitForString("login:")
objNewTab.Screen.Send(user + "\r")
objNewTab.Screen.WaitForString("Password:")
objNewTab.Screen.Send(password + "\r")
objNewTab.Screen.WaitForString(">")
objNewTab.Screen.Send("enable\r")
objNewTab.Screen.WaitForString("Password")
objNewTab.Screen.Send("casa\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):
return
sessionFile = open(file, "r")
sessionArray = []
for line in sessionFile:
session = line.strip()
if session:
sessionArray.append(session)
sessionFile.close()
# Receive variable
user = crt.Dialog.Prompt("Enter user name:", "Login", "", False)
password = crt.Dialog.Prompt("Enter password:", "Login", "", True)
for session in sessionArray[0:]:
try:
CreateObjTab(user, password,session)
except ScriptError:
pass
if not SCRIPT_TAB.Session.Connected:
return


main()
Reply With Quote
  #2  
Old 06-19-2018, 08:13 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 3,557
Hi winniec,

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 = True
Even 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:
Code:
        ###Send the commands to the tab
            for command in COMMANDS:
                try:
                    tab.Screen.Send(command+"\n")
What your script does is very close to one of our example scripts in the forums here so you might look it over for further tips/clues.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 06-21-2018, 06:18 AM
winniec winniec is offline
Registered User
 
Join Date: Jun 2018
Posts: 5
Quote:
Originally Posted by bgagnon View Post
Hi winniec,

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 = True
Even 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:
Code:
        ###Send the commands to the tab
            for command in COMMANDS:
                try:
                    tab.Screen.Send(command+"\n")
What your script does is very close to one of our example scripts in the forums here so you might look it over for further tips/clues.
Thanks for the response.
I have a synchronized code at the very beginning. Sorry I did not paste entirely.
SCRIPT_TAB=crt.GetScriptTab()
SCRIPT_TAB.Screen.Synchronous=True
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"):
tab.Session.Log(False)
tab.Session.Disconnect()

Though it works....but seems not so perfectly...
Reply With Quote
Reply

Tags
command , diconnect , loggging , tab


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 08:40 AM.