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 01-19-2021, 03:38 PM
afdslkj2r afdslkj2r is offline
Registered User
 
Join Date: Jan 2021
Posts: 5
Post Cannot print full screen output to file

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)
The screen output is chewed up as are the lines captured in the output file. Oddly, the output file never captures the full output. I've scoured the forums, the internet, etc, to no avail. Any help would be appreciated, even if it's just a link somewhere with solid information about SecurtCRT methods/attributes.Thank you.

Last edited by cboyack; 01-19-2021 at 03:40 PM. Reason: Please use the [CODE] and [/CODE] tags to denote a script
Reply With Quote
  #2  
Old 01-19-2021, 05:46 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,068
Quote:
Originally Posted by afdslkj2r View Post
The screen output is chewed up as are the lines captured in the output file. Oddly, the output file never captures the full output.
If you're connecting to a Palo Alto device, chances are that you're getting odd results because those devices can send odd output.

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).

  • After a few commands, turn off logging and open that log file in an editor like SCITE or Notepad++ in which you can "see" non-printing characters like {ESC}, {CR}, {LF}, etc. and try to make sense of what you see there.

    • Are there any patterns that you notice (on the lines containing the actual command that is sent) which you might possibly be able to use in your scripting?
      Sure, the output generated by a command may very well contain EOL characters like \n and \r, but the line on the screen where the PA device has echo'd the command you typed in? I'm guessing that it won't be as simple as waiting for a \n or a \r, after a command is sent and you'll need to adjust your script accordingly.

Quote:
Originally Posted by afdslkj2r View Post
I've scoured the forums, the internet, etc, to no avail. Any help would be appreciated, even if it's just a link somewhere with solid information about SecurtCRT methods/attributes.Thank you.
Documentation about SecureCRT's scripting API is contained within SecureCRT's built-in help.
  1. Click on SecureCRT's main Help pull-down menu and choose Help Topics.
  2. When the help window appears, select the Table of Contents, navigate to the top-level Scripting section; then navigate to the Script Objects Reference section.
    • NOTE: If you are running SecureCRT on Linux/macOS platforms, you may need to collapse the Introduction section in order to see the other top-level topics that are in the help (like Scripting!).

    There, you'll see a list of all the top-level scripting objects provided by SecureCRT. Within each object, you'll see a list of the properties and methods provided by each object.
    Scripting API Reference - SecureCRT for Windows:


    Scripting API Reference - SecureCRT for Linux/macOS:

If you have a specific question, provide the specific details and we'll try to give you information that corresponds to your inquiry.

--Jake
Attached Images
File Type: png SCRT_Help_ScriptAPI_Windows.png (218.4 KB, 98 views)
File Type: png SCRT_Help_ScriptAPI_Linux+macOS.png (165.5 KB, 88 views)
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #3  
Old 01-20-2021, 12:59 PM
afdslkj2r afdslkj2r is offline
Registered User
 
Join Date: Jan 2021
Posts: 5
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.)
Reply With Quote
  #4  
Old 01-22-2021, 09:55 AM
afdslkj2r afdslkj2r is offline
Registered User
 
Join Date: Jan 2021
Posts: 5
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.
Reply With Quote
  #5  
Old 01-22-2021, 11:31 AM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,068
Quote:
Originally Posted by afdslkj2r View Post
The raw logs proved useful by pointing me in the direction of basically spamming the WaitForString method
Tip #1: The 'edit' or 'set' or '\r\n' code is an ambiguous boolean statement and in the end it results in a string that is only comprised of the word 'edit'.

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))
Tip #2:
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.

Quote:
Originally Posted by afdslkj2r View Post
(Palo CLI uses CRLF at the end of each line btw).
I'm happy to hear that you are connecting to a device that sends EOL characters instead of cursor movement escape sequences.

Quote:
Originally Posted by afdslkj2r View Post
...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.
I suspect that most of the problems you're seeing here are because of the ambiguous boolean statement you tried to use as well as the use of timeouts when calling WaitFor...

--Jake
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #6  
Old 01-22-2021, 11:50 AM
afdslkj2r afdslkj2r is offline
Registered User
 
Join Date: Jan 2021
Posts: 5
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.
Reply With Quote
  #7  
Old 01-29-2021, 10:03 AM
afdslkj2r afdslkj2r is offline
Registered User
 
Join Date: Jan 2021
Posts: 5
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.
Reply With Quote
Reply


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 10:04 PM.