Created
July 4, 2023 07:51
-
-
Save foamrider/314c3ff836268447d5274cc15ff6d39e to your computer and use it in GitHub Desktop.
Convert RDB (Microsoft Remote Desktop) to CSV for mRemoteNG
This file contains hidden or 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 csv | |
import json | |
import re | |
import sys | |
import uuid | |
def json_to_csv(json_file, csv_file): | |
with open(json_file) as f: | |
data = json.load(f) | |
# Extract Credentials, Connections, and Groups data | |
credentials = {item["PersistentModelId"]: item["Username"] for item in data.get("Credentials", [])} | |
connections = data.get("Connections", []) | |
csv_data = [] | |
group_uuids = {} | |
# First create 'Container' entries for each group | |
for group in data.get("Groups", []): | |
id = str(uuid.uuid4()) | |
group_uuids[group["PersistentModelId"]] = id | |
csv_data.append([group["Name"], id, '', 'Container'] + ['']*73) | |
# Then create 'Connection' entries | |
for conn in connections: | |
friendly_name = conn.get("FriendlyName") | |
hostname = conn.get("HostName") | |
cred_id = conn.get("CredentialsId") | |
group_id = conn.get("GroupId") | |
full_username = credentials.get(cred_id) | |
if full_username is not None: | |
domain, username = re.match(r'(?:([^@\\]*)[\\@])?(.*)', full_username).groups() | |
else: | |
domain, username = "", "" | |
parent_id = group_uuids.get(group_id) | |
id = str(uuid.uuid4()) | |
csv_data.append([friendly_name, id, parent_id, 'Connection', '', '', '', username, domain, hostname] + ['']*64) | |
with open(csv_file, 'w', newline='') as f: | |
writer = csv.writer(f, delimiter=';') | |
writer.writerow(["Name", "Id", "Parent", "NodeType", "Description", "Icon", "Panel", "Username", "Domain", | |
"Hostname"] + ['']*65) # 65 more empty column headers, not sure if needed | |
writer.writerows(csv_data) | |
if len(sys.argv) != 3: | |
print("Usage: convert-rdb-to-csv.py <input.json> <output.csv>") | |
else: | |
json_file = sys.argv[1] | |
csv_file = sys.argv[2] | |
json_to_csv(json_file, csv_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment