This repository was archived by the owner on Mar 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathparser.py
More file actions
66 lines (51 loc) · 2.13 KB
/
parser.py
File metadata and controls
66 lines (51 loc) · 2.13 KB
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
# -*- coding: utf-8 -*-
"""Module with ssh config parser."""
import re
from paramiko.config import SSHConfig
class SSHConfigParser(SSHConfig):
"""Class to override parse method of paramiko parser."""
def parse(self, file_obj): # noqa
"""
Read an OpenSSH config from the given file object.
:param file_obj: a file-like object to read the config file from
"""
termius_group = "# termius:group"
termius_ignore_regexp = re.compile(r'# termius:ignore')
termius_groups_regexp = re.compile(r'{}'.format(termius_group))
host = {'host': ['*'], 'config': {}}
for line in file_obj:
line = line.strip()
if not line:
continue
if line.startswith('#'):
ignore_comment = termius_ignore_regexp.match(line)
if ignore_comment:
host['config']['ignore'] = ''
groups_comment = termius_groups_regexp.match(line)
if groups_comment:
host['group'] = line.replace(termius_group, "").strip()
continue
match = re.match(self.SETTINGS_REGEX, line)
if not match:
raise Exception('Unparsable line %s' % line)
key = match.group(1).lower()
value = match.group(2)
if key == 'host':
self._config.append(host)
host = {
'host': self._get_hosts(value),
'config': {}
}
elif key == 'proxycommand' and value.lower() == 'none':
host['config'][key] = None
else:
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
if key in ['identityfile', 'localforward', 'remoteforward']:
if key in host['config']:
host['config'][key].append(value)
else:
host['config'][key] = [value]
elif key not in host['config']:
host['config'][key] = value
self._config.append(host)