X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2Fssh.py;h=60f62561be1f453f7bb400a36a3392160d897f0a;hp=4beec5aa83c4979fb6e0efe2addaa62c4e7aa3a9;hb=cc0c2870a18fb74a56410eca2d1870bddc945397;hpb=5f809dd2414d50fd6f229fc2ba2ff42d66df5dde diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index 4beec5aa83..60f62561be 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -13,15 +13,17 @@ """Library for SSH connection management.""" -import paramiko + import socket import StringIO -from paramiko import RSAKey +from time import time, sleep + +from paramiko import RSAKey, SSHClient, AutoAddPolicy from paramiko.ssh_exception import SSHException, NoValidConnectionsError from robot.api import logger -from scp import SCPClient -from time import time, sleep +from scp import SCPClient, SCPException + __all__ = ["exec_cmd", "exec_cmd_no_error"] @@ -86,8 +88,8 @@ class SSH(object): pkey = RSAKey.from_private_key( StringIO.StringIO(node['priv_key'])) - self._ssh = paramiko.SSHClient() - self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self._ssh = SSHClient() + self._ssh.set_missing_host_key_policy(AutoAddPolicy()) self._ssh.connect(node['host'], username=node['username'], password=node.get('password'), pkey=pkey, @@ -465,3 +467,33 @@ def exec_cmd_no_error(node, cmd, timeout=600, sudo=False, message=None): raise RuntimeError(msg) return stdout, stderr + +def scp_node(node, local_path, remote_path, get=False, timeout=30): + """Copy files from local_path to remote_path or vice versa. + + :param node: SUT node. + :param local_path: Path to local file that should be uploaded; or + path where to save remote file. + :param remote_path: Remote path where to place uploaded file; or + path to remote file which should be downloaded. + :param get: scp operation to perform. Default is put. + :param timeout: Timeout value in seconds. + :type node: dict + :type local_path: str + :type remote_path: str + :type get: bool + :type timeout: int + :raises RuntimeError: If SSH connection failed or SCP transfer failed. + """ + ssh = SSH() + + try: + ssh.connect(node) + except SSHException: + raise RuntimeError('Failed to connect to {host}!' + .format(host=node['host'])) + try: + ssh.scp(local_path, remote_path, get, timeout) + except SCPException: + raise RuntimeError('SCP execution failed on {host}!' + .format(host=node['host']))