mirror of https://github.com/apache/cloudstack.git
remoteSSHClient cleansed
This commit is contained in:
parent
30c6e2707a
commit
fa58080807
|
|
@ -5,9 +5,9 @@
|
|||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
|
|
@ -15,15 +15,17 @@
|
|||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import paramiko
|
||||
import paramiko
|
||||
import time
|
||||
import cloudstackException
|
||||
import contextlib
|
||||
import logging
|
||||
from contextlib import closing
|
||||
|
||||
|
||||
class remoteSSHClient(object):
|
||||
def __init__(self, host, port, user, passwd, retries = 10, log_lvl=logging.INFO, keyPairFileLocation=None):
|
||||
def __init__(self, host, port, user, passwd, retries=10,
|
||||
log_lvl=logging.INFO, keyPairFileLocation=None):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.user = user
|
||||
|
|
@ -39,29 +41,36 @@ class remoteSSHClient(object):
|
|||
retry_count = retries
|
||||
while True:
|
||||
try:
|
||||
if keyPairFileLocation == None:
|
||||
self.ssh.connect(str(host),int(port), user, passwd)
|
||||
self.logger.debug("SSH connect: %s@%s with passwd %s"%(user, str(host), passwd))
|
||||
if keyPairFileLocation is None:
|
||||
self.ssh.connect(str(host), int(port), user, passwd)
|
||||
self.logger.debug("SSH connect: %s@%s with passwd %s" %
|
||||
(user, str(host), passwd))
|
||||
else:
|
||||
self.ssh.connect(
|
||||
hostname=str(host),
|
||||
port=int(port),
|
||||
username=str(user),
|
||||
key_filename=str(keyPairFileLocation),
|
||||
look_for_keys=False
|
||||
)
|
||||
self.logger.debug("connecting to server %s with user %s key %s"%(str(host), user, keyPairFileLocation))
|
||||
self.logger.debug("SSH connect: %s@%s with passwd %s"%(user, str(host), passwd))
|
||||
self.ssh.connect(hostname=str(host),
|
||||
port=int(port),
|
||||
username=str(user),
|
||||
key_filename=str(keyPairFileLocation),
|
||||
look_for_keys=False
|
||||
)
|
||||
self.logger.debug(
|
||||
"connecting to server %s with user %s key %s" %
|
||||
(str(host), user, keyPairFileLocation))
|
||||
self.logger.debug("SSH connect: %s@%s with passwd %s" %
|
||||
(user, str(host), passwd))
|
||||
except paramiko.SSHException, sshex:
|
||||
if retry_count == 0:
|
||||
raise cloudstackException.InvalidParameterException(repr(sshex))
|
||||
raise cloudstackException. \
|
||||
InvalidParameterException(repr(sshex))
|
||||
retry_count = retry_count - 1
|
||||
time.sleep(5)
|
||||
except paramiko.AuthenticationException, authEx:
|
||||
raise cloudstackException.InvalidParameterException("Invalid credentials to login to %s on port %s"%(str(host), port))
|
||||
raise cloudstackException. \
|
||||
InvalidParameterException("Invalid credentials to "
|
||||
+ "login to %s on port %s" %
|
||||
(str(host), port))
|
||||
else:
|
||||
return
|
||||
|
||||
|
||||
def execute(self, command):
|
||||
stdin, stdout, stderr = self.ssh.exec_command(command)
|
||||
output = stdout.readlines()
|
||||
|
|
@ -71,16 +80,17 @@ class remoteSSHClient(object):
|
|||
if errors is not None and len(errors) > 0:
|
||||
for error in errors:
|
||||
results.append(error.rstrip())
|
||||
|
||||
|
||||
else:
|
||||
for strOut in output:
|
||||
results.append(strOut.rstrip())
|
||||
self.logger.debug("{Cmd: %s via Host: %s} {returns: %s}"%(command, str(self.host), results))
|
||||
self.logger.debug("{Cmd: %s via Host: %s} {returns: %s}" %
|
||||
(command, str(self.host), results))
|
||||
return results
|
||||
|
||||
|
||||
def scp(self, srcFile, destPath):
|
||||
transport = paramiko.Transport((self.host, int(self.port)))
|
||||
transport.connect(username = self.user, password=self.passwd)
|
||||
transport.connect(username=self.user, password=self.passwd)
|
||||
sftp = paramiko.SFTPClient.from_transport(transport)
|
||||
try:
|
||||
sftp.put(srcFile, destPath)
|
||||
|
|
@ -90,7 +100,7 @@ class remoteSSHClient(object):
|
|||
def close(self):
|
||||
self.ssh.close()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with contextlib.closing(remoteSSHClient("10.223.75.10", 22, "root",
|
||||
"password")) as ssh:
|
||||
|
|
|
|||
Loading…
Reference in New Issue