Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilbunilcho/e05c45a4b7fc3258bf105a83ce8254cc to your computer and use it in GitHub Desktop.
Save ilbunilcho/e05c45a4b7fc3258bf105a83ce8254cc to your computer and use it in GitHub Desktop.
SecureCRT script for connection with gateway
  • using session name as hostname
  • you should set gateway session and target session in connection manager
  • can connect to target host directly
  • but needed kinit on gateway server for sudo permission
# $language = "Python"
# $interface = "1.0"

GATEWAY_SESSION_NAME = "gateway.host.com"

KINIT_PASSWORD = "kinitpassword"

def Main():
    # cancel connection to target host
    if crt.Session.Connected:
        crt.Session.Disconnect()

    tab, error = 0, 0
    target = ""

    # if session saved in sub directory
    target = crt.Session.Path.split("\\")[1]
    # or
    # target = crt.Session.Path

    try:
        # save tab object
        tab = crt.Session.ConnectInTab("/S \"" + GATEWAY_SESSION_NAME + "\"")
    except ScriptError:
        error = crt.getLastErrorMessage()

    if crt.Session.Connected and not error:
        crt.Screen.Synchronous = True

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

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

        crt.Screen.Send("kinit\n")

        crt.Screen.WaitForString("host.com:")
        crt.Screen.Send(KINIT_PASSWORD + "\n")

        crt.Screen.WaitForString(prompt)
        crt.Screen.Send("rlogin -l irteamsu " + target + "\n")

        # change caption to target hostname
        tab.Caption = target

        crt.Screen.Synchronous = False
    else:
        crt.Dialog.MessageBox(error);

Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment