Skip to content

Instantly share code, notes, and snippets.

@hellt
Created December 8, 2021 20:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellt/5645e306f0f1937322d73b6803f16008 to your computer and use it in GitHub Desktop.
Save hellt/5645e306f0f1937322d73b6803f16008 to your computer and use it in GitHub Desktop.
Royal TSX containerlab integration
import subprocess
import json
# connect to the remote VM and list all running labs in JSON format
s = subprocess.Popen("ssh {user}@{host} {cmd}".format(user="root", host="12.32.123.3", cmd='clab inspect --all --format json'), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
all_containers = json.loads(s[0])
baseConfig = json.dumps({
"Objects": [
{
"Type": "Credential",
"Name": "srl",
"Username": "admin",
"Password": "admin",
"ID": "000001",
"Path": "/Settings"
},
{
"Type": "SecureGateway",
"Name": "srl-host",
"ComputerName": "12.32.123.3",
"Username": "root",
"Password" : "IloveMyPassword!",
"ID": "000002",
"Path": "/Settings"
},
]
}
)
folders = set([])
for container in all_containers:
folders.add(container['lab_name'])
cfg = json.loads(baseConfig)
for folder in folders:
cfg['Objects'].append({'Type': 'Folder', 'Name': folder, "SecureGatewayID": "000002", "SecureGatewayFromParent": "true", "CredentialsFromParent": "true" ,'Objects': []})
for obj in cfg['Objects']:
if obj['Type'] == 'Folder':
for container in all_containers:
if container['lab_name'] == obj['Name']:
obj['Objects'].append({'Type':'TerminalConnection',
'TerminalConnectionType': 'SSH',
"Name": container['name'],
"ComputerName": container['ipv4_address'].split('/')[0],
"CredentialID": "000001",
"SecureGatewayUsage": "Always",
"SecureGatewayID": "000002"})
ssr = json.dumps(cfg)
print(ssr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment