Creating Python Scripts

SecureCRT


SecureCRT contains an embedded Python interpreter, which means that Python scripts can be run from SecureCRT without having to install additional software.

Note: To run a logon script in SecureCRT, you must first turn off the Automate logon option in the Connection/Logon Actions category of the Session Options dialog and then, on the same dialog, check the Logon script check box and choose the script that you want to run.

Note2: A script cannot be run from another script. This includes scripts started manually and logon scripts.

Scripts for SecureCRT are text files that you create with your text editor.

A script header is not required for Python scripts as long as the file extension “.py” is used. If you want to use a header (for example, if you are using a file without an extension or with an extension that is not common), then the header must begin on the first line of the script.

Script headers will be used by SecureCRT to identify which script language the script is written in and the version of SecureCRT scripting interface. Each line of the script header must begin with a pound/hash (#) character. A SecureCRT script header includes a $language line that identifies the script engine and an $interface line to identify SecureCRT's interface version.

The syntax of the script header is always the same regardless of the script language you are using.

A simple but complete SecureCRT script with a header that identifies it as Python is shown below:

# $language = "Python"

# $interface = "1.0"

 

def main():

  # Display SecureCRT's version

  crt.Dialog.MessageBox("SecureCRT version is: " + crt.Version)

main()

Note: A SecureCRT script header may also contain blank lines that begin with #.

The quoted string following $language identifies the script engine. For Python scripts, the appropriate identifier is Python. Currently the script header should specify version 1.0 for $interface. Future versions of SecureCRT may support other versions

It is not a requirement that you place your code within a main routine however there may be reasons why you would want to do this. The Python interpreter will parse and execute global script code (script code you have defined outside of any subroutine) before your main is executed. If you have "initialization" code that you want to ensure has been completely executed before your actual script code begins, it may be useful to place your initialization code at the global level. This will ensure that your initialization code will all execute before your main code runs.

Another reason you may want a main routine is to allow your scripts a way of aborting themselves in case of problems. In Python for SecureCRT, there is no built-in way of exiting a script at the global level. However, if you want to exit a subroutine it is possible to use the return syntax to do so. For example:

def main():

  condition = DoSomething()

  if (condition == 0):

    # Error, bailout

    return

main()

For examples of SecureCRT scripts, see the sample scripts on the VanDyke Software website in the Scripting FAQ section.

Many excellent Python resources can be found on the Internet.  Here are a few you may find useful:

http://docs.python.org/reference/index.html

http://diveintopython.org

http://wiki.python.org/main/BeginnersGuide