Session Object

SecureCRT


 

Description

The Session object provides access to the state and properties that exist for the current connection A data path or circuit between two computers over a phone line, network cable, or other means. or session A session is a set of options that are assigned to a connection to a remote machine. These settings and options are saved under a session name and allow the user to have different preferences for different hosts. .

 

Syntax

session.Property [ = expression ]

session.Method([arglist])

 

Remarks

SecureCRT's Session object is accessed through the top-level object’s Session property.

 

Session Object Properties and Methods

Properties

Methods

    Config

    Connect

    Connected

    ConnectInTab

    LocalAddress

    Disconnect

    LogFileName

    Log

    Logging

    LogUsingSessionOptions

    Path

    Print

    RemoteAddress

    SetStatusText

    RemotePort

 

 

Properties

 

Config

Description

Returns the configuration associated with the session.

Remarks

This configuration will not be saveable to a new name.

VBScript

Syntax

Set objectvarname = object.Config

Example

Set config = crt.Session.Config

Python

Syntax

objectvarname = object.Config

Example

config = crt.Session.Config

 

Connected

Description

Returns a Boolean value indicating whether the current session is connected or not.

Remarks

Boolean read-only property.

VBScript

Syntax

[ varname = ] object.Connected

Python

Syntax

[ varname = ] object.Connected

 

LocalAddress

Description

Returns the IP address of the local machine in the form of a string.

Remarks

LocalAddress is a read-only string property. The LocalAddress property should only be accessed if the session is connected. Attempting to access LocalAddress while not connected is an error.

VBScript

Syntax

[ varname = ] object.LocalAddress

Python

Syntax

[ varname = ] object.LocalAddress

 

LogFileName

Description

Returns or sets the name of the current log file.

Remarks

If filename is invalid a runtime error is generated. See also: session.Log

If the log filename contains parameter substitutions that are known when the function is called, they are filled in. Otherwise, the literal string is returned.

VBScript

Syntax

[ varname = ] object.LogFileName [ = filename ]

Python

Syntax

[ varname = ] object.LogFileName [ = filename ]

 

Logging

Description

Returns a Boolean value indicating whether the current session is logging or not.

Remarks

Boolean read-only property.

VBScript

Syntax

[ varname = ] object.Logging

Python

Syntax

[ varname = ] object.Logging

 

Path

Description

Returns the path for the current session starting from location of the session INI files.

Remarks

Path is a read-only string property that returns the path for the current session starting from location of the session INI files. If no session has been selected, this function will return the name "Default".

VBScript

Syntax

[ varname = ] object.Path

Example

If crt.session.path = "work\server0" Then

    myprompt = "linux"

End If

Python

Syntax

[ varname = ] object.Path

Example

if crt.Session.Path == "work\server0":

  myprompt = "linux"

 

RemoteAddress

Description

Returns the IP address of the remote host in the form of a string.

Remarks

RemoteAddress is a read-only string property. The RemoteAddress property should only be accessed if the session is connected. Attempting to access RemoteAddress while not connected is an error.

VBScript

Syntax

[ varname = ] object.RemoteAddress

Python

Syntax

[ varname = ] object.RemoteAddress

 

RemotePort

Description

Returns the port number of the remote port.

VBScript

Syntax

[ varname = ] object.RemotePort

Python

Syntax

[ varname = ] object.RemotePort

 

Methods

Connect

Description

Connects to a session.

Remarks

The Connect method takes a string parameter that specifies how a connection is to be made. The format of the string parameter matches the format of the command line arguments to SecureCRT. The Connect method can accept an empty argument set.

The command crt.Session.Connect() means "connect to the current session". If a connection has not been made before, the command will fail. If a connection already exists, nothing happens, otherwise, this command is equivalent to pressing the Reconnect button.

 

Examples:

Connect using a predefined session:

crt.session.Connect("/s mysession")

 

Connect to "myhost" on port 2345 using the Telnet Telnet is a protocol that provides an interface for communications between clients and servers. protocol and Default session parameters:

crt.session.Connect("/telnet myhost 2345")

 

The Connect method takes a parameter (True or False) that determines whether or not the script should wait for the connection to fully authenticate before continuing. For instance, the connect call in the examples below will wait until the connection has been fully authenticated before returning and allowing the script to execute the error check just below the connect call. This parameter only applies to SSH1 and SSH2 connections. Whereas, passing False would allow the error check to execute before the connection has a chance to finish. You will probably want to use False only in cases where you want to script the logon process for the session you are attempting to connect to. The default value for this parameter is True.

 

The Connect method also takes an optional parameter (True or False) that specifies whether or not to suppress pop-up messages. This parameter defaults to False (don't suppress).

 

VBScript

Syntax

object.Connect [arg [, True|False] [, True|False]]

Examples

# $language = "VBScript"

# $interface = "1.0"

On Error Resume Next

crt.Session.Connect "/SSH2 /PASSWORD password username@hostname", True

If Err.Number <> 0 Then

    MsgBox "Connection Failed"

Else

    MsgBox "Connection Successful"

End If

 

The following is an example script that uses the /ENCRYPTEDPASSWORD command-line option.

# $language = "VBScript"

# $interface = "1.0"

set config = crt.OpenSessionConfiguration("<session name>")

CRT.dialog.messagebox(config.GetOption("Password"))

crt.Session.Connect "/SSH2 /ENCRYPTEDPASSWORD " & config.GetOption("Password") & " <username>@<hostname>", True

Python

Syntax

object.Connect(arg [, True|False] [, True|False])

Examples

# $language = "Python"

# $interface = "1.0"

 

errcode = 0

try:

  crt.Session.Connect("/SSH2 /PASSWORD password username@hostname", True)

except ScriptError:

  errcode = crt.GetLastError()

if errcode != 0:

  crt.Dialog.MessageBox("Connection Failed")

else:

  crt.Dialog.MessageBox("Connection Successful")

 

The following is an example script that uses the /ENCRYPTEDPASSWORD command-line option.

# $language = "Python"

# $interface = "1.0"

 

import SecureCRT

 

set config = crt.OpenSessionConfiguration("<session name>")

crt.Dialog.Messagebox(config.GetOption("Password"))

crt.Session.Connect("/SSH2 /ENCRYPTEDPASSWORD " + config.GetOption("Password") + "<username>@<hostname>", True)

 

ConnectInTab

Description

Connects to a session in a tab or tiled session window.

Remarks

Takes the same arguments as Connect. This also returns a Tab object.

The ConnectInTab method also takes two optional parameters (True or False). The first determines whether or not the script should wait for the connection to fully authenticate before continuing. The first parameter only applies to SSH1 and SSH2 connections. The second optional parameter specifies whether the call fails silently or raises an exception. If the second parameter is False and the connection attempt fails, an exception is raised and the method returns an empty object. If the second parameter is True and the connection attempt fails, the method returns a valid Tab object, which can then be used to check for  connection status in order to determine if the connection attempt was successful. See the Connect method above for additional examples.

VBScript

Syntax

Set objTab = object.ConnectInTab [arg [, True|False] [, True|False]]

Examples

Connect in a tab using a predefined session:

Set tab = crt.session.ConnectInTab("/s mysession")

 

Connect in a tab to "myhost" on port 2345 using the Telnet Telnet is a protocol that provides an interface for communications between clients and servers. protocol and Default session parameters:

Set tab = crt.session.ConnectInTab("/telnet myhost 2345")

 

Close tab if attempt to connect fails:

Set objNewTab = crt.Session.ConnectInTab("Host_Does_Not_Exist",,True)

MsgBox _

    "Script Tab's index: " & crt.GetScriptTab().Index & vbcrlf & _

    "New Tab's index: " & objNewTab.Index

    If Not objNewTab.Session.Connected Then

         ' Make sure we're not re-using disconnected

         ' tabs, we don't want to close the script

         ' tab, just any new tabs that get created by

         ' calls to ConnectInTab(), but which don't

         ' result in a successful connection.

         If crt.GetScriptTab().Index <> objNewTab.Index Then

             crt.Dialog.MessageBox("Closing failed tab")

             objNewTab.Close()

         End If

     End If

Python

Syntax

objTab = object.ConnectInTab [arg [, True|False] [, True|False]]

Example

Connect in a tab using a predefined session:

crt.Session.ConnectInTab("/s mysession")

 

Connect in a tab to "myhost" on port 2345 using the Telnet protocol and Default session parameters:

crt.Session.ConnectInTab("/telnet myhost 2345")

 

Close tab if attempt to connect fails:

objNewTab = crt.Session.ConnectInTab("Host_Does_Not_Exist", failSilently=True)

crt.Dialog.MessageBox(

    "Script Tab's index: " + str(crt.GetScriptTab().Index) + "\n" +

    "New Tab's index: " + str(objNewTab.Index))

if objNewTab.Session.Connected != True:

     # Make sure we're not re-using disconnected

     # tabs, we don't want to close the script

     # tab, just any new tabs that get created by

     # calls to ConnectInTab(), but which don't

     # result in a successful connection.

     if crt.GetScriptTab().Index != objNewTab.Index:

         crt.Dialog.MessageBox("Closing failed tab")

         objNewTab.Close()

 

Disconnect

Description

Disconnects the current session.

Remarks

If the current session is not connected, Disconnect does nothing.

VBScript

Syntax

object.Disconnect

Python

Syntax

object.Disconnect()

 

Log

Description

Enables or disables logging.

Remarks

Starts or stops logging depending on the Boolean state of the "start" parameter. When logging is being started the optional Boolean "append" and "raw" parameters may be set to True to open the log file for appending or to log raw characters respectively. The append and raw parameters are optional and are false if not specified (When "start" is false the values of append and raw are ignored).

VBScript

Syntax

object.Log(start[, append[, raw]])

Python

Syntax

object.Log(start[, append[, raw]])

 

LogUsingSessionOptions

Description

Turns on logging using the logging options for the current session.

Remarks

If the session is an ad hoc session, the Default session's logging options will be used.

VBScript

Syntax

object.LogUsingSessionOptions

Python

Syntax

object.LogUsingSessionOptions()

 

Print

Description

Starts or stops autoprint.

Remarks

Starts or stops autoprint depending on the Boolean start parameter.

VBScript

Syntax

object.Print(start)

Python

Syntax

object.Print(start)

 

SetStatusText

Description

Allows you to set the text within the status bar for a specific session.

Remarks

Sets the status bar message to the specified text string.

VBScript

Syntax

object.SetStatusText(text)

Python

Syntax

object.SetStatusText(text)