SessionConfiguration Object

SecureCRT


Description

The SessionConfiguration object provides methods for accessing session configuration through scripting.

Syntax

SessionConfiguration.Method([arglist])

Remarks

Can be used in the following cases:

  Change settings in an existing session before connecting.

  Convert an Excel spreadsheet to save SecureCRT or SecureFX (if installed) sessions.

SessionConfiguration Object Methods

Methods

 

    ConnectInTab

 

    GetOption

 

    Save

 

    SetOption

 

Methods

ConnectInTab

Description

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

Remarks

This method returns a Tab object.

VBScript

Syntax

Set objTab = object.ConnectInTab

Example

Set objConfig = crt.OpenSessionConfiguration("Test")

objConfig.SetOption "Hostname", "host"

Set objTab = objConfig.ConnectInTab

Python

Syntax

objTab = object.ConnectInTab()

Example

objConfig = crt.OpenSessionConfiguration("Test")

objConfig.SetOption("Hostname", "host")

objTab = objConfig.ConnectInTab()

 

GetOption

Description

Gets the value of the specified option.

Remarks

OptionName is a string parameter that is the name of the option. This method returns the value of the specified option. If the option is a string type (S), a string is returned. If the option is a multi-string (Z), an array of strings is returned. If the option is a DWORD (D), an integer is returned.

VBScript

Syntax

varname = object.GetOption(OptionName)

Example

Set objTab = crt.GetScriptTab
Set objConfig = objTab.Session.Config
szUsername = objConfig.GetOption("Username")
szSessionName = objTab.Session.Path
MsgBox "Username for current session (" & _
szSessionName & ") = " & szUsername

vPortForwards = objConfig.GetOption("Port Forward Table V2")
nElements = UBound(vPortForwards)
If nElements = -1 Then
MsgBox "No port forward configuration defined"
Else
MsgBox nElements + 1 & _
" port forward entries exist in this session (" & _
objTab.Session.Path & ")"
End If

 

In the following example, the current session’s Description is displayed.

 

Set objTab = crt.GetScriptTab
Set objConfig = objTab.Session.Config
strDescription = Join(objConfig.GetOption("Description"), vbcrlf)
crt.Dialog.MessageBox "Current Session's Description: " & vbcrlf & _
strDescription

Python

Syntax

varname = object.GetOption(OptionName)

Example

objTab = crt.GetScriptTab()

objConfig = objTab.Session.Config

szUsername = objConfig.GetOption("Username")

szSessionName = objTab.Session.Path

crt.Dialog.MessageBox("Username for current session (" +

szSessionName + ") = " + szUsername)

 

vPortForwards = objConfig.GetOption("Port Forward Table V2")

nElements = len(vPortForwards)

if nElements == 0:

  crt.Dialog.MessageBox("No port forward configuration defined")

else:

  crt.Dialog.MessageBox(str(nElements) +

  " port forward entries exist in this session (" + objTab.Session.Path + ")")

 

In the following example, the current session’s Description is displayed.

 

  objTab = crt.GetScriptTab()

  objConfig = objTab.Session.Config

  formattedDescription = []

  arrDescription = objConfig.GetOption("Description")

  nElements = len(arrDescription)

  for index in xrange(nElements):

    formattedDescription.append(arrDescription[index])

    formattedDescription.append("\n")

  strDescription = ''.join(formattedDescription)

  crt.Dialog.MessageBox("Current Session's Description: \n" + strDescription)

 

Save

Description

Saves the configuration.

Remarks

If SessionPath is not provided, the configuration will be saved to the .ini file that is associated with the Config object used. If the SessionPath provided is the same as an existing session configuration, the existing .ini file will be overwritten with the settings contained in the Config object used. If the SessionPath provided does not exist, a new session of that name will be created in the session path specified. Note that the session path is not a full file system path, rather it is the path relative to the folders as they appear within the SecureCRT Connect dialog, with the built-in "Sessions" folder being the "root" path. In the graphic below, the path to the selected session would be Redhat8 - SSH2\redhat.

 

 

VBScript

Syntax

object.Save [SessionPath]

Example

' Save configuration to a new session.
objConfig.Save "Redhat8 - SSH2\redhat - PF"

Python

Syntax

object.save([SessionPath])

Example

# Save configuration to a new session.

objConfig.Save("Redhat8 - SSH2\redhat - PF")

 

SetOption

Description

Sets the specified option to the specified value.

Remarks

OptionName is a string parameter that is the name of the option. Value is the value to set the option to. If the option is a string type (S), a string should be specified for the value.  If the option is a multi-string (Z), an array of strings should be specified for the value. If the option is a DWORD (D), the value should be numeric. If there is an error this method will display a error message box. If the errors are not being displayed, the error message can be retrieved by using crt.GetLastErrorMessage.

VBScript

Syntax

object.SetOption OptionName, Value

 

Example

Config.SetOption "Terminal Protocol Name", "SSH2"

 

In the following example, the current session’s Description is changed.

 

Set objTab = crt.GetScriptTab

Set objConfig = objTab.Session.Config

strNewDescription = _

"This host is located in room 304." & vbcrlf & _

"Requires SSH2 publickey authentication."

 

' Update the Config object's description;

' convert string to array using Split()

objConfig.SetOption "Description", Split(strNewDescription, vbcrlf)

 

' Display updated configuration

strDescription = Join(objConfig.GetOption("Description"), vbcrlf)

crt.Dialog.MessageBox "Session's Updated Description (New): " & _

vbcrlf & _

strDescription

' Save the configuration

objConfig.Save

 

The following shows how to use a password in a script. The example uses Server1's password to connect to Server.

Note: When set, the Password parameter must be encoded. The Session Password Saved parameter must be set to true for the Password parameter to be used.

 

Sub Main ()

  set config = crt.OpenSessionConfiguration("Server")

  set config1 = crt.OpenSessionConfiguration("Server1")

  config.SetOption "Session Password Saved", 1

  config.SetOption "Password", config1.GetOption("Password")

  config.ConnectInTab

End Sub

Python

Syntax

object.SetOption(OptionName, Value)

Example

config.SetOption("Terminal Protocol Name", "SSH2")

 

In the following example, the current session’s Description is changed.

 

objTab = crt.GetScriptTab()

objConfig = objTab.Session.Config

arrNewDescription = []

arrNewDescription.append("This host is located in room 304.")

arrNewDescription.append("Requires SSH2 publickey authentication.")

 

# Update the Config object's description;

objConfig.SetOption("Description", arrNewDescription)

 

# Display updated configuration

formattedDescription = []

arrDescription = objConfig.GetOption("Description")

nElements = len(arrDescription)

for index in xrange(nElements):

  formattedDescription.append(arrDescription[index])

  formattedDescription.append("\n")

strDescription = ''.join(formattedDescription)

crt.Dialog.MessageBox("Session's Updated Description (New): \n" + strDescription)

# Save the configuration

objConfig.Save()

 

The following shows how to use a password in a script. The example uses Server1's password to connect to Server.

Note: When set, the Password parameter must be encoded. The Session Password Saved parameter must be set to true for the Password parameter to be used.

 

def main():

  config = crt.OpenSessionConfiguration("Server")

  config1 = crt.OpenSessionConfiguration("Server1")

  config.SetOption("Session Password Saved", 1)

  config.SetOption("Password", config1.GetOption("Password"))

  config.ConnectInTab()

main()