1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Library for SSH connection management."""
17 from time import time, sleep
21 from paramiko import RSAKey
22 from paramiko.ssh_exception import SSHException
23 from scp import SCPClient
24 from robot.api import logger
25 from robot.utils.asserts import assert_equal
27 __all__ = ["exec_cmd", "exec_cmd_no_error"]
32 class SSHTimeout(Exception):
33 """This exception is raised when a timeout occurs."""
38 """Contains methods for managing and using SSH connections."""
40 __MAX_RECV_BUF = 10*1024*1024
41 __existing_connections = {}
49 """Get IP address and port hash from node dictionary.
51 :param node: Node in topology.
53 :returns: IP address and port for the specified node.
57 return hash(frozenset([node['host'], node['port']]))
59 def connect(self, node, attempts=5):
60 """Connect to node prior to running exec_command or scp.
62 If there already is a connection to the node, this method reuses it.
64 :param node: Node in topology.
65 :param attempts: Number of reconnect attempts.
68 :raises IOError: If cannot connect to host.
71 node_hash = self._node_hash(node)
72 if node_hash in SSH.__existing_connections:
73 self._ssh = SSH.__existing_connections[node_hash]
74 if self._ssh.get_transport().is_active():
75 logger.debug('Reusing SSH: {ssh}'.format(ssh=self._ssh))
78 self._reconnect(attempts-1)
80 raise IOError('Cannot connect to {host}'.
81 format(host=node['host']))
86 if 'priv_key' in node:
87 pkey = RSAKey.from_private_key(
88 StringIO.StringIO(node['priv_key']))
90 self._ssh = paramiko.SSHClient()
91 self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
93 self._ssh.connect(node['host'], username=node['username'],
94 password=node.get('password'), pkey=pkey,
97 self._ssh.get_transport().set_keepalive(10)
99 SSH.__existing_connections[node_hash] = self._ssh
100 logger.debug('New SSH to {peer} took {total} seconds: {ssh}'.
102 peer=self._ssh.get_transport().getpeername(),
103 total=(time() - start),
106 raise IOError('Cannot connect to {host}'.
107 format(host=node['host']))
109 def disconnect(self, node):
110 """Close SSH connection to the node.
112 :param node: The node to disconnect from.
115 node_hash = self._node_hash(node)
116 if node_hash in SSH.__existing_connections:
117 logger.debug('Disconnecting peer: {host}, {port}'.
118 format(host=node['host'], port=node['port']))
119 ssh = SSH.__existing_connections.pop(node_hash)
122 def _reconnect(self, attempts=0):
123 """Close the SSH connection and open it again.
125 :param attempts: Number of reconnect attempts.
129 self.disconnect(node)
130 self.connect(node, attempts)
131 logger.debug('Reconnecting peer done: {host}, {port}'.
132 format(host=node['host'], port=node['port']))
134 def exec_command(self, cmd, timeout=10):
135 """Execute SSH command on a new channel on the connected Node.
137 :param cmd: Command to run on the Node.
138 :param timeout: Maximal time in seconds to wait until the command is
139 done. If set to None then wait forever.
142 :return return_code, stdout, stderr
143 :rtype: tuple(int, str, str)
144 :raise SSHTimeout: If command is not finished in timeout time.
146 stdout = StringIO.StringIO()
147 stderr = StringIO.StringIO()
149 chan = self._ssh.get_transport().open_session(timeout=5)
150 peer = self._ssh.get_transport().getpeername()
151 except AttributeError:
153 chan = self._ssh.get_transport().open_session(timeout=5)
154 peer = self._ssh.get_transport().getpeername()
157 chan = self._ssh.get_transport().open_session(timeout=5)
158 peer = self._ssh.get_transport().getpeername()
159 chan.settimeout(timeout)
161 logger.trace('exec_command on {peer} with timeout {timeout}: {cmd}'
162 .format(peer=peer, timeout=timeout, cmd=cmd))
165 chan.exec_command(cmd)
166 while not chan.exit_status_ready() and timeout is not None:
167 if chan.recv_ready():
168 stdout.write(chan.recv(self.__MAX_RECV_BUF))
170 if chan.recv_stderr_ready():
171 stderr.write(chan.recv_stderr(self.__MAX_RECV_BUF))
173 if time() - start > timeout:
175 'Timeout exception during execution of command: {cmd}\n'
176 'Current contents of stdout buffer: {stdout}\n'
177 'Current contents of stderr buffer: {stderr}\n'
178 .format(cmd=cmd, stdout=stdout.getvalue(),
179 stderr=stderr.getvalue())
183 return_code = chan.recv_exit_status()
185 while chan.recv_ready():
186 stdout.write(chan.recv(self.__MAX_RECV_BUF))
188 while chan.recv_stderr_ready():
189 stderr.write(chan.recv_stderr(self.__MAX_RECV_BUF))
192 logger.trace('exec_command on {peer} took {total} seconds'.
193 format(peer=peer, total=end-start))
195 logger.trace('return RC {rc}'.format(rc=return_code))
196 logger.trace('return STDOUT {stdout}'.format(stdout=stdout.getvalue()))
197 logger.trace('return STDERR {stderr}'.format(stderr=stderr.getvalue()))
198 return return_code, stdout.getvalue(), stderr.getvalue()
200 def exec_command_sudo(self, cmd, cmd_input=None, timeout=30):
201 """Execute SSH command with sudo on a new channel on the connected Node.
203 :param cmd: Command to be executed.
204 :param cmd_input: Input redirected to the command.
205 :param timeout: Timeout.
206 :returns: return_code, stdout, stderr
210 >>> from ssh import SSH
212 >>> ssh.connect(node)
213 >>> # Execute command without input (sudo -S cmd)
214 >>> ssh.exec_command_sudo("ifconfig eth0 down")
215 >>> # Execute command with input (sudo -S cmd <<< "input")
216 >>> ssh.exec_command_sudo("vpp_api_test", "dump_interface_table")
218 if cmd_input is None:
219 command = 'sudo -S {c}'.format(c=cmd)
221 command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input)
222 return self.exec_command(command, timeout)
224 def exec_command_lxc(self, lxc_cmd, lxc_name, lxc_params='', sudo=True,
226 """Execute command in LXC on a new SSH channel on the connected Node.
228 :param lxc_cmd: Command to be executed.
229 :param lxc_name: LXC name.
230 :param lxc_params: Additional parameters for LXC attach.
231 :param sudo: Run in privileged LXC mode. Default: privileged
232 :param timeout: Timeout.
235 :type lxc_params: str
238 :returns: return_code, stdout, stderr
240 command = "lxc-attach {p} --name {n} -- /bin/sh -c '{c}'"\
241 .format(p=lxc_params, n=lxc_name, c=lxc_cmd)
244 command = 'sudo -S {c}'.format(c=command)
245 return self.exec_command(command, timeout)
247 def interactive_terminal_open(self, time_out=45):
248 """Open interactive terminal on a new channel on the connected Node.
250 :param time_out: Timeout in seconds.
251 :returns: SSH channel with opened terminal.
253 .. warning:: Interruptingcow is used here, and it uses
254 signal(SIGALRM) to let the operating system interrupt program
255 execution. This has the following limitations: Python signal
256 handlers only apply to the main thread, so you cannot use this
257 from other threads. You must not use this in a program that
258 uses SIGALRM itself (this includes certain profilers)
260 chan = self._ssh.get_transport().open_session()
263 chan.settimeout(int(time_out))
264 chan.set_combine_stderr(True)
267 while not buf.endswith((":~# ", ":~$ ", "~]$ ", "~]# ")):
269 chunk = chan.recv(self.__MAX_RECV_BUF)
273 if chan.exit_status_ready():
274 logger.error('Channel exit status ready')
276 except socket.timeout:
277 logger.error('Socket timeout: {0}'.format(buf))
278 raise Exception('Socket timeout: {0}'.format(buf))
281 def interactive_terminal_exec_command(self, chan, cmd, prompt):
282 """Execute command on interactive terminal.
284 interactive_terminal_open() method has to be called first!
286 :param chan: SSH channel with opened terminal.
287 :param cmd: Command to be executed.
288 :param prompt: Command prompt, sequence of characters used to
289 indicate readiness to accept commands.
290 :returns: Command output.
292 .. warning:: Interruptingcow is used here, and it uses
293 signal(SIGALRM) to let the operating system interrupt program
294 execution. This has the following limitations: Python signal
295 handlers only apply to the main thread, so you cannot use this
296 from other threads. You must not use this in a program that
297 uses SIGALRM itself (this includes certain profilers)
299 chan.sendall('{c}\n'.format(c=cmd))
301 while not buf.endswith(prompt):
303 chunk = chan.recv(self.__MAX_RECV_BUF)
307 if chan.exit_status_ready():
308 logger.error('Channel exit status ready')
310 except socket.timeout:
311 logger.error('Socket timeout during execution of command: '
312 '{0}\nBuffer content:\n{1}'.format(cmd, buf))
313 raise Exception('Socket timeout during execution of command: '
314 '{0}\nBuffer content:\n{1}'.format(cmd, buf))
315 tmp = buf.replace(cmd.replace('\n', ''), '')
317 tmp.replace(item, '')
321 def interactive_terminal_close(chan):
322 """Close interactive terminal SSH channel.
324 :param: chan: SSH channel to be closed.
328 def scp(self, local_path, remote_path, get=False, timeout=30):
329 """Copy files from local_path to remote_path or vice versa.
331 connect() method has to be called first!
333 :param local_path: Path to local file that should be uploaded; or
334 path where to save remote file.
335 :param remote_path: Remote path where to place uploaded file; or
336 path to remote file which should be downloaded.
337 :param get: scp operation to perform. Default is put.
338 :param timeout: Timeout value in seconds.
339 :type local_path: str
340 :type remote_path: str
345 logger.trace('SCP {0} to {1}:{2}'.format(
346 local_path, self._ssh.get_transport().getpeername(),
349 logger.trace('SCP {0}:{1} to {2}'.format(
350 self._ssh.get_transport().getpeername(), remote_path,
352 # SCPCLient takes a paramiko transport as its only argument
353 scp = SCPClient(self._ssh.get_transport(), socket_timeout=timeout)
356 scp.put(local_path, remote_path)
358 scp.get(remote_path, local_path)
361 logger.trace('SCP took {0} seconds'.format(end-start))
364 def exec_cmd(node, cmd, timeout=600, sudo=False):
365 """Convenience function to ssh/exec/return rc, out & err.
367 Returns (rc, stdout, stderr).
370 raise TypeError('Node parameter is None')
372 raise TypeError('Command parameter is None')
374 raise ValueError('Empty command parameter')
379 except SSHException as err:
380 logger.error("Failed to connect to node" + str(err))
381 return None, None, None
385 (ret_code, stdout, stderr) = ssh.exec_command(cmd, timeout=timeout)
387 (ret_code, stdout, stderr) = ssh.exec_command_sudo(cmd,
389 except SSHException as err:
391 return None, None, None
393 return ret_code, stdout, stderr
396 def exec_cmd_no_error(node, cmd, timeout=600, sudo=False, message=None):
397 """Convenience function to ssh/exec/return out & err.
399 Verifies that return code is zero.
401 :param node: DUT node.
402 :param cmd: Command to be executed.
403 :param timeout: Timeout value in seconds. Default: 600.
404 :param sudo: Sudo privilege execution flag. Default: False.
405 :param message: Error message in case of failure. Default: None.
411 :returns: Stdout, Stderr.
412 :rtype: tuple(str, str)
413 :raise RuntimeError: If bash return code is not 0.
415 ret_code, stdout, stderr = exec_cmd(node, cmd, timeout=timeout, sudo=sudo)
416 msg = ('Command execution failed: "{cmd}"\n{stderr}'.
417 format(cmd=cmd, stderr=stderr) if message is None else message)
419 raise RuntimeError(msg)
421 return stdout, stderr