-
Notifications
You must be signed in to change notification settings - Fork 1
/
crt2foss.py
executable file
·99 lines (77 loc) · 2.46 KB
/
crt2foss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import errno
import re
from typing import List
item_regex = re.compile(r'.*(?P<type>\w):"(?P<key>.+?)"=(?P<value>.*)$', re.DOTALL)
def parse_ini(path: str) -> dict:
values = []
with open(path) as f:
for line in f.readlines():
line = line.strip()
if line.startswith("\ufeff"):
line = line[1:]
if len(line) > 2 and (line[1] == ":" or line[2] == ":"):
values.append(line)
elif line == "":
continue
else:
values[-1] += "\n" + line
matches = []
for i in values:
try:
match = item_regex.match(i)
matches.append(match.groupdict())
except Exception:
pass
result = {}
for match in matches:
t = match["type"]
k = match["key"]
v = match["value"]
if t == "S":
result[k] = v
if t == "D":
result[k] = int(v, 16)
# We don't care about the other types
return result
def gen_cmdlines_ssh(ini: dict, cfgdir: str) -> List[str]:
hostname = ini["Hostname"]
port = ini.get("[SSH2] Port", 22)
username = ini["Username"]
keyfile = ini.get("Identity Filename V2", None)
if keyfile:
keyfile = os.path.normpath(
keyfile \
.replace('\\', '/') \
.replace('${VDS_CONFIG_PATH}', cfgdir)
) \
.replace('"', '\\"')
cmdline = []
if keyfile:
cmdline += ["-i", f'"{keyfile}"']
if port != 22:
cmdline += ["-p", str(port)]
cmdline.append(f"{username}@{hostname}")
ssh = ["ssh"] + cmdline
scp = ["scp"] + cmdline
return [' '.join(ssh), ' '.join(scp)]
def gen_cmdlines(ini: dict, cfgdir: str) -> List[str]:
if ini["Protocol Name"].startswith("SSH"):
return gen_cmdlines_ssh(ini, cfgdir)
return []
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} path/to/Config path/to/session.ini", file=sys.stderr)
sys.exit(1)
cfg = sys.argv[1]
ini = sys.argv[2]
if not os.path.exists(ini):
raise OSError(f"Session file does not exist: '{ini}'", errno=errno.ENOENT)
if not os.path.isfile(ini):
raise OSError(f"Not a file: '{ini}'", errno=errno.ENFILE)
inidict = parse_ini(ini)
cmdlines = gen_cmdlines(inidict, cfg)
print('\n'.join(cmdlines))