#1
|
|||
|
|||
![]()
I'm trying to combine the two python scripts 'SendData.py' and 'GetDataToFile.py'. I can get both scripts to work independently of each other but not together. The platform is Palo Alto Panorama.
Code:
# $language = "python" # $interface = "1.0" import os output_path = 'path to output folder' show_script = 'script with cli commands' def main(output_file): crt.Screen.Synchronous = True filename = os.path.join(output_file, 'output.txt') fp = open(filename, "wb+") for line in open(show_script, 'r'): crt.Screen.Send(line) crt.Screen.WaitForString("\n", 2) promptStr = '>' waitStrs = ["\n", promptStr] row = 1 while True: result = crt.Screen.WaitForStrings( waitStrs ) if result == 2: break screenrow = crt.Screen.CurrentRow - 1 readline = crt.Screen.Get(screenrow, 1, screenrow, 500) fp.write(readline + os.linesep) crt.Screen.Synchronous = False main(output_path) Last edited by cboyack; 01-19-2021 at 03:40 PM. Reason: Please use the [CODE] and [/CODE] tags to denote a script |
#2
|
||||
|
||||
Quote:
I see that your script code expects to find a "\n" in some places after sending a line/command to the device. It has been my experience that a number of Palo Alto devices never send any Carriage Return (\r) or any NewLine (\n) sequence on lines where a command is sent. Instead of sending a \r or a \n to signify that a command has been received, such devices typically send a blizzard of seemingly unnecessary escape sequences that instruct SecureCRT to move the cursor to the beginning of the line, back to the end of the line, to the top of the screen, back to the bottom of the screen, clear the line, redraw the line, etc. in an indeterminate way that is difficult to make much sense of. Capture a raw log (File > Raw Log Session) of a connection to such a device where you are issuing a few simple commands manually (don't use your script).
Quote:
If you have a specific question, provide the specific details and we'll try to give you information that corresponds to your inquiry. --Jake
__________________
Jake Devenport VanDyke Software Technical Support YouTube Channel: https://www.youtube.com/vandykesoftware Email: support@vandyke.com Web: https://www.vandyke.com/support |
#3
|
|||
|
|||
Thanks
Thank you for the comprehensive and informed reply. I will investigate and post my results here soon. (Big thanks on showing me where to find information on crt python.)
|
#4
|
|||
|
|||
Working Example
The raw logs proved useful by pointing me in the direction of basically spamming the WaitForString method (Palo CLI uses CRLF at the end of each line btw). The following works for me, although some of the output printed to both the screen and the file sometimes gets garbled if the send/response experiences lag. Hopefully someone else can benefit from this. Thanks again for your help.
Code:
# $language = "python" # $interface = "1.0" import os output_path = 'path to output file' input_file = 'path to input file' def main(output_file): crt.Screen.Synchronous = True filename = os.path.join(output_file, 'output.txt') fp = open(filename, "w+") strs = '\r\n' for line in open(input_file, 'r+'): crt.Screen.Send(line) crt.Sleep(500) crt.Screen.WaitForString('edit' or 'set' or '\r\n', 2) screenrow = crt.Screen.CurrentRow - 1 readline = crt.Screen.Get(screenrow, 1, screenrow, 500) fp.write(readline + os.linesep) crt.Screen.Synchronous = False main(output_path) Last edited by jdev; 01-22-2021 at 10:25 AM. Reason: Please wrap code with a [CODE] tag at the beginning and a [/CODE] tag at the end. |
#5
|
||||
|
||||
Quote:
Try running the following script example if you want a demonstration of how your attempt at WaitForString('edit' or 'set' or '\r\n') won't work, but using WaitForStrings() with a list of strings could potentially work. Code:
# $language = "Python" # $interface = "1.0" crt.Screen.Synchronous = True whatToWaitFor = 'edit' or 'set' or '\r\n' # The strange python code on the line above # results in a string that has only the # value of the word "edit". Why? Because # python interprets the statement as a logic # expression, evaluating the logical tokens # (each separated by the keyword 'or') in # order from left to right -- and since the # first string found ('edit') is not empty, # it is interpreted as True, so the value # of that first string is assigned to the # variable whatToWaitFor crt.Dialog.MessageBox( "Test #1: WaitForString() - Singular\n" + "whatToWaitFor is:\n{}".format(whatToWaitFor) + "\n\n" + "After clicking [OK]...\n" + "\t1) Press {Enter}\n" + "\t2) Type the word 'set'\n\n" + "...all within 5 seconds") nResult = crt.Screen.WaitForString(whatToWaitFor, 5) crt.Dialog.MessageBox("WaitForString() data...\n" + "Called with arg: {}\nReturned: {}\nMatchIndex: {}".format( whatToWaitFor, nResult, crt.Screen.MatchIndex)) # In this second test, rather than using an # ambiguous boolean expression as was done in # Test #1 above, we create a list of strings... whatToWaitFor = ['edit', 'set', '\r\n'] crt.Dialog.MessageBox( "Test #2: WaitForStrings() - Plural\n" + "whatToWaitFor is:\n{}".format(whatToWaitFor) + "\n\n" + "After clicking [OK]...\n" + "\t1) Type the word 'set' (again)\n" + "\t2) [does not matter]\n\n" + "...You won't get to step 2.") # ... and once we have that list of strings, we call # the WaitForStrings() method (plural), passing in # that list as the argument to WaitForStrings() nResult = crt.Screen.WaitForStrings(whatToWaitFor, 5) crt.Dialog.MessageBox("WaitForStrings() data...\n" + "Called with arg: {}\nReturned: {}\nMatchIndex: {}".format( whatToWaitFor, nResult, crt.Screen.MatchIndex)) Calling WaitForString(), WaitForStrings(), or ReadString() with a timeout parameter will generally introduce timing and synchronization problems especially when you don't handle the timeout case should it occur. As is, your call to WaitForString times out after 2 seconds and you don't do anything to handle that timeout scenario (or even check to see if a timeout condition occurred). Reliable script execution is best achieved by not using any timeouts when calling WaitForString(), WaitForStrings(), or ReadString(); tell SecureCRT what you want to wait for and do not allow the script to move on with subsequent code statements until what you're looking for was actually found. I'm happy to hear that you are connecting to a device that sends EOL characters instead of cursor movement escape sequences. Quote:
--Jake
__________________
Jake Devenport VanDyke Software Technical Support YouTube Channel: https://www.youtube.com/vandykesoftware Email: support@vandyke.com Web: https://www.vandyke.com/support |
#6
|
|||
|
|||
Ah, I did not know it was a time-out - thought I was forcing it to wait at least 2 seconds. Thanks for the script and advice. I'll give it a go.
|
#7
|
|||
|
|||
Testing hasn't produced a better solution
I've been testing variations of this script without any luck. Its current state seems to work best for what I'm doing (though it's far from perfect). I'll update this if I ever come across something better. Thanks again for the information. It was really helpful.
|
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|