#1
|
|||
|
|||
Can not disconnect - python scripting
Hello,
I have some questions please :: 1. I am trying to log into lots of modems and get some basic info from them, all i want is the current software version and serial number from them. When i log into the devices, there is no guarantee that the format of the text will be the same as the last, so i am struggling to use waitForString, because, a certain string might not appear, so is there a way to read all the output from a certain input ???? 2. I am trying to use the below code on 200 ish devices, however, on certain devices, it hangs, as in, i log in, the code runs, the display is on the screen, but, nothing happens, it does not disconnect ?? Its always the same devices that it hangs on, yet i can use secureCRT to log into them directly, and log out, however it will no disconnect when i run the code, so i wonder, is there a more forcible way to disconnect other than crt.Session.Disconnect() ??? 3. I find it annoying not being able to output 'type' to screen, i.e. crt.Dialog.MessageBox(type(what_ever_variable)) its got to be a string, so .... crt.Dialog.MessageBox(str(type(what_ever_variable))) But, obviusly that just sends String to the screen, there must be a better way to debug the code. Any help would be greatly appreciated Code:
# $language = "python" # $interface = "1.0" errcode = 0 user1 = "USERNAMEONE" passwd1 = "PASSWORDONE" def main(): crt.Screen.Synchronous = True num = 0 errorMessages = "" what_you_looking_for = "enable\rshow version" # Open the results.txt file and start writing the XML document file = open('results.xml' , 'w') file.write("<?xml version=\"1.0\"?>\n") file.write("<results>\n") file.close() file = open('hosts.txt', 'r') hosts = file.readlines() file.close() file = open('serial_numbers.txt', 'r') serial_numbers = file.readlines() file.close() for each_node in hosts: connector(each_node) crt.Screen.Send("enable\rshow version\rHe77") strResult = crt.Screen.ReadString("He77") strResult = str(strResult) dummy = serial_numbers[num].rstrip() crt.Dialog.MessageBox(str(dummy)) file = open('results.xml' , 'a') file.write("<output>" + strResult + "</output>\n") file.write("</host>") file.close() num += 1 disconnect_me() def disconnect_me(): if crt.Session.Connected: crt.Session.Disconnect() crt.Dialog.MessageBox("Should now disconnect") def all_done(): file = open('results.xml' , 'a') file.write("</results>\n") file.close() crt.Dialog.MessageBox("Thats me all done.") def connector(each_node) : # try :: try and connect using your TACACS credentials try: # Check to see if we are connected, and if we are disconnect the current session if crt.Session.Connected: crt.Session.Disconnect() # try and log in with the first credentials cmd = "/SSH2 /L %s /ACCEPTHOSTKEYS /PASSWORD %s /C 3DES /M SHA1 %s" % (user1, passwd1, each_node.rstrip()) crt.Session.Connect(cmd) if crt.Session.Connected: return except ScriptError: error = crt.GetLastErrorMessage() main() all_done() |
#2
|
|||
|
|||
Hi andymc,
What version of SecureCRT are you using? On what platform/OS? I will try to address some of your inquiries in my next post, but for sure I can see instances where you have forgotten the best practice of always having a WaitForString() following a Send(): Code:
crt.Screen.Send("enable\rshow version\rHe77") That can definitely cause issues and even though our scripting manual is for VBScript, the concept is still applicable. See section 4.2.
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
#3
|
|||
|
|||
Hello,
What version of SecureCRT are you using? - Version 7.2.5 (x64 build 550) - Official Release - June 5, 2014 On what platform/OS? - Windows 7 Professional Code:
crt.Screen.Send("enable\rshow version\rHe77") Code:
crt.Screen.Send("show version") Code:
rHe77 Code:
strResult = crt.Screen.ReadString("He77") Andy |
#4
|
|||
|
|||
Hi andymc,
Even if you break that out to 3 lines, the point is there should be a WaitFor between each. Usually you wait for the shell prompt. You stated in the earlier post that there was not really any common text you could use in the WaitFors. Is there not even some pattern, such as the hostname you connect to being part of the prompt, or the device name? I would definitely recommend you look at our example script that illustrates how to connect to many hosts and send commands. Where your objective differs is that you need to get certain pieces of data from the output but there have been a few posts of a similar nature in the forums that illustrate that. Quote:
Changes in SecureCRT 7.2 (Beta 1) -- October 8, 2013 ---------------------------------------------------- New features:
__________________
Thanks, --Brenda VanDyke Software Technical Support support@vandyke.com (505) 332-5730 |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|