VanDyke Software

Scripting Examples

Index

Importing Custom Modules Referencing the "crt" Object (Python)

These examples provide techniques for importing your custom-written Python modules that need to reference the crt scripting object.

To be clear, this approach does not allow scripts running outside of SecureCRT to access the crt scripting object. The crt script object is not available to any process outside of the SecureCRT process itself. SecureCRT populates the script engine with the crt object at runtime; so unless SecureCRT is running the script itself, there is no crt object available to any script engine.

This approach simply allows you to modularize elements of your existing SecureCRT scripts — scripts that must continue to be run from within the SecureCRT process itself.

The main script, ImportModuleWith_crt_Reference.py.txt, which imports the module code, crt_Module.py.txt, performs this sequence of actions:

  1. sys.dont_write_bytecode = True
    This statement is used to PREVENT Python from automatically creating .PYC files. Otherwise, coding, testing, and revising your module's functionality would prove cumbersome and confusing since Python imports the .PYC file (which doesn't reflect your current code) if it exists.

  2. del(sys.modules['crt_Module'])
    The Python environment is initialized only once in SecureCRT at startup, and remains as-is until the SecureCRT process terminates. This means that once you run a script which imports a module, that module is in the sys.modules table for the lifetime of the SecureCRT process. If you are in the process of developing and testing a module, code changes you make will not be reflected unless you either:
    1. restart SecureCRT
      or
    2. use the del(sys.modules['crt_Module']) statement to remove the module from the sys.modules table so that it can be imported a-fresh.
  3. import crt_module
    This is the line of code that tells the Python interpreter to import your module code, if it's not already in the sys.modules table.

  4. "Injects" the crt object into the module (so that statements within the module that reference the crt object won't result in an error). In this pair of example scripts,
    1. the crt_Module.py module code defines a method as Inject_crt_Object(obj_crt_API), which allows the crt application programming interface known to the main script to be used within the module code itself.
    2. the main script's .py code, following the import(crt_module) and reload(crt_module) statements, calls the module's Inject_crt_Object method, passing in its copy of the crt object, as in: Inject_crt_Object(crt).

Example Scripts

ImportModuleWith_crt_Reference.py.txt (4.0 KB)
This is the main Python script code. It imports the crt_Module code. The script will work in either Python 2.7 or 3.x.

crt_Module.py.txt (953 Bytes)
This file contains the module code itself. This module is imported by the main script code.

 

VanDyke Software uses cookies to give you the best online experience. Before continuing to use this site, please confirm that you agree to our use of cookies. Please see our Cookie Usage for details.