mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import paramiko
|
|
import cloudstackException
|
|
class remoteSSHClient(object):
|
|
def __init__(self, host, port, user, passwd):
|
|
self.host = host
|
|
self.port = port
|
|
self.user = user
|
|
self.passwd = passwd
|
|
self.ssh = paramiko.SSHClient()
|
|
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
try:
|
|
self.ssh.connect(str(host),int(port), user, passwd)
|
|
except paramiko.SSHException, sshex:
|
|
raise cloudstackException.InvalidParameterException(repr(sshex))
|
|
|
|
def execute(self, command):
|
|
stdin, stdout, stderr = self.ssh.exec_command(command)
|
|
output = stdout.readlines()
|
|
errors = stderr.readlines()
|
|
results = []
|
|
if output is not None and len(output) == 0:
|
|
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())
|
|
|
|
return results
|
|
|
|
|
|
if __name__ == "__main__":
|
|
ssh = remoteSSHClient("192.168.137.2", 22, "root", "password")
|
|
print ssh.execute("ls -l")
|
|
print ssh.execute("rm x") |