fix regex expression to find IPV4 address
[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 from scp import SCPClient
19 import logging
20
21 class SSHClient:
22     """Wrapper class for paramiko module to connect via SSH
23     """
24     _log = None
25
26     _ip = None
27     _user = None
28     _rsa_private_key = None
29     _timeout = None
30     _ssh = None
31     _connected = False
32
33     _output = None
34     _error = None
35
36     def __init__(self, ip=None, user=None, rsa_private_key=None, timeout=15,
37             logger_name=None, password = None):
38         self._ip = ip
39         self._user = user
40         self._password = password
41         self._rsa_private_key = rsa_private_key
42         self._timeout = timeout
43
44         if (logger_name is not None):
45             self._log = logging.getLogger(logger_name)
46
47         self._connected = False
48
49     def set_credentials(self, ip, user, rsa_private_key, password = None):
50         self._ip = ip
51         self._user = user
52         self._password = password
53         self._rsa_private_key = rsa_private_key
54
55     def connect(self):
56
57         if self._connected:
58             if (self._log is not None):
59                 self._log.debug("Already connected!")
60             return
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
68             return
69
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)
74         else:
75             private_key = None
76
77         try:
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
85             self._ssh.close()
86             return
87
88         self._connected = True
89
90     def disconnect(self):
91         if self._connected:
92             self._connected = False
93             self._ssh.close()
94
95     def run_cmd(self, cmd):
96         self.connect()
97
98         if self._connected is not True:
99             return -1
100
101         try:
102             ret = 0
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))
110             ret = -1
111
112         self.disconnect()
113
114         return ret
115
116     def scp_put(self, src, dst):
117         self.connect()
118
119         if self._connected is not True:
120             return -1
121
122         try:
123             ret = 0
124             scp = SCPClient(self._ssh.get_transport())
125             scp.put(src, dst)
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))
132             ret = -1
133
134         self.disconnect()
135
136         return ret
137
138     def scp_get(self, src, dst):
139         self.connect()
140
141         if self._connected is not True:
142             return -1
143
144         try:
145             ret = 0
146             scp = SCPClient(self._ssh.get_transport())
147             scp.get(src, dst)
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))
154             ret = -1
155
156         self.disconnect()
157
158         return ret
159
160     def get_output(self):
161         return self._output
162
163     def get_error(self):
164         return self._error