View Single Post
  #2  
Old 06-06-2019, 04:21 PM
gregg gregg is offline
Registered User
 
Join Date: Oct 2010
Posts: 53
Lightbulb

The below code will iterate every session and (might*) update the password where username matches.

You must update the variable SESSION_ROOT with the location of your Sessions directory. Note: I couldn't find a crt.API call that would tell me the config path, so this hard-coded value is present.

The following line is commented out which does the actual updating, so code is currently read-only. (AKA, i recommend backing up your sessions before running where it updates)

Code:
# change_password(cfg, pw)

(might*) - I say this MIGHT update because secure CRT stores passwords encrypted. Using SetOption("Password", password) does not actually store encrypted. The docs say this:

Quote:
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.
...
config.SetOption "Session Password Saved", 1
...and did not seem to do anything towards encrypting the password.

It's python, so indentation is very important to maintain.

Code:
# $language = "Python"
# $interface = "1.0"

import os

# Path to on-disk sessions in order to enumerate them
SESSION_ROOT = "c:\\wherever\\your\\VanDyke\\scrt\\config\\Sessions"

def ask_username():
    return crt.Dialog.Prompt("Username", "")

def ask_new_password():
    return crt.Dialog.Prompt("New password", "")

# update the secure crt session with new password
def change_password(sess_cfg, password):
    sess_cfg.SetOption("Session Password Saved", 1)
    sess_cfg.SetOption("Password", password)
    sess_cfg.Save()

# convert file system full\path\fname.ini to sCRT session\name
def build_session_name(path, fname):
    # get the relative path to the session name
    sess_path = path[len(SESSION_ROOT):].lstrip("\\")

    # strip ".ini"
    sess_name = fname[:-4]

    return sess_path + "\\" + sess_name

def main():
    un = ask_username()
    if not un:
        return

    pw = ask_new_password()
    if not pw:
        return

    total = 0
    changed_list = []
    for root, dirs, files in os.walk(SESSION_ROOT):
        for fname in files:
            if fname == "__FolderData__.ini":
                continue

            if not fname.endswith(".ini"):
                continue

            total += 1
            sess_name = build_session_name(root, fname)

            try:
                cfg = crt.OpenSessionConfiguration(sess_name)
            except Exception:
                # could not open session
                continue

            try:
                if cfg.GetOption("Username") == un:
                    # change_password(cfg, pw)
                    changed_list.append(sess_name)
            except Exception:
                # serial ports, etc don't have Username property
                continue

    # some stats
    message =  "Total sessions: %d\n" % total
    message += "Changed sessions: %d\n" % len(changed_list)
    message += "\n".join(changed_list)
    crt.Dialog.MessageBox(message, "Updated Sessions Stats")

main()
Reply With Quote