Created
December 8, 2021 20:22
-
-
Save hellt/5645e306f0f1937322d73b6803f16008 to your computer and use it in GitHub Desktop.
Royal TSX containerlab integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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