-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement hosts import from SecureCRT
- Loading branch information
Showing
7 changed files
with
237 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Package with logic to import hosts from SecureCRT provider.""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module with SecureCRT parser.""" | ||
from os.path import expanduser | ||
|
||
|
||
class SecureCRTConfigParser(object): | ||
"""SecureCRT xml parser.""" | ||
|
||
meta_sessions = ['Default'] | ||
|
||
@classmethod | ||
def parse_hosts(cls, xml): | ||
"""Parse SecureCRT Sessions.""" | ||
sessions = cls.get_element_by_name( | ||
xml.getchildren(), 'Sessions' | ||
).getchildren() | ||
|
||
parsed_hosts = [] | ||
|
||
for session in sessions: | ||
if session.get('name') not in cls.meta_sessions: | ||
parsed_hosts.append(cls.make_host(session)) | ||
|
||
return parsed_hosts | ||
|
||
@classmethod | ||
def parse_identity(cls, xml): | ||
"""Parse SecureCRT SSH2 raw key.""" | ||
identity = cls.get_element_by_name( | ||
xml.getchildren(), 'SSH2' | ||
) | ||
if identity is None: | ||
return None | ||
|
||
identity_filename = cls.get_element_by_name( | ||
identity.getchildren(), | ||
'Identity Filename V2' | ||
) | ||
|
||
if identity_filename is None: | ||
return None | ||
|
||
path = identity_filename.text.split('/') | ||
public_key_name = path[-1].split('::')[0] | ||
private_key_name = public_key_name.split('.')[0] | ||
|
||
if path[0].startswith('$'): | ||
path.pop(0) | ||
path.insert(0, expanduser("~")) | ||
|
||
path[-1] = public_key_name | ||
public_key_path = '/'.join(path) | ||
path[-1] = private_key_name | ||
private_key_path = '/'.join(path) | ||
|
||
return private_key_path, public_key_path | ||
|
||
@classmethod | ||
def make_host(cls, session): | ||
"""Adapt SecureCRT Session to Termius host.""" | ||
session_attrs = session.getchildren() | ||
|
||
return { | ||
'label': session.get('name'), | ||
'hostname': cls.get_element_by_name( | ||
session_attrs, 'Hostname' | ||
).text, | ||
'port': cls.get_element_by_name( | ||
session_attrs, '[SSH2] Port' | ||
).text, | ||
'username': cls.get_element_by_name( | ||
session_attrs, 'Username' | ||
).text | ||
} | ||
|
||
@classmethod | ||
def get_element_by_name(cls, elements, name): | ||
"""Get SecureCRT config block.""" | ||
for element in elements: | ||
if element.get('name') == name: | ||
return element | ||
|
||
return None |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module with SecureCRT provider.""" | ||
import logging | ||
import xml | ||
|
||
from termius.core.models.terminal import Host, SshConfig, Identity, SshKey, \ | ||
Group | ||
from termius.porting.providers.securecrt.parser import SecureCRTConfigParser | ||
|
||
from ..base import BasePortingProvider | ||
|
||
|
||
class SecureCRTPortingProvider(BasePortingProvider): | ||
"""Synchronize secure crt config content with application.""" | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def __init__(self, source, *args, **kwargs): | ||
"""Contruct new service to sync ssh config.""" | ||
super(SecureCRTPortingProvider, self).__init__(*args, **kwargs) | ||
|
||
self.config_source = source | ||
|
||
def export_hosts(self): | ||
"""Skip export.""" | ||
pass | ||
|
||
def provider_hosts(self): | ||
"""Retrieve host instances from ssh config.""" | ||
root = xml.etree.ElementTree.parse(self.config_source).getroot() | ||
hosts = [] | ||
|
||
raw_hosts = SecureCRTConfigParser.parse_hosts( | ||
root | ||
) | ||
identity_paths = SecureCRTConfigParser.parse_identity(root) | ||
main_group = Group(label='SecureCRT') | ||
|
||
group_config = SshConfig( | ||
identity=Identity( | ||
is_visible=False, | ||
label='SecureCRT' | ||
) | ||
) | ||
|
||
if identity_paths: | ||
try: | ||
with open(identity_paths[0], 'rb') as private_key_file: | ||
private_key = private_key_file.read() | ||
|
||
with open(identity_paths[1], 'rb') as public_key_file: | ||
public_key = public_key_file.read() | ||
|
||
key = SshKey( | ||
label='SecureCRT', | ||
private_key=private_key, | ||
public_key=public_key | ||
) | ||
group_config.identity.ssh_key = key | ||
except IOError: | ||
self.logger.info( | ||
'Cannot find SSH2 raw key %s' % identity_paths[1] | ||
) | ||
|
||
main_group.ssh_config = group_config | ||
|
||
for raw_host in raw_hosts: | ||
host = Host( | ||
label=raw_host['label'], | ||
address=raw_host['hostname'] | ||
) | ||
host.group = main_group | ||
ssh_config = SshConfig( | ||
port=raw_host['port'], | ||
identity=Identity( | ||
username=raw_host.get('username'), | ||
is_visible=False, | ||
label=raw_host.get('username') | ||
) | ||
) | ||
|
||
host.ssh_config = ssh_config | ||
|
||
hosts.append(host) | ||
|
||
return hosts |
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