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 08-13-2016, 03:57 PM
vysh vysh is offline
Registered User
 
Join Date: Oct 2014
Posts: 40
[Python] Weird newlines using ReadString

Hi guys, this code give me something unexpected:

Code:
crt.GetTab(tabNum).Screen.IgnoreEscape = True

# Capture upstream interface information
licaShowUSCommand = "show interface upstream\r"

crt.GetTab(tabNum).Screen.Send(licaShowUSCommand)
licaUS = crt.GetTab(tabNum).Screen.ReadString(licaPrompt, 3)

licaUS = re.sub(licaShowUSCommand, '', licaUS)
This is what shows in SecureCRT after running the code:

Code:
miniCMTS>show interface upstream
Channel  Status   Frequency  Channel Width  Modulation              D30Mode  Channel  Inner Power
                  (MHz)      (MHz)          Profile                          Type     (dbmv)     
1        enable   32.0       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0          
2        enable   38.4       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0          
3        enable   44.8       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0          
4        enable   51.2       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0          
miniCMTS>
This is what gets captured in "licaUS" variable:

Code:
Channel  Status   Frequency  Channel Width  Modulation              D30Mode  Channel  Inner Power

                  (MHz)      (MHz)          Profile                          Type     (dbmv)     

1        enable   30.4       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0          

2        enable   36.8       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0          

3        enable   43.2       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0          

4        enable   49.6       6.4            AtdmaMediumNoiseQpsk    disable  ATDMA    0
Hidden characters enabled (see attachment) -- shows extra CRs. Where do they come from? I am really puzzled...
Attached Images
File Type: png upstream.png (16.5 KB, 362 views)

Last edited by vysh; 08-14-2016 at 03:18 PM.
Reply With Quote
  #2  
Old 08-15-2016, 09:20 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,293
Hi vysh,

The information is likely being sent to SecureCRT that way by the remote.

However, having multiple CRs (carriage returns) would not necessarily cause "new lines". There is only one LF (line feed) being shown in your editor when hidden characters are enabled.

To get a true understanding of the data the remote is sending, you would want to capture a raw log probably.

To create a raw log:
  • Before connecting with the session, select Raw Log Session from SecureCRT's File menu.
  • In the Select Log File dialog, choose a folder and filename in which you would like the log text to be stored and press the Save button. At this point, if you open the File menu, you will notice that the Raw Log Session menu item now has a check-mark next to it; this indicates that raw logging is activated.
  • Now connect to the remote machine and perform the actions that cause the behavior.
  • Disconnect the session and select Raw Log Session from the File menu to turn off the raw logging feature.

If you want us to look at it, since a raw log can contain sensitive information, feel free to send it as an attachment via email to support@vandyke.com. Please reference "Attn Brenda - Forum Thread #12442" in the subject line.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 08-17-2016, 05:41 AM
vysh vysh is offline
Registered User
 
Join Date: Oct 2014
Posts: 40
Thanks for the reply.

I have found out that the extra CRs get added here:
Code:
results_path = os.path.join(os.path.expanduser("~"), "lica_info.txt")
f = open(results_path, 'w')
    f.write(final_results,)
    f.close()
Immediately before that if I crt.Dialog.MessageBox(final_results) everything looks fine, no extra CRs. But in the file it writes "\r\r\n". I am unsure why :-/
For now I just implemented a dirty workaround with
Code:
f.write(final_results.replace('\r', ''))
. It strips all '\r's from the string and Windows transforms single \n's to \r\n and all looks good, but it doesn't solve the problem itself.
While I was looking into my newline issues I decided to additionally copy everything to Clipboard. But I immediately ran into another issue.

Code:
def main():
	text = "First line.\nSecond line\n\nFourth line."

	crt.Clipboard.Text = text

main()
It will not convert \n to \r\n when run in SecureCRT 8.0.2 for Windows. Is this normal?

Last edited by vysh; 08-17-2016 at 09:15 AM.
Reply With Quote
  #4  
Old 08-17-2016, 10:53 AM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,293
Hi vysh,

I am not sure about "extra CRs" (I have not seen a raw log, so I don't know what the remote sends), but the line ending conventions on the differing operating systems *are* different. Windows uses CR/LF, Mac/Linux/Unix uses LF only.

Quote:
It will not convert \n to \r\n when run in SecureCRT 8.0.2 for Windows. Is this normal?
I am not sure what you mean by "run in" ...?

You are putting that literal text into the clipboard, it's not going to convert the line endings.

What problem are you trying to solve at this juncture?
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 08-17-2016, 11:21 AM
vysh vysh is offline
Registered User
 
Join Date: Oct 2014
Posts: 40
I assumed new lines would be converted automatically into their os-specific versions when putting them into the clipboard just as it is the case with open():

https://docs.python.org/2/library/functions.html#open:
Quote:
The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading.

However it looks like they do not get converterd, so you have to use code like:
Code:
    if os.name == 'nt':
        final_results = final_results.replace('\n', '\r\n')

    crt.Clipboard.Text = final_results
Reply With Quote
  #6  
Old 08-17-2016, 12:58 PM
bgagnon bgagnon is offline
VanDyke Technical Support
 
Join Date: Oct 2008
Posts: 4,293
Hi vysh,

Quote:
I assumed new lines would be converted automatically into their os-specific versions when putting them into the clipboard
No, as you found, that is not the case. We do believe there is a Python-specific function that you could use to achieve the desired conversion, but we just don't have that info on-hand. If you find the specific function, or any community members know of it, feel free to post the info.
__________________
Thanks,
--Brenda

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
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 01:03 AM.