2 ## Copyright (c) 2019 Intel Corporation
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
8 ## http://www.apache.org/licenses/LICENSE-2.0
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.
18 from scp import SCPClient
22 """Wrapper class for paramiko module to connect via SSH
28 _rsa_private_key = None
36 def __init__(self, ip=None, user=None, rsa_private_key=None, timeout=15,
37 logger_name=None, password = None):
40 self._password = password
41 self._rsa_private_key = rsa_private_key
42 self._timeout = timeout
44 if (logger_name is not None):
45 self._log = logging.getLogger(logger_name)
47 self._connected = False
49 def set_credentials(self, ip, user, rsa_private_key, password = None):
52 self._password = password
53 self._rsa_private_key = rsa_private_key
58 if (self._log is not None):
59 self._log.debug("Already connected!")
61 if ((self._ip is None) or (self._user is None) or
62 ((self._rsa_private_key is None) ==
63 (self._password is None))):
64 if (self._log is not None):
65 self._log.error("Wrong parameter! IP %s, user %s, RSA private key %s"
66 % (self._ip, self._user, self._rsa_private_key))
67 self._connected = False
70 self._ssh = paramiko.SSHClient()
71 self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
72 if (self._rsa_private_key is not None):
73 private_key = paramiko.RSAKey.from_private_key_file(self._rsa_private_key)
78 self._ssh.connect(hostname = self._ip, username = self._user,
79 password = self._password, pkey = private_key)
80 except Exception as e:
81 if (self._log is not None):
82 self._log.error("Failed to connect to the host! IP %s, user %s, RSA private key %s\n%s"
83 % (self._ip, self._user, self._rsa_private_key, e))
84 self._connected = False
88 self._connected = True
92 self._connected = False
95 def run_cmd(self, cmd):
98 if self._connected is not True:
103 _stdin, stdout, stderr = self._ssh.exec_command(cmd, timeout = self._timeout)
104 self._output = stdout.read()
105 self._error = stderr.read()
106 except Exception as e:
107 if (self._log is not None):
108 self._log.error("Failed to execute command! IP %s, cmd %s\n%s"
109 % (self._ip, cmd, e))
116 def scp_put(self, src, dst):
119 if self._connected is not True:
124 scp = SCPClient(self._ssh.get_transport())
126 self._output = stdout.read()
127 self._error = stderr.read()
128 except Exception as e:
129 if (self._log is not None):
130 self._log.error("Failed to execute command! IP %s, cmd %s\n%s"
131 % (self._ip, cmd, e))
138 def scp_get(self, src, dst):
141 if self._connected is not True:
146 scp = SCPClient(self._ssh.get_transport())
148 self._output = stdout.read()
149 self._error = stderr.read()
150 except Exception as e:
151 if (self._log is not None):
152 self._log.error("Failed to execute command! IP %s, cmd %s\n%s"
153 % (self._ip, cmd, e))
160 def get_output(self):