fix container image and use latest code
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / rapid / rapid_sshclient.py
1 ##
2 ## Copyright (c) 2019 Intel Corporation
3 ##
4 ## Licensed under the Apache License, Version 2.0 (the "License");
5 ## you may not use this file except in compliance with the License.
6 ## You may obtain a copy of the License at
7 ##
8 ##     http://www.apache.org/licenses/LICENSE-2.0
9 ##
10 ## Unless required by applicable law or agreed to in writing, software
11 ## distributed under the License is distributed on an "AS IS" BASIS,
12 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ## See the License for the specific language governing permissions and
14 ## limitations under the License.
15 ##
16
17 import paramiko
18 import logging
19
20 class SSHClient:
21     """Wrapper class for paramiko module to connect via SSH
22     """
23     _log = None
24
25     _ip = None
26     _user = None
27     _rsa_private_key = None
28     _timeout = None
29     _ssh = None
30     _connected = False
31
32     _output = None
33     _error = None
34
35     def __init__(self, ip=None, user=None, rsa_private_key=None, timeout=15, logger_name=None):
36         self._ip = ip
37         self._user = user
38         self._rsa_private_key = rsa_private_key
39         self._timeout = timeout
40
41         if (logger_name is not None):
42             self._log = logging.getLogger(logger_name)
43
44         self._connected = False
45
46     def set_credentials(self, ip, user, rsa_private_key):
47         self._ip = ip
48         self._user = user
49         self._rsa_private_key = rsa_private_key
50
51     def connect(self):
52         if self._connected:
53             if (self._log is not None):
54                 self._log.debug("Already connected!")
55             return
56
57         if ((self._ip is None) or (self._user is None) or
58             (self._rsa_private_key is None)):
59             if (self._log is not None):
60                 self._log.error("Wrong parameter! IP %s, user %s, RSA private key %s"
61                                 % (self._ip, self._user, self._rsa_private_key))
62             self._connected = False
63             return
64
65         self._ssh = paramiko.SSHClient()
66         self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
67         private_key = paramiko.RSAKey.from_private_key_file(self._rsa_private_key)
68
69         try:
70             self._ssh.connect(hostname = self._ip, username = self._user, pkey = private_key)
71         except Exception as e:
72             if (self._log is not None):
73                 self._log.error("Failed to connect to the host! IP %s, user %s, RSA private key %s\n%s"
74                                 % (self._ip, self._user, self._rsa_private_key, e))
75             self._connected = False
76             self._ssh.close()
77             return
78
79         self._connected = True
80
81     def disconnect(self):
82         if self._connected:
83             self._connected = False
84             self._ssh.close()
85
86     def run_cmd(self, cmd):
87         self.connect()
88
89         if self._connected is not True:
90             return -1
91
92         try:
93             ret = 0
94             _stdin, stdout, stderr = self._ssh.exec_command(cmd, timeout = self._timeout)
95             self._output = stdout.read()
96             self._error = stderr.read()
97         except Exception as e:
98             if (self._log is not None):
99                 self._log.error("Failed to execute command! IP %s, cmd %s\n%s"
100                                 % (self._ip, cmd, e))
101             ret = -1
102
103         self.disconnect()
104
105         return ret
106
107     def get_output(self):
108         return self._output
109
110     def get_error(self):
111         return self._error