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")
73 from scp import SCPClient
80 class SSHError(Exception):
84 class SSHTimeout(SSHError):
89 """Represent ssh connection."""
91 def __init__(self, user, host, port=DEFAULT_PORT, pkey=None,
92 key_filename=None, password=None, name=None):
93 """Initialize SSH client.
95 :param user: ssh username
96 :param host: hostname or ip address of remote ssh server
97 :param port: remote ssh port
98 :param pkey: RSA or DSS private key string or file object
99 :param key_filename: private key filename
100 :param password: password
104 self.log = logging.getLogger(__name__ + '.' + self.name)
106 self.log = logging.getLogger(__name__)
110 # we may get text port from YAML, convert to int
111 self.port = int(port)
112 self.pkey = self._get_pkey(pkey) if pkey else None
113 self.password = password
114 self.key_filename = key_filename
116 # paramiko loglevel debug will output ssh protocl debug
117 # we don't ever really want that unless we are debugging paramiko
119 if os.environ.get("PARAMIKO_DEBUG", "").lower() == "true":
120 logging.getLogger("paramiko").setLevel(logging.DEBUG)
122 logging.getLogger("paramiko").setLevel(logging.WARN)
124 def _get_pkey(self, key):
125 if isinstance(key, six.string_types):
126 key = six.moves.StringIO(key)
128 for key_class in (paramiko.rsakey.RSAKey, paramiko.dsskey.DSSKey):
130 return key_class.from_private_key(key)
131 except paramiko.SSHException as e:
133 raise SSHError("Invalid pkey: %s" % (errors))
135 def _get_client(self):
139 self._client = paramiko.SSHClient()
140 self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
141 self._client.connect(self.host, username=self.user,
142 port=self.port, pkey=self.pkey,
143 key_filename=self.key_filename,
144 password=self.password,
145 allow_agent=False, look_for_keys=False,
148 except Exception as e:
149 message = ("Exception %(exception_type)s was raised "
150 "during connect. Exception value is: %(exception)r")
152 raise SSHError(message % {"exception": e,
153 "exception_type": type(e)})
159 def run(self, cmd, stdin=None, stdout=None, stderr=None,
160 raise_on_error=True, timeout=3600,
161 keep_stdin_open=False, pty=False):
162 """Execute specified command on the server.
164 :param cmd: Command to be executed.
166 :param stdin: Open file or string to pass to stdin.
167 :param stdout: Open file to connect to stdout.
168 :param stderr: Open file to connect to stderr.
169 :param raise_on_error: If False then exit code will be return. If True
170 then exception will be raized if non-zero code.
171 :param timeout: Timeout in seconds for command execution.
172 Default 1 hour. No timeout if set to 0.
173 :param keep_stdin_open: don't close stdin on empty reads
174 :type keep_stdin_open: bool
175 :param pty: Request a pseudo terminal for this connection.
176 This allows passing control characters.
181 client = self._get_client()
183 if isinstance(stdin, six.string_types):
184 stdin = six.moves.StringIO(stdin)
186 return self._run(client, cmd, stdin=stdin, stdout=stdout,
187 stderr=stderr, raise_on_error=raise_on_error,
189 keep_stdin_open=keep_stdin_open, pty=pty)
191 def _run(self, client, cmd, stdin=None, stdout=None, stderr=None,
192 raise_on_error=True, timeout=3600,
193 keep_stdin_open=False, pty=False):
195 transport = client.get_transport()
196 session = transport.open_session()
199 session.exec_command(cmd)
200 start_time = time.time()
205 # If we have data to be sent to stdin then `select' should also
206 # check for stdin availability.
207 if stdin and not stdin.closed:
213 # Block until data can be read/write.
214 r, w, e = select.select([session], writes, [session], 1)
216 if session.recv_ready():
217 data = session.recv(4096)
218 self.log.debug("stdout: %r", data)
219 if stdout is not None:
223 if session.recv_stderr_ready():
224 stderr_data = session.recv_stderr(4096)
225 self.log.debug("stderr: %r", stderr_data)
226 if stderr is not None:
227 stderr.write(stderr_data)
230 if session.send_ready():
231 if stdin is not None and not stdin.closed:
233 data_to_send = stdin.read(4096)
235 # we may need to keep stdin open
236 if not keep_stdin_open:
238 session.shutdown_write()
241 sent_bytes = session.send(data_to_send)
242 # LOG.debug("sent: %s" % data_to_send[:sent_bytes])
243 data_to_send = data_to_send[sent_bytes:]
245 if session.exit_status_ready():
248 if timeout and (time.time() - timeout) > start_time:
249 args = {"cmd": cmd, "host": self.host}
250 raise SSHTimeout("Timeout executing command "
251 "'%(cmd)s' on host %(host)s" % args)
253 raise SSHError("Socket error.")
255 exit_status = session.recv_exit_status()
256 if exit_status != 0 and raise_on_error:
257 fmt = "Command '%(cmd)s' failed with exit_status %(status)d."
258 details = fmt % {"cmd": cmd, "status": exit_status}
260 details += " Last stderr data: '%s'." % stderr_data
261 raise SSHError(details)
264 def execute(self, cmd, stdin=None, timeout=3600):
265 """Execute the specified command on the server.
267 :param cmd: Command to be executed.
268 :param stdin: Open file to be sent on process stdin.
269 :param timeout: Timeout for execution of the command.
271 :returns: tuple (exit_status, stdout, stderr)
273 stdout = six.moves.StringIO()
274 stderr = six.moves.StringIO()
276 exit_status = self.run(cmd, stderr=stderr,
277 stdout=stdout, stdin=stdin,
278 timeout=timeout, raise_on_error=False)
281 return (exit_status, stdout.read(), stderr.read())
283 def wait(self, timeout=120, interval=1):
284 """Wait for the host will be available via ssh."""
285 start_time = time.time()
288 return self.execute("uname")
289 except (socket.error, SSHError) as e:
290 self.log.debug("Ssh is still unavailable: %r", e)
292 if time.time() > (start_time + timeout):
293 raise SSHTimeout("Timeout waiting for '%s'", self.host)
295 def put(self, files, remote_path=b'.', recursive=False):
296 client = self._get_client()
298 with SCPClient(client.get_transport()) as scp:
299 scp.put(files, remote_path, recursive)
301 # keep shell running in the background, e.g. screen
302 def send_command(self, command):
303 client = self._get_client()
304 client.exec_command(command, get_pty=True)
306 def _put_file_sftp(self, localpath, remotepath, mode=None):
307 client = self._get_client()
309 with client.open_sftp() as sftp:
310 sftp.put(localpath, remotepath)
312 mode = 0o777 & os.stat(localpath).st_mode
313 sftp.chmod(remotepath, mode)
315 TILDE_EXPANSIONS_RE = re.compile("(^~[^/]*/)?(.*)")
317 def _put_file_shell(self, localpath, remotepath, mode=None):
318 # quote to stop wordpslit
319 tilde, remotepath = self.TILDE_EXPANSIONS_RE.match(remotepath).groups()
322 cmd = ['cat > %s"%s"' % (tilde, remotepath)]
324 # use -- so no options
325 cmd.append('chmod -- 0%o %s"%s"' % (mode, tilde, remotepath))
327 with open(localpath, "rb") as localfile:
328 # only chmod on successful cat
329 self.run("&& ".join(cmd), stdin=localfile)
331 def put_file(self, localpath, remotepath, mode=None):
332 """Copy specified local file to the server.
334 :param localpath: Local filename.
335 :param remotepath: Remote filename.
336 :param mode: Permissions to set after upload
339 self._put_file_sftp(localpath, remotepath, mode=mode)
340 except (paramiko.SSHException, socket.error):
341 self._put_file_shell(localpath, remotepath, mode=mode)