View Single Post
  #5  
Old 10-31-2013, 02:22 AM
ogun ogun is offline
Registered User
 
Join Date: Sep 2013
Posts: 15
I wrote the following script for our needs.
It reads a csv file with three columns:
hostname, folder, description

It then creates sessions for each in a session folder named shared, each session a copy of the default session.

We use it as a way of having a shared session database, using urlget to grab the CSV file.

Since I can't find the attach file option, I'll just code tag it instead
Code:
#$language = "Python"
#$interface = "1.0"

# Top level folder to put all sessions in.
TOPLEVEL = "Shared"
INPUTFILE = "devices.csv"

import SecureCRT
import csv
import os.path
import shutil


def main():
    # Open file for reading
    # We expect the CSV file to have three columns per line:
    # Hostname, Folder, Description
    csvfile = open(INPUTFILE)
    csvreader = csv.reader(csvfile)

    # Remove existing folder, we rebuild every time.
    script_name = crt.ScriptFullName
    script_dir = os.path.dirname(script_name)

    # We expect to be in $CONFIG/Scripts with the sessions in $CONFIG/Sessions
    sessions_folder = os.path.join(script_dir, '../Sessions')

    # Did we get it right?
    if not os.path.isdir(sessions_folder):
        msg = "Could not find sessions folder. Got %s" % sessions_folder
        crt.Dialog.MessageBox(msg)
        return

    # Append top level
    shared_folder = os.path.join(sessions_folder, TOPLEVEL)

    # Delete old folder
    if os.path.exists(shared_folder):
        shutil.rmtree(shared_folder)

    # Open the default session to use as a template for created sessions
    session = crt.OpenSessionConfiguration('Default')

    for line in csvreader:
        sessionname = line[0].split('.')[0]
        hostname = line[0]
        folder = TOPLEVEL + "\\" + line[1]
        comment = line[2]

        session.SetOption('Protocol Name', 'SSH2')
        session.SetOption('[SSH2] Port', 22)
        session.SetOption('Hostname', hostname)
        session.SetOption('Description', [comment])

        session.Save(folder + "\\" + sessionname)

# Do the running
main()
Reply With Quote