diff options
Diffstat (limited to 'VNFs/DPPD-PROX/helper-scripts/rapid/rapid_sshclient.py')
-rw-r--r-- | VNFs/DPPD-PROX/helper-scripts/rapid/rapid_sshclient.py | 65 |
1 files changed, 59 insertions, 6 deletions
diff --git a/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_sshclient.py b/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_sshclient.py index e9fe134c..d8aeacc1 100644 --- a/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_sshclient.py +++ b/VNFs/DPPD-PROX/helper-scripts/rapid/rapid_sshclient.py @@ -15,6 +15,7 @@ ## import paramiko +from scp import SCPClient import logging class SSHClient: @@ -32,9 +33,11 @@ class SSHClient: _output = None _error = None - def __init__(self, ip=None, user=None, rsa_private_key=None, timeout=15, logger_name=None): + def __init__(self, ip=None, user=None, rsa_private_key=None, timeout=15, + logger_name=None, password = None): self._ip = ip self._user = user + self._password = password self._rsa_private_key = rsa_private_key self._timeout = timeout @@ -43,19 +46,21 @@ class SSHClient: self._connected = False - def set_credentials(self, ip, user, rsa_private_key): + def set_credentials(self, ip, user, rsa_private_key, password = None): self._ip = ip self._user = user + self._password = password self._rsa_private_key = rsa_private_key def connect(self): + if self._connected: if (self._log is not None): self._log.debug("Already connected!") return - if ((self._ip is None) or (self._user is None) or - (self._rsa_private_key is None)): + ((self._rsa_private_key is None) == + (self._password is None))): if (self._log is not None): self._log.error("Wrong parameter! IP %s, user %s, RSA private key %s" % (self._ip, self._user, self._rsa_private_key)) @@ -64,10 +69,14 @@ class SSHClient: self._ssh = paramiko.SSHClient() self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - private_key = paramiko.RSAKey.from_private_key_file(self._rsa_private_key) + if (self._rsa_private_key is not None): + private_key = paramiko.RSAKey.from_private_key_file(self._rsa_private_key) + else: + private_key = None try: - self._ssh.connect(hostname = self._ip, username = self._user, pkey = private_key) + self._ssh.connect(hostname = self._ip, username = self._user, + password = self._password, pkey = private_key) except Exception as e: if (self._log is not None): self._log.error("Failed to connect to the host! IP %s, user %s, RSA private key %s\n%s" @@ -104,6 +113,50 @@ class SSHClient: return ret + def scp_put(self, src, dst): + self.connect() + + if self._connected is not True: + return -1 + + try: + ret = 0 + scp = SCPClient(self._ssh.get_transport()) + scp.put(src, dst) + self._output = stdout.read() + self._error = stderr.read() + except Exception as e: + if (self._log is not None): + self._log.error("Failed to execute command! IP %s, cmd %s\n%s" + % (self._ip, cmd, e)) + ret = -1 + + self.disconnect() + + return ret + + def scp_get(self, src, dst): + self.connect() + + if self._connected is not True: + return -1 + + try: + ret = 0 + scp = SCPClient(self._ssh.get_transport()) + scp.get(src, dst) + self._output = stdout.read() + self._error = stderr.read() + except Exception as e: + if (self._log is not None): + self._log.error("Failed to execute command! IP %s, cmd %s\n%s" + % (self._ip, cmd, e)) + ret = -1 + + self.disconnect() + + return ret + def get_output(self): return self._output |