mirror of
https://github.com/CiscoDevNet/cml-community.git
synced 2025-10-23 07:42:03 +00:00
Windows SecureCRT Lab/Session Generation (#37)
* New script project for SecureCRT Session Generation Simple script for automated session generation with secureCRT on windows. minimal module requirements
This commit is contained in:
@@ -61,3 +61,4 @@ This project was written and is maintained by the following individuals:
|
||||
* Ozzy Schoonover <OzzySchoonover@gmail.com>
|
||||
* Alexander Deca <alexander@deca-consulting.be>
|
||||
* Xinyu Wei <trustywolf@sfc.wide.ad.jp>
|
||||
* Kelly Christians <kelly.christians@rsmus.com>
|
32
scripts/breakout-to-secureCRT-windows/config.yaml
Normal file
32
scripts/breakout-to-secureCRT-windows/config.yaml
Normal file
@@ -0,0 +1,32 @@
|
||||
#
|
||||
# breakout default configuration file
|
||||
# - 'username' and 'password' for authentication
|
||||
# - replace hostname 'cml-controller.cml.lab' with your IP or FQDN
|
||||
# - set an empty 'listen_address' to listen globally
|
||||
#
|
||||
# Known environment variables:
|
||||
#
|
||||
# BREAKOUT_USERNAME
|
||||
# BREAKOUT_PASSWORD
|
||||
# BREAKOUT_CONTROLLER
|
||||
# BREAKOUT_LISTEN_ADDRESS
|
||||
# BREAKOUT_UI_SERVER_PORT
|
||||
# BREAKOUT_CONSOLE_START_PORT
|
||||
# BREAKOUT_VNC_START_PORT
|
||||
# BREAKOUT_LAB_CONFIG_NAME
|
||||
# BREAKOUT_POPULATE_ALL
|
||||
# BREAKOUT_VERIFY_TLS
|
||||
# BREAKOUT_EXTRA_LF
|
||||
#
|
||||
|
||||
console_start_port: 9000
|
||||
controller: https://your.controller.fqdn
|
||||
extra_lf: false
|
||||
lab_config_name: labs.yaml
|
||||
listen_address: '[::1]'
|
||||
password: 'changeMe'
|
||||
populate_all: true
|
||||
ui_server_port: 8080
|
||||
username: your.username
|
||||
verify_tls: true
|
||||
vnc_start_port: 5900
|
87
scripts/breakout-to-secureCRT-windows/main.py
Normal file
87
scripts/breakout-to-secureCRT-windows/main.py
Normal file
@@ -0,0 +1,87 @@
|
||||
import yaml
|
||||
import os
|
||||
import winreg
|
||||
|
||||
def generate_session_ini(label, port, lab_title, filename, parent_folder):
|
||||
"""
|
||||
Generates a SecureCRT session.ini file with the provided information.
|
||||
|
||||
Args:
|
||||
label (str): Label of the node.
|
||||
port (int): Listen port of the device.
|
||||
lab_title (str): Title of the lab.
|
||||
filename (str): Name of the session.ini file to create.
|
||||
parent_folder (str): Path to the parent folder where labs will be created.
|
||||
"""
|
||||
# Ensure parent folder exists
|
||||
if not os.path.exists(parent_folder):
|
||||
os.makedirs(parent_folder)
|
||||
|
||||
# Create lab-specific folder within the parent folder
|
||||
lab_folder = parent_folder +"/" + lab_title
|
||||
if not os.path.exists(lab_folder):
|
||||
os.makedirs(lab_folder)
|
||||
|
||||
|
||||
hexport = hex(port).split('x')[-1]
|
||||
|
||||
filepath = lab_folder + "/" + filename
|
||||
with open(filepath, "w") as file:
|
||||
file.write(f"S:\"Hostname\"=::1\n")
|
||||
file.write(f"D:\"Port\"=0000{hexport}\n")
|
||||
file.write(f"S:\"Protocol Name\"=Telnet\n") # Use telnet protocol
|
||||
file.write(f"S:\"Color Scheme\"=Traditional\n")
|
||||
file.write(f"D:\"ANSI Color\"=00000001\n")
|
||||
file.write(f"S:\"Emulation\"=VT100\n")
|
||||
|
||||
key_path = r"SOFTWARE\VanDyke\SecureCRT" # Replace with your specific subkey if needed
|
||||
try:
|
||||
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path) as key:
|
||||
# Replace "value_name" with the specific value you want to read within the key
|
||||
value_name = "Config Path"
|
||||
secureCrtPath, _ = winreg.QueryValueEx(key, value_name)
|
||||
except WindowsError:
|
||||
print(f"Error accessing registry key: {key_path}")
|
||||
#define folder for labs and sessions to be placed
|
||||
parent_folder = secureCrtPath + "\sessions\_CML"
|
||||
#purge old labs/sessions
|
||||
os.system(f'rmdir /s /q "{parent_folder}"')
|
||||
|
||||
# Specify the YAML file path
|
||||
yaml_file_path = "labs.yaml" # Replace with your YAML file path
|
||||
|
||||
# Load the YAML data
|
||||
try:
|
||||
with open(yaml_file_path, "r") as file:
|
||||
data = yaml.safe_load(file)
|
||||
print(f"Successfully loaded YAML data from: {yaml_file_path}")
|
||||
except FileNotFoundError as e:
|
||||
print(f"Error: YAML file not found: {e}")
|
||||
exit()
|
||||
except yaml.YAMLError as e:
|
||||
print(f"Error loading YAML data: {e}")
|
||||
exit()
|
||||
|
||||
# Extract and handle potential missing key
|
||||
nodes_data = data.get('nodes', {})
|
||||
|
||||
# Generate session.ini files and folders
|
||||
node_info = []
|
||||
print("Generating SecureCRT session.ini files for each node:")
|
||||
for item_name, item_data in data.items():
|
||||
# Check for 'nodes' key within each top-level item
|
||||
if 'nodes' in item_data:
|
||||
for node_id, node_data in item_data['nodes'].items():
|
||||
label = node_data['label']
|
||||
for device in node_data['devices']:
|
||||
if device['enabled']:
|
||||
port = device['listen_port']
|
||||
lab_title = item_data['lab_title']
|
||||
filename = f"{label}_{port}.ini"
|
||||
valid_chars = " -_.0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
#sanitize folder and file names
|
||||
lab_title = "".join(c for c in lab_title if c in valid_chars)
|
||||
filename = "".join(c for c in filename if c in valid_chars)
|
||||
generate_session_ini(label, port, lab_title, filename, parent_folder)
|
||||
print(f"- {parent_folder}/{lab_title}/{filename}")
|
||||
print(f"File Generation Complete, Restart secureCRT if open already.")
|
66
scripts/breakout-to-secureCRT-windows/readme.md
Normal file
66
scripts/breakout-to-secureCRT-windows/readme.md
Normal file
@@ -0,0 +1,66 @@
|
||||
# CML-Breakout-SecureCRT-Windows
|
||||
It will launch the breakout tool and have it generate a labs.yaml file that contains any running nodes.
|
||||
It will discover your secureCRT configuration folder from the windows registry.
|
||||
It then purges the '{secureCrtConfigFolder}\{sessions}\_CML' folder (if it exists)
|
||||
It then parses the yaml file and generates secureCRT lab folder(s) and session files under a _CML folder in your secureCRT config\sessions folder.
|
||||
It then launches the breakout tool.
|
||||
|
||||
|
||||
Assumptions:
|
||||
- using windows OS
|
||||
- python 3.x is installed and pyYAML module is installed. (pip install pyyaml)
|
||||
- secureCRT is installed and has its config location stored in the registry for the current user.
|
||||
- .bat file assumes all files are in c:\cml\ (adjust as needed)
|
||||
- breakout-windows-amd64.exe is in the same folder as the rest of the files
|
||||
- this creates sessions using ipv6 loopback.
|
||||
- the session name is the label in CML, not the CLI hostname.
|
||||
|
||||
Tested on:
|
||||
- CML: 2.6.1-11
|
||||
- breakout-windows-amd64.exe 0.3.1-build-v2.6.1-11
|
||||
- secureCRT 9.4.1
|
||||
|
||||
Setup:
|
||||
- copy files to c:\cml folder
|
||||
- in config.yaml edit the server name, username, and password.
|
||||
- ensure python 3.x is installed
|
||||
- ensure pyYAML module is installed (pip install pyyaml)
|
||||
- download the windows breakout tool to the c:\cml folder
|
||||
|
||||
|
||||
to run the script:
|
||||
- close secureCRT
|
||||
- ensure any devices you want to access are running in cml
|
||||
- run start-breakout-tool.bat
|
||||
- open secureCRT, you will see a _CML folder with lab folder(s) containing sessions for any running devices in cml.
|
||||
|
||||
Example:
|
||||
```
|
||||
C:\cml>start-breakout.bat
|
||||
get simplified node definitions from controller...
|
||||
get active console keys from controller...
|
||||
get active VNC keys from controller...
|
||||
get all the labs from controller...
|
||||
init with --enable-all flag, enabling all running labs...
|
||||
get all the nodes for the labs from controller...
|
||||
get nodes for lab Lab at Wed 11:43 AM from controller...
|
||||
lab file written.
|
||||
Successfully loaded YAML data from: labs.yaml
|
||||
Generating SecureCRT session.ini files for each node:
|
||||
- Z:\Work\VanDyke\Config\sessions\_CML/Lab at Wed 1143 AM/iosv-0_9000.ini
|
||||
- Z:\Work\VanDyke\Config\sessions\_CML/Lab at Wed 1143 AM/iosvl2-0_9002.ini
|
||||
File Generation Complete, Restart secureCRT if open already.
|
||||
System version: 2.6.1+build.11
|
||||
+------------+---------+----------+
|
||||
| NODE LABEL | DEVICE | ADDRESS |
|
||||
+------------+---------+----------+
|
||||
| iosv-0 | serial0 | TCP/9000 |
|
||||
+------------+ +----------+
|
||||
| iosvl2-0 | | TCP/9002 |
|
||||
+------------+---------+----------+
|
||||
|
||||
Running... Press Ctrl-C to stop.
|
||||
```
|
||||
|
||||
Enjoy.
|
||||
|
1
scripts/breakout-to-secureCRT-windows/requirements.txt
Normal file
1
scripts/breakout-to-secureCRT-windows/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
PyYAML==6.0.1
|
4
scripts/breakout-to-secureCRT-windows/start-breakout.bat
Normal file
4
scripts/breakout-to-secureCRT-windows/start-breakout.bat
Normal file
@@ -0,0 +1,4 @@
|
||||
cd \cml
|
||||
breakout-windows-amd64.exe init --enable-all
|
||||
python.exe main.py
|
||||
breakout-windows-amd64.exe run
|
Reference in New Issue
Block a user