summaryrefslogtreecommitdiffstats
path: root/utils/installer-adapter/SSHUtils.py
blob: 9c92a3be16f7a2383d7dc6a6c71316c3aa77f7d6 (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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
##############################################################################
# Copyright (c) 2015 Ericsson AB and others.
# Author: Jose Lausuch (jose.lausuch@ericsson.com)
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


import paramiko
from scp import SCPClient
import time
import RelengLogger as rl


class SSH_Connection:

    def __init__(self,
                 host,
                 user,
                 password,
                 use_system_keys=True,
                 private_key=None,
                 use_proxy=False,
                 proxy_host=None,
                 proxy_user=None,
                 proxy_password=None,
                 timeout=10):
        self.host = host
        self.user = user
        self.password = password
        self.use_system_keys = use_system_keys
        self.private_key = private_key
        self.use_proxy = use_proxy
        self.proxy_host = proxy_host
        self.proxy_user = proxy_user
        self.proxy_password = proxy_password
        self.timeout = timeout
        paramiko.util.log_to_file("paramiko.log")
        self.logger = rl.Logger("SSHUtils").getLogger()

    def connect(self):
        client = paramiko.SSHClient()
        if self.use_system_keys:
            client.load_system_host_keys()
        elif self.private_key:
            client.load_host_keys(self.private_key)
        else:
            client.load_host_keys('/dev/null')

        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        t = self.timeout
        proxy = None
        if self.use_proxy:
            proxy_command = 'ssh -o UserKnownHostsFile=/dev/null '
            '-o StrictHostKeyChecking=no %s@%s -W %s:%s' % (self.proxy_user,
                                                            self.proxy_host,
                                                            self.host, 22)
            proxy = paramiko.ProxyCommand(proxy_command)
            self.logger.debug("Proxy command: %s" % proxy_command)
        while t > 0:
            try:
                self.logger.debug(
                    "Trying to stablish ssh connection to %s..." % self.host)
                client.connect(self.host,
                               username=self.user,
                               password=self.password,
                               look_for_keys=True,
                               sock=proxy,
                               pkey=self.private_key,
                               timeout=self.timeout)
                self.logger.debug("Successfully connected to %s!" % self.host)
                return client
            except:
                time.sleep(1)
                t -= 1

        if t == 0:
            return None

    def scp_put(self, local_path, remote_path):
        client = self.connect()
        if client:
            scp = SCPClient(client.get_transport())
            try:
                scp.put(local_path, remote_path)
                client.close()
                return 0
            except Exception, e:
                self.logger.error(e)
                client.close()
                return 1
        else:
            self.logger.error("Cannot stablish ssh connection.")

    def scp_get(self, local_path, remote_path):
        client = self.connect()
        if client:
            scp = SCPClient(client.get_transport())
            try:
                scp.get(remote_path, local_path)
                client.close()
                return 0
            except Exception, e:
                self.logger.error(e)
                client.close()
                return 1
        else:
            self.logger.error("Cannot stablish ssh connection.")
            return 1

    def run_remote_cmd(self, command):
        client = self.connect()
        if client:
            try:
                stdin, stdout, stderr = client.exec_command(command)
                out = ''
                for line in stdout.readlines():
                    out += line
                err = stderr.readlines()
                client.close()
                return out, err
            except:
                client.close()
                return 1
        else:
            self.logger.error("Cannot stablish ssh connection.")
            return 1