2 from cloud import common
7 class SSHClient(object):
9 def __init__(self, host, username, password):
11 self.username = username
12 self.password = password
15 def open(self, timeout=TIMEOUT):
16 self.client = paramiko.SSHClient()
17 self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
18 self.client.connect(self.host, username=self.username,
19 password=self.password, timeout=timeout)
22 if self.client is not None:
26 def execute(self, command, sudo=False, timeout=TIMEOUT):
27 if sudo and self.username != 'root':
28 command = "sudo -S -p '' %s" % command
29 stdin, stdout, stderr = self.client.exec_command(command,
32 stdin.write(self.password + '\n')
34 return ''.join(''.join(stderr.readlines()) +
35 ''.join(stdout.readlines()))
37 def run(self, command):
38 transport = self.client.get_transport()
39 transport.set_keepalive(1)
40 chan = transport.open_session()
41 chan.exec_command(command)
43 while not chan.exit_status_ready():
45 data = chan.recv(1024)
48 data = chan.recv(1024)
50 if chan.recv_stderr_ready():
51 error_buff = chan.recv_stderr(1024)
54 error_buff = chan.recv_stderr(1024)
55 exit_status = chan.recv_exit_status()
56 LOG.debug('Exit status %s' % exit_status)