1 # Copyright 2013: Mirantis Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 # yardstick comment: this is a modified copy of rally/rally/common/sshutils.py
18 """High level ssh library.
22 Execute command and get output:
24 ssh = sshclient.SSH("root", "example.com", port=33)
25 status, stdout, stderr = ssh.execute("ps ax")
27 raise Exception("Command failed with non-zero status.")
28 print(stdout.splitlines())
30 Execute command with huge output:
32 class PseudoFile(io.RawIOBase):
37 ssh = SSH("root", "example.com")
38 with PseudoFile() as p:
39 ssh.run("tail -f /var/log/syslog", stdout=p, timeout=False)
41 Execute local script on remote side:
43 ssh = sshclient.SSH("user", "example.com")
45 with open("~/myscript.sh", "r") as stdin_file:
46 status, out, err = ssh.execute('/bin/sh -s "arg1" "arg2"',
51 ssh = SSH("user", "example.com")
52 # use rb for binary files
53 with open("/store/file.gz", "rb") as stdin_file:
54 ssh.run("cat > ~/upload/file.gz", stdin=stdin_file)
58 eventlet.monkey_patch(select=True, time=True)
60 eventlet.monkey_patch()
62 sshclient = eventlet.import_patched("yardstick.ssh")
74 from chainmap import ChainMap
75 from oslo_utils import encodeutils
76 from scp import SCPClient
79 from yardstick.common import exceptions
80 from yardstick.common.utils import try_int, NON_NONE_DEFAULT, make_dict_from_map
81 from yardstick.network_services.utils import provision_tool
84 def convert_key_to_str(key):
85 if not isinstance(key, (paramiko.RSAKey, paramiko.DSSKey)):
88 key.write_private_key(k)
92 # class SSHError(Exception):
96 # class SSHTimeout(SSHError):
101 """Represent ssh connection."""
103 SSH_PORT = paramiko.config.SSH_PORT
104 DEFAULT_WAIT_TIMEOUT = 120
107 def gen_keys(key_filename, bit_count=2048):
108 rsa_key = paramiko.RSAKey.generate(bits=bit_count, progress_func=None)
109 rsa_key.write_private_key_file(key_filename)
110 print("Writing %s ..." % key_filename)
111 with open('.'.join([key_filename, "pub"]), "w") as pubkey_file:
112 pubkey_file.write(rsa_key.get_name())
113 pubkey_file.write(' ')
114 pubkey_file.write(rsa_key.get_base64())
115 pubkey_file.write('\n')
119 # must return static class name, anything else refers to the calling class
120 # i.e. the subclass, not the superclass
124 def get_arg_key_map(cls):
126 'user': ('user', NON_NONE_DEFAULT),
127 'host': ('ip', NON_NONE_DEFAULT),
128 'port': ('ssh_port', cls.SSH_PORT),
129 'pkey': ('pkey', None),
130 'key_filename': ('key_filename', None),
131 'password': ('password', None),
132 'name': ('name', None),
135 def __init__(self, user, host, port=None, pkey=None,
136 key_filename=None, password=None, name=None):
137 """Initialize SSH client.
139 :param user: ssh username
140 :param host: hostname or ip address of remote ssh server
141 :param port: remote ssh port
142 :param pkey: RSA or DSS private key string or file object
143 :param key_filename: private key filename
144 :param password: password
148 self.log = logging.getLogger(__name__ + '.' + self.name)
150 self.log = logging.getLogger(__name__)
152 self.wait_timeout = self.DEFAULT_WAIT_TIMEOUT
155 # everybody wants to debug this in the caller, do it here instead
156 self.log.debug("user:%s host:%s", user, host)
158 # we may get text port from YAML, convert to int
159 self.port = try_int(port, self.SSH_PORT)
160 self.pkey = self._get_pkey(pkey) if pkey else None
161 self.password = password
162 self.key_filename = key_filename
164 # paramiko loglevel debug will output ssh protocl debug
165 # we don't ever really want that unless we are debugging paramiko
167 if os.environ.get("PARAMIKO_DEBUG", "").lower() == "true":
168 logging.getLogger("paramiko").setLevel(logging.DEBUG)
170 logging.getLogger("paramiko").setLevel(logging.WARN)
173 def args_from_node(cls, node, overrides=None, defaults=None):
174 if overrides is None:
179 params = ChainMap(overrides, node, defaults)
180 return make_dict_from_map(params, cls.get_arg_key_map())
183 def from_node(cls, node, overrides=None, defaults=None):
184 return cls(**cls.args_from_node(node, overrides, defaults))
186 def _get_pkey(self, key):
187 if isinstance(key, six.string_types):
188 key = six.moves.StringIO(key)
190 for key_class in (paramiko.rsakey.RSAKey, paramiko.dsskey.DSSKey):
192 return key_class.from_private_key(key)
193 except paramiko.SSHException as e:
195 raise exceptions.SSHError(error_msg='Invalid pkey: %s' % errors)
198 def is_connected(self):
199 return bool(self._client)
201 def _get_client(self):
202 if self.is_connected:
205 self._client = paramiko.SSHClient()
206 self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
207 self._client.connect(self.host, username=self.user,
208 port=self.port, pkey=self.pkey,
209 key_filename=self.key_filename,
210 password=self.password,
211 allow_agent=False, look_for_keys=False,
214 except Exception as e:
215 message = ("Exception %(exception_type)s was raised "
216 "during connect. Exception value is: %(exception)r" %
217 {"exception": e, "exception_type": type(e)})
219 raise exceptions.SSHError(error_msg=message)
221 def _make_dict(self):
227 'key_filename': self.key_filename,
228 'password': self.password,
233 return self.get_class()(**self._make_dict())
240 def run(self, cmd, stdin=None, stdout=None, stderr=None,
241 raise_on_error=True, timeout=3600,
242 keep_stdin_open=False, pty=False):
243 """Execute specified command on the server.
245 :param cmd: Command to be executed.
247 :param stdin: Open file or string to pass to stdin.
248 :param stdout: Open file to connect to stdout.
249 :param stderr: Open file to connect to stderr.
250 :param raise_on_error: If False then exit code will be return. If True
251 then exception will be raized if non-zero code.
252 :param timeout: Timeout in seconds for command execution.
253 Default 1 hour. No timeout if set to 0.
254 :param keep_stdin_open: don't close stdin on empty reads
255 :type keep_stdin_open: bool
256 :param pty: Request a pseudo terminal for this connection.
257 This allows passing control characters.
262 client = self._get_client()
264 if isinstance(stdin, six.string_types):
265 stdin = six.moves.StringIO(stdin)
267 return self._run(client, cmd, stdin=stdin, stdout=stdout,
268 stderr=stderr, raise_on_error=raise_on_error,
270 keep_stdin_open=keep_stdin_open, pty=pty)
272 def _run(self, client, cmd, stdin=None, stdout=None, stderr=None,
273 raise_on_error=True, timeout=3600,
274 keep_stdin_open=False, pty=False):
276 transport = client.get_transport()
277 session = transport.open_session()
280 session.exec_command(cmd)
281 start_time = time.time()
283 # encode on transmit, decode on receive
284 data_to_send = encodeutils.safe_encode("", incoming='utf-8')
287 # If we have data to be sent to stdin then `select' should also
288 # check for stdin availability.
289 if stdin and not stdin.closed:
295 # Block until data can be read/write.
296 e = select.select([session], writes, [session], 1)[2]
298 if session.recv_ready():
299 data = encodeutils.safe_decode(session.recv(4096), 'utf-8')
300 self.log.debug("stdout: %r", data)
301 if stdout is not None:
305 if session.recv_stderr_ready():
306 stderr_data = encodeutils.safe_decode(
307 session.recv_stderr(4096), 'utf-8')
308 self.log.debug("stderr: %r", stderr_data)
309 if stderr is not None:
310 stderr.write(stderr_data)
313 if session.send_ready():
314 if stdin is not None and not stdin.closed:
316 stdin_txt = stdin.read(4096)
317 if stdin_txt is None:
319 data_to_send = encodeutils.safe_encode(
320 stdin_txt, incoming='utf-8')
322 # we may need to keep stdin open
323 if not keep_stdin_open:
325 session.shutdown_write()
328 sent_bytes = session.send(data_to_send)
329 # LOG.debug("sent: %s" % data_to_send[:sent_bytes])
330 data_to_send = data_to_send[sent_bytes:]
332 if session.exit_status_ready():
335 if timeout and (time.time() - timeout) > start_time:
336 message = ('Timeout executing command %(cmd)s on host %(host)s'
337 % {"cmd": cmd, "host": self.host})
338 raise exceptions.SSHTimeout(error_msg=message)
340 raise exceptions.SSHError(error_msg='Socket error')
342 exit_status = session.recv_exit_status()
343 if exit_status != 0 and raise_on_error:
344 fmt = "Command '%(cmd)s' failed with exit_status %(status)d."
345 details = fmt % {"cmd": cmd, "status": exit_status}
347 details += " Last stderr data: '%s'." % stderr_data
348 raise exceptions.SSHError(error_msg=details)
351 def execute(self, cmd, stdin=None, timeout=3600, raise_on_error=False):
352 """Execute the specified command on the server.
354 :param cmd: (str) Command to be executed.
355 :param stdin: (StringIO) Open file to be sent on process stdin.
356 :param timeout: (int) Timeout for execution of the command.
357 :param raise_on_error: (bool) If True, then an SSHError will be raised
358 when non-zero exit code.
360 :returns: tuple (exit_status, stdout, stderr)
362 stdout = six.moves.StringIO()
363 stderr = six.moves.StringIO()
365 exit_status = self.run(cmd, stderr=stderr,
366 stdout=stdout, stdin=stdin,
367 timeout=timeout, raise_on_error=raise_on_error)
370 return exit_status, stdout.read(), stderr.read()
372 def wait(self, timeout=None, interval=1):
373 """Wait for the host will be available via ssh."""
375 timeout = self.wait_timeout
377 end_time = time.time() + timeout
380 return self.execute("uname")
381 except (socket.error, exceptions.SSHError) as e:
382 self.log.debug("Ssh is still unavailable: %r", e)
384 if time.time() > end_time:
385 raise exceptions.SSHTimeout(
386 error_msg='Timeout waiting for "%s"' % self.host)
388 def put(self, files, remote_path=b'.', recursive=False):
389 client = self._get_client()
391 with SCPClient(client.get_transport()) as scp:
392 scp.put(files, remote_path, recursive)
394 def get(self, remote_path, local_path='/tmp/', recursive=True):
395 client = self._get_client()
397 with SCPClient(client.get_transport()) as scp:
398 scp.get(remote_path, local_path, recursive)
400 # keep shell running in the background, e.g. screen
401 def send_command(self, command):
402 client = self._get_client()
403 client.exec_command(command, get_pty=True)
405 def _put_file_sftp(self, localpath, remotepath, mode=None):
406 client = self._get_client()
408 with client.open_sftp() as sftp:
409 sftp.put(localpath, remotepath)
411 mode = 0o777 & os.stat(localpath).st_mode
412 sftp.chmod(remotepath, mode)
414 TILDE_EXPANSIONS_RE = re.compile("(^~[^/]*/)?(.*)")
416 def _put_file_shell(self, localpath, remotepath, mode=None):
417 # quote to stop wordpslit
418 tilde, remotepath = self.TILDE_EXPANSIONS_RE.match(remotepath).groups()
421 cmd = ['cat > %s"%s"' % (tilde, remotepath)]
423 # use -- so no options
424 cmd.append('chmod -- 0%o %s"%s"' % (mode, tilde, remotepath))
426 with open(localpath, "rb") as localfile:
427 # only chmod on successful cat
428 self.run("&& ".join(cmd), stdin=localfile)
430 def put_file(self, localpath, remotepath, mode=None):
431 """Copy specified local file to the server.
433 :param localpath: Local filename.
434 :param remotepath: Remote filename.
435 :param mode: Permissions to set after upload
438 self._put_file_sftp(localpath, remotepath, mode=mode)
439 except (paramiko.SSHException, socket.error):
440 self._put_file_shell(localpath, remotepath, mode=mode)
442 def provision_tool(self, tool_path, tool_file=None):
443 return provision_tool(self, tool_path, tool_file)
445 def put_file_obj(self, file_obj, remotepath, mode=None):
446 client = self._get_client()
448 with client.open_sftp() as sftp:
449 sftp.putfo(file_obj, remotepath)
451 sftp.chmod(remotepath, mode)
453 def get_file_obj(self, remotepath, file_obj):
454 client = self._get_client()
456 with client.open_sftp() as sftp:
457 sftp.getfo(remotepath, file_obj)
460 class AutoConnectSSH(SSH):
463 def get_arg_key_map(cls):
464 arg_key_map = super(AutoConnectSSH, cls).get_arg_key_map()
465 arg_key_map['wait'] = ('wait', True)
468 # always wait or we will get OpenStack SSH errors
469 def __init__(self, user, host, port=None, pkey=None,
470 key_filename=None, password=None, name=None, wait=True):
471 super(AutoConnectSSH, self).__init__(user, host, port, pkey, key_filename, password, name)
472 if wait and wait is not True:
473 self.wait_timeout = int(wait)
475 def _make_dict(self):
476 data = super(AutoConnectSSH, self)._make_dict()
478 'wait': self.wait_timeout
483 if not self.is_connected:
485 timeout = self.wait_timeout
487 end_time = time.time() + timeout
490 return self._get_client()
491 except (socket.error, exceptions.SSHError) as e:
492 self.log.debug("Ssh is still unavailable: %r", e)
494 if time.time() > end_time:
495 raise exceptions.SSHTimeout(
496 error_msg='Timeout waiting for "%s"' % self.host)
498 def drop_connection(self):
499 """ Don't close anything, just force creation of a new client """
502 def execute(self, cmd, stdin=None, timeout=3600):
504 return super(AutoConnectSSH, self).execute(cmd, stdin, timeout)
506 def run(self, cmd, stdin=None, stdout=None, stderr=None,
507 raise_on_error=True, timeout=3600,
508 keep_stdin_open=False, pty=False):
510 return super(AutoConnectSSH, self).run(cmd, stdin, stdout, stderr, raise_on_error,
511 timeout, keep_stdin_open, pty)
513 def put(self, files, remote_path=b'.', recursive=False):
515 return super(AutoConnectSSH, self).put(files, remote_path, recursive)
517 def put_file(self, local_path, remote_path, mode=None):
519 return super(AutoConnectSSH, self).put_file(local_path, remote_path, mode)
521 def put_file_obj(self, file_obj, remote_path, mode=None):
523 return super(AutoConnectSSH, self).put_file_obj(file_obj, remote_path, mode)
525 def get_file_obj(self, remote_path, file_obj):
527 return super(AutoConnectSSH, self).get_file_obj(remote_path, file_obj)
529 def provision_tool(self, tool_path, tool_file=None):
531 return super(AutoConnectSSH, self).provision_tool(tool_path, tool_file)
535 # must return static class name, anything else refers to the calling class
536 # i.e. the subclass, not the superclass
537 return AutoConnectSSH