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 07-24-2018, 03:43 AM
startek startek is offline
Registered User
 
Join Date: Jul 2018
Posts: 9
automate running repetitive commands in SecureCRT terminal

I want automate running repetitive commands in SecureCRT terminal emulator for Linux, use Python script. Script read flash memory blocks on device by sequentially running 4 commands for each block. Blocks have unique addresses(hex), specified in blocks.txt file (61 address total, lines are separated by newlines). The script should sequentially read these addresses from the list and run four 'readmem' commands for the each address. First address from the list are specified in code (80020000), the rest 61 are in the list. I modified some of python script example for this, it does part of the job, run the command cycle for the first block, so I need do the same for the remaining 61 blocks. How to modify script for this?
Code:
# $language = "Python"
# $interface = "1.0"


def main():
    errorMessages = ""

if crt.Session.Connected:
    crt.Screen.Synchronous = True

# When we first connect, there will likely be data arriving from the
# remote system.  This is one way of detecting when it's safe to
# start sending data.
while True:

    if not crt.Screen.WaitForCursor(1):
        break
# Once the cursor has stopped moving for about a second, we'll
# assume it's safe to start interacting with the remote system.

# Get the shell prompt so that we can know what to look for when
# determining if the command is completed. Won't work if the prompt
# is dynamic (e.g. changes according to current working folder, etc)
row = crt.Screen.CurrentRow
prompt = crt.Screen.Get(row, 0, row, crt.Screen.CurrentColumn - 1)
prompt = prompt.strip()

crt.Screen.Send("cd system\r")
# Wait for the command to complete, by looking for the prompt to
# appear once again.
crt.Screen.WaitForString(prompt)

crt.Screen.Send("readmem -s 2 -n 16384 0x80020000\r")
# Wait for the command to complete, by looking for the prompt to
# appear once again.
crt.Screen.WaitForString(prompt)

crt.Screen.Send("readmem -c\r")
crt.Screen.WaitForString(prompt)

crt.Screen.Send("readmem -c\r")
crt.Screen.WaitForString(prompt)

crt.Screen.Send("readmem -c\r")
crt.Screen.WaitForString(prompt)

# Start new cycle for new address from the list.txt (61 address)
... ...
crt.Screen.Send("exit\r")

main()
Reply With Quote
  #2  
Old 07-24-2018, 09:09 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 501
Hi startek,

It sounds like you will need to be able to select a file, then perform a set of actions for each line in the file. One way to accomplish this is using a For loop. I've included an example of selecting a file, reading that file, and performing actions For each line.

Code:
# $language = "Python"
# $interface = "1.0"

import os

def main():
    # Select the blocks file to use
    strBlocksFilePath = crt.Dialog.FileOpenDialog(
        title="Select blocks file to use",
        filter="")
    if strBlocksFilePath == '':
        return
    
    # Open the selected file
    file = open(strBlocksFilePath, 'r') 

    # Do the following for each line in the blocks file
    for line in file: 
        crt.Screen.Send("readmem -s 2 -n 16384 0x" + line + "\r")
        #<command 2>
        #...
            
    #Close the file
    file.close()

main()
Does this help get you the functionality you're looking for?
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #3  
Old 07-24-2018, 02:31 PM
startek startek is offline
Registered User
 
Join Date: Jul 2018
Posts: 9
Hi,

thank you for the sample code. This code block could be added into the code I posted, correct?
I got error:
SyntaxError

Error: 'return' ouside function
Line 47
return

Code:
# $language = "Python"
# $interface = "1.0"

import os


def main():
    errorMessages = ''


if crt.Session.Connected:
    crt.Screen.Synchronous = True

# When we first connect, there will likely be data arriving from the
# remote system.  This is one way of detecting when it's safe to
# start sending data.

while True:

    if not crt.Screen.WaitForCursor(1):
        break

# Once the cursor has stopped moving for about a second, we'll
# assume it's safe to start interacting with the remote system.

# Get the shell prompt so that we can know what to look for when
# determining if the command is completed. Won't work if the prompt
# is dynamic (e.g. changes according to current working folder, etc)

row = crt.Screen.CurrentRow
prompt = crt.Screen.Get(row, 0, row, crt.Screen.CurrentColumn - 1)
prompt = prompt.strip()

crt.Screen.Send('cd system\r')

# Wait for the command to complete, by looking for the prompt to
# appear once again.

crt.Screen.WaitForString(prompt)

# Select the blocks file to use

strBlocksFilePath = crt.Dialog.FileOpenDialog(
    title='Select blocks file to use',
    filter='')
if strBlocksFilePath == '':
    return

    # Open the selected file

    file = open(strBlocksFilePath, 'r')

    # Do the following for each line in the blocks file

    for line in file:
        crt.Screen.Send('readmem -s 2 -n 16384 0x' + line + '\r')
crt.Screen.WaitForString(prompt)

crt.Screen.Send('diag readmem -c\r')
crt.Screen.WaitForString(prompt)

crt.Screen.Send('diag readmem -c\r')
crt.Screen.WaitForString(prompt)

crt.Screen.Send('diag readmem -c\r')
crt.Screen.WaitForString(prompt)

# Close the file

file.close()
crt.Screen.Send('exit\r')

main()
Reply With Quote
  #4  
Old 07-24-2018, 03:11 PM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 501
Hi startek,

I believe the trouble you're seeing is a result of incorrect indentation. I've included the code you provided with the indentation issues I saw corrected. Does this version change the behavior you see?

Code:
# $language = "Python"
# $interface = "1.0"

import os


def main():
    errorMessages = ''


    if crt.Session.Connected:
        crt.Screen.Synchronous = True

    # When we first connect, there will likely be data arriving from the
    # remote system.  This is one way of detecting when it's safe to
    # start sending data.

    while True:
        if not crt.Screen.WaitForCursor(1):
            break

    # Once the cursor has stopped moving for about a second, we'll
    # assume it's safe to start interacting with the remote system.

    # Get the shell prompt so that we can know what to look for when
    # determining if the command is completed. Won't work if the prompt
    # is dynamic (e.g. changes according to current working folder, etc)

    row = crt.Screen.CurrentRow
    prompt = crt.Screen.Get(row, 0, row, crt.Screen.CurrentColumn - 1)
    prompt = prompt.strip()

    crt.Screen.Send('cd system\r')

    # Wait for the command to complete, by looking for the prompt to
    # appear once again.
    crt.Screen.WaitForString(prompt)

    # Select the blocks file to use
    strBlocksFilePath = crt.Dialog.FileOpenDialog(
        title='Select blocks file to use',
        filter='')
    if strBlocksFilePath == '':
        return

        # Open the selected file

    file = open(strBlocksFilePath, 'r')

    # Do the following for each line in the blocks file

    for line in file:
        crt.Screen.Send('readmem -s 2 -n 16384 0x' + line + '\r')
        crt.Screen.WaitForString(prompt)

        crt.Screen.Send('diag readmem -c\r')
        crt.Screen.WaitForString(prompt)

        crt.Screen.Send('diag readmem -c\r')
        crt.Screen.WaitForString(prompt)

        crt.Screen.Send('diag readmem -c\r')
        crt.Screen.WaitForString(prompt)

    # Close the file

    file.close()
    crt.Screen.Send('exit\r')

main()
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730
Reply With Quote
  #5  
Old 07-24-2018, 04:57 PM
startek startek is offline
Registered User
 
Join Date: Jul 2018
Posts: 9
Hi Eric,

yes, that was result of incorrect indentation (although I formatted via the few online formatters). Script run as expected, but due to device periodically sends to the terminal various event messages, script can't read blocks without errors. I can't stop this messages from printing. When device print event messages while memory is being read, an error occurs, system starts print memory address at the beginning in decimal format, output broken, after some time normal output returned, then all starts anew, good output mixed with broken output. So I was able to get first block without errors, but starting from 2nd block an errors periodically appeared, finally code stopped at 1/3 blocks. Possibly I should read 2-3 block at one cycle, but manual reading a full memory is just tedious.
Reply With Quote
  #6  
Old 07-25-2018, 03:26 AM
startek startek is offline
Registered User
 
Join Date: Jul 2018
Posts: 9
Hi Eric,

My suspicion is that the issue can be caused due to cache memory has been exceeded. The first block is always printed normally, the problems occur when processing subsequent blocks. Each block size is 65536 bytes.
According to Python official documentation, we can force the Garbage Collector to release unreferenced memory with gc.collect().

import gc
gc.collect()

although Python do this automatically anyway, but I want to try.
Or, maybe better to create a child process:

with concurrent.futures.ProcessPoolExecutor(max_workers=1) as executor:
result = executor.submit(func, *args, **kwargs).result()

Also, is it possible to set the delay after each completed cycle?
Can you specify how to make these options? Maybe one if them will work.
Reply With Quote
  #7  
Old 07-25-2018, 09:29 AM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 501
startek,

I'm sorry you're still having trouble. It is unlikely that the Garbage Collector is the issue. The Python code is running on your local machine, in SecureCRT's memory process only. This would generally not have any effect on the remote system's behavior.

I am not familiar with use of the Garbage Collector. This is outside the scope of support for SecureCRT's scripting API and may be bettered addressed by Python specific documentation rather than SecureCRT's. With that being said, myself and other members of the forums may be able to provide better insight with some additional information.

You indicated that the device is sending "event messages", is there a pattern to these messages?

Can you provide sample output of what the data should look like and what it looks like when the problem is occurring?

A delay can be added to the script using the Sleep method. Sleep specifies the time (in milliseconds) to pause the script's execution.
Code:
crt.Sleep(1000)
__________________
Thanks,
--Eric

VanDyke Software
Technical Support
support@vandyke.com
(505) 332-5730

Last edited by jdev; 07-25-2018 at 09:46 AM.
Reply With Quote
  #8  
Old 07-25-2018, 11:24 AM
startek startek is offline
Registered User
 
Join Date: Jul 2018
Posts: 9
Hi,

More precisely, the issue is not related to local computer where SecureCRT run, it caused by the remote device itself, running embedded OS.
As I suspect, problem occur due to exceeding cashe memory on the embedded device after some reading cycles or after certain time.
Device can read max 16384 bytes at a time, so I splitted block into 4 parts.
Below is sample output where problem occur:

Code:
80038330: 3402 ffff  3c04 8094  0c1a 494d  2484 4024 | 4...<.....IM$.@$
80038340: 3402 ffff  1602 0007  8fbf 0008  1620 0006 | 4............ ..
80038350: 8fb1 0004  3c04 8094  0c1a 495e  2484 4024 | ....<.....I^$.@$
80038360: 8fbf 0008  8fb1 0004  8fb0 0000  03e0 0008 | ................
80038370: 27bd 0010  27bd fff0  afbf 0000  2404 0001 | '...'.......$...
80038380: 0c00 e0c2  3405 ffff  8fbf 0000  03e0 0008 | ....4...........
80038390: 27bd 0010  27bd fff0  afbf 0000  0000 2021 | '...'......... !
800383a0: 0c00 e0c2  3405 ffff  8fbf 0000  03e0 0008 | ....4...........
800383b0: 27bd 0010  27bd fff0  afbf 0000  3c04 806e | '...'.......<..n
800383c0: 3c02 8094  2442 4028  2483 e11c  8065 0000 | <...$B@($....e..
800383d0: 8066 0001  a045 0000  a046 00:56:50.250 07/25/2018 (PROV) FATAL ERROR: DHCP procedure has timed out.

DHCPc:  Releasing the lease with client id htype=ff, value=bb a0 5f 2f 00 03 00 01 90 6e bb a0 5f 2f
[00:56:50 07/25/2018] [DHCP Client Thread] BcmEcosIpHalIf::RemoveLeaseImpl:  (IP Stack2 HalIf) Removing lease IP address 0.0.0.0 from IP stack 2
[00:56:50 07/25/2018] [DHCP Client Thread] BcmEcosIpHalIf::RemoveLeaseImpl:  (IP Stack2 HalIf) This is the last address on the stack; shutting the stack down:numberOfAddresses=0
[00:56:50 07/25/2018] [DHCP Client Thread] BcmEcosIpHalIf::ShutdownIpStackImpl:  (IP Stack2 HalIf) 
Shutting down IP stack 2
emta factory agent IpStackEvent
1  15365 32878 | .f...E...F..<..n
2147714016: 15362 32916  9282 16488  9385 57632  33066    0 | <...$B@h$.. .*..
2147714032: 33067    1  41034    0  41035    1  15362 32916 | .+...J...K..<...
2147714048: 9282 16552  33071    0  33059    1  41039    0 | $B@../...#...O..
2147714064: 41027    1  15363 32878  15362 32916  9282 16616 | .C..<..n<...$B@.
2147714080: 9315 57636  34920    0  39016    3  32873    4 | $c.$.h...h...i..
2147714096: 43080    0  47176    3  41033    4  15363 32878 | .H...H...I..<..n
2147714112: 15362 32916  9282 16680  9324 57644  35213    0 | <...$BA($l.,....
2147714128: 39309    3  33166    4  43085    0  47181    3 | .........M...M..
2147714144: 41038    4  15362 32916  9282 16744  35207    0 | .N..<...$BAh....
2147714160: 39303    3  33160    4  43079    0  47175    3 | .........G...G..
2147714176: 41032    4  15362 32916  9282 16808  9387 57632 | .H..<...$BA.$.. 
2147714192: 33132    0  33133    1  41036    0  41037    1 | .l...m...L...M..
2147714208: 15362 32916  9282 16816  9347 57628  32870    0 | <...$BA.$....f..
a normal hex output printed up to offset number 800383c0(h), starting from offset 800383d0(h) the output is broken due to event messages printed at the same time (FATAL ERROR, etc). The output appear entirely broken, with decimal offset instead of hex. Its 2nd block from 61 total. Then after some time, output is restored again:

Code:
2147729360:   64 8225  3081 23160  9221    1    64 8225 | .@ !..Zx$....@ !
2147729376: 10149   64  10150   65  10151   66  10152   67 | '..@'..A'..B'..C
2147729392: 10153   68  3082 19918  10154   69  37798   64 | '..D..M.'..E...@

Console/system> diag readmem -c

8003c000: 93a7 0041  93a8 0042  93a9 0043  93aa 0044 | ...A...B...C...D
8003c010: 93ab 0045  03a0 2021  3c05 806e  0c18 acaa | ...E.. !<..n....
8003c020: 24a5 e4ec  0220 2021  03a0 2821  0c01 284c | $....  !..(!..(L
8003c030: 0000 0000  0c01 28e2  0220 2021  8fbf 0058 | ......(..  !...X
8003c040: 8fb1 0054  8fb0 0050  03e0 0008  27bd 0060 | ...T...P....'..`
8003c050: 03e0 0008  0000 1021  27bd fff0  afbf 0000 | .......!'.......
8003c060: 0c0a b57c  00a0 2021  8fbf 0000  03e0 0008 | ...|.. !........
8003c070: 27bd 0010  27bd fff0  afbf 0000  3c02 808c | '...'.......<...
8003c080: 8c42 9c50  1440 000d  3c02 808c  3c02 808c | .B.P.@..<...<...
8003c090: 8c44 3198  5080 0009  3c02 808c  3c05 806e | .D1.P...<...<..n
8003c0a0: 0c1b 634c  24a5 e508  0040 2021  3c05 806e | ..cL$....@ !<..n
8003c0b0: 0c1b 5ec1  24a5 8b00  3c02 808c  8c44 9c50 | ..^.$...<....D.P
After some time, broken again:
Code:
80096f20: 0200 1021  ae50 0000  ac50 0000  8e42 0000 | ...!.P...P...B..
80096f30: ac42 0004  2624 0004  3c05 806f  0c0a 85f6 | .B..&$..<..o....
80096f40: 24a5 139c  8fbf 001c  8fb2 0018  8fb1 0014 DHCPc:  Waiting 4 seconds before sending Discover; client id htype=ff, value=bb a0 5f 2f 00 03 00 01 90 6e bb a0 5f 2f
| $...............
2148101968: 36784   16   992    8  10173   32  10173 65472 | ........'.. '...
2148101984: 44991   56  44981   52  44980   48  44979   44 | ...8...4...0...,
2148102000: 44978   40  44977   36  44976   32   128 40993 | ...(...$... ...!
2148102016: 15362 32915  9282 41928  44162    0  15362 32998 | <...$B......<...
2148102032: 4096   22  9301 35568  35920    0  44976    8 | ....$U...P......
and so on
Reply With Quote
  #9  
Old 07-25-2018, 03:21 PM
ekoranyi ekoranyi is offline
VanDyke Technical Support
 
Join Date: Jan 2017
Posts: 501
startek,

After reviewing the provided information I don't know that the SecureCRT Scripting API has specific tools to help you overcome your device limitations. If your device does not support disabling error/informational messages from being displayed to the console it will be difficult the get "clean" data. The key will most likely be identifying a pattern that determines if the data is "good" or "bad" data. Thus far I have not been able to identify a pattern that would help definitively say if the data was corrupted or not.

If you can identify a pattern the process may look something like.

1) Run your command
2) Capture all data until the new prompt
3) Test this data against your identified pattern
4) If the data is corrupted re-run the last command
Most of this work would fall outside of the scope of support for SecureCRT's Scripting API and would be better addressed by working with Python and data patterns.

If you can identify a pattern to determine the validity of the data on screen and have specific questions about implementing it in SecureCRT's API we would be happy to assist.
__________________
Thanks,
--Eric

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 12:12 PM.