diff options
author | Luc Provoost <luc.provoost@intel.com> | 2021-07-16 11:40:11 +0200 |
---|---|---|
committer | Luc Provoost <luc.provoost@intel.com> | 2021-07-16 11:40:11 +0200 |
commit | 8a12bf3b9202cfb02102c9f179dc2cc15be79a63 (patch) | |
tree | 4881c61876131a6c7eb24ecfa53b7a2f2927c1c0 /VNFs/DPPD-PROX/helper-scripts/rapid/rapid_sshclient.py | |
parent | 1c4e666aef045b3aa5eb9999e66b44db00cd47c7 (diff) |
Using paramiko for all ssh & scp
By using paramiko, we can now also use differnt credentials to login into
the PROX instances. In the ssh section of the environment file, you can
now also add the password parameter.
Change-Id: I08d31cbf0d02d7e82b7fbe34268851f73ff5dde0
Signed-off-by: Luc Provoost <luc.provoost@intel.com>
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 |