You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/advanced.rst
+209-6Lines changed: 209 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,13 @@ Agents and Private Keys
12
12
SSH Agent forwarding
13
13
-----------------------
14
14
15
-
SSH agent forwarding, what ``ssh -A`` does on the command line, is supported and enabled by default. Creating an object as ``ParallelSSHClient(hosts, forward_ssh_agent=False)`` will disable this behaviour.
15
+
SSH agent forwarding, what ``ssh -A`` does on the command line, is supported and enabled by default. Creating a client object as:
16
+
17
+
.. code-block:: python
18
+
19
+
ParallelSSHClient(hosts, forward_ssh_agent=False)
20
+
21
+
will disable this behaviour.
16
22
17
23
Programmatic Private Keys
18
24
--------------------------
@@ -32,6 +38,10 @@ Where ``my_key`` is a private key file in current working directory.
32
38
33
39
The helper function :py:func:`load_private_key <pssh.utils.load_private_key>` will attempt to load all available key types and raises :mod:`SSHException <pssh.exceptions.SSHException>` if it cannot load the key file.
@@ -73,6 +83,10 @@ It is also possible to programmatically provide an SSH agent for the client to u
73
83
74
84
Supplying an agent programmatically implies that a system SSH agent will *not* be used even if available.
75
85
86
+
.. seealso::
87
+
88
+
:py:class:`pssh.agent.SSHAgent`
89
+
76
90
77
91
Tunneling
78
92
**********
@@ -106,7 +120,7 @@ In the above example, connection to the target host is made via ``my_proxy_user@
106
120
107
121
.. note::
108
122
109
-
Proxy host connections are asynchronous and use the SSH protocol's native TCP tunneling - aka local port forward. No external commands are used for the proxy connection, unlike the `ProxyCommand` directive in OpenSSH and other utilities.
123
+
Proxy host connections are asynchronous and use the SSH protocol's native TCP tunneling - aka local port forward. No external commands or processes are used for the proxy connection, unlike the `ProxyCommand` directive in OpenSSH and other utilities.
110
124
111
125
While the connections initiated by ``ParallelSSH`` are asynchronous, the connections from proxy host -> target hosts may not be, depending on SSH server implementation. If only one proxy host is used to connect to a large number of target hosts and proxy SSH server connections are *not* asynchronous, this may adversely impact performance on the proxy host.
112
126
@@ -180,6 +194,193 @@ In the following example, first host in host list will use cmd ``host-index-0``,
See :py:func:`run_command API documentation <pssh.pssh_client.ParallelSSHClient.run_command>`for a complete list of features and options.
201
+
202
+
.. note::
203
+
204
+
With a PTY, stdout and stderr output is combined into stdout.
205
+
206
+
Without a PTY, separate output is given for stdout and stderr, although some programs and server configurations require a PTY.
207
+
208
+
Run with sudo
209
+
---------------
210
+
211
+
``ParallelSSH`` can be instructed to run its commands under ``sudo``:
212
+
213
+
.. code-block:: python
214
+
215
+
client = <..>
216
+
217
+
output = client.run_command(<..>, sudo=True)
218
+
client.join(output)
219
+
220
+
While not best practice and password-less `sudo`is best configured for a limited set of commands, a sudo password may be provided via the stdin channel:
221
+
222
+
.. code-block:: python
223
+
224
+
client = <..>
225
+
226
+
output = client.run_command(<..>, sudo=True)
227
+
for host in output:
228
+
stdin = output[host].stdin
229
+
stdin.write('my_password\n')
230
+
stdin.flush()
231
+
client.join(output)
232
+
233
+
Output encoding
234
+
-----------------
235
+
236
+
By default, output is encoded as``UTF-8``. This can be configured with the ``encoding`` keyword argument.
237
+
238
+
.. code-block:: python
239
+
240
+
client = <..>
241
+
242
+
client.run_command(<..>, encoding='utf-16')
243
+
stdout = list(output[client.hosts[0]].stdout)
244
+
245
+
Contents of ``stdout`` will be `UTF-16` encoded.
246
+
247
+
.. note::
248
+
249
+
Encoding must be valid `Python codec <https://docs.python.org/2.7/library/codecs.html>`_
250
+
251
+
Disabling use of pseudo terminal emulation
252
+
--------------------------------------------
253
+
254
+
By default, ``ParallelSSH`` uses the user's configured shell to run commands with. As a shell is used by default, a pseudo terminal (`PTY`) is also requested by default.
255
+
256
+
For cases where use of a `PTY`isnot wanted, such as having separate stdout and stderr outputs, or the remote command is a daemon that needs to fork and detach itself or when use of a shell is explicitly disabled, use of PTY can also be disabled.
257
+
258
+
The following example prints to stderr withPTY disabled.
SFTP-`SCP version 2`-is supported by ``Parallel-SSH``and two functions are provided by the client for copying files withSFTP.
312
+
313
+
SFTP does not have a shell interface and no output is provided foranySFTP commands.
314
+
315
+
As such, SFTP functions in``ParallelSSHClient``return greenlets that will need to be joined to raiseany exceptions from them. :py:func:`gevent.joinall` may be used for that.
316
+
317
+
318
+
Copying files to remote hosts in parallel
319
+
----------------------------------------------
320
+
321
+
To copy the local filewith relative path ``../test`` to the remote relative path ``test_dir/test``- remote directory will be created if it does not exist, permissions allowing. ``raise_error=True`` instructs ``joinall`` to raiseany exceptions thrown by the greenlets.
:py:func:`copy_file <pssh.pssh_client.ParallelSSHClient.copy_file>`API documentation and exceptions raised.
343
+
344
+
:py:func:`gevent.joinall` Gevent's ``joinall`` API documentation.
345
+
346
+
Copying files from remote hosts in parallel
347
+
----------------------------------------------
348
+
349
+
Copying remote files in parallel requires that file names are de-duplicated otherwise they will overwrite each other. ``copy_remote_file`` names local files as``<local_file><suffix_separator><host>``, suffixing each filewith the host name it came from, separated by a configurable character or string.
The above will create files ``local.file_host1`` where ``host1``is the host name the file was copied from.
362
+
363
+
.. seealso::
364
+
365
+
:py:func:`copy_remote_file <pssh.pssh_client.ParallelSSHClient.copy_remote_file>`API documentation and exceptions raised.
366
+
367
+
Single host copy
368
+
-----------------
369
+
370
+
If wanting to copy a filefrom a single remote host and retain the original filename, can use the single host :py:class:`SSHClient <pssh.ssh_client.SSHClient>`and its :py:func:`copy_file <pssh.ssh_client.SSHClient.copy_remote_file>` directly.
:py:func:`SSHClient.copy_remote_file <pssh.ssh_client.SSHClient.copy_remote_file>`API documentation and exceptions raised.
382
+
383
+
183
384
Hosts filtering and overriding
184
385
*******************************
185
386
@@ -209,7 +410,7 @@ Any type of iterator may be used as hosts list, including generator and list com
209
410
210
411
.. note ::
211
412
212
-
Since generators by design only iterate over a sequence once then stop, ``client.hosts`` should be re-assigned after each call to ``run_command`` when using generators as target of `client.hosts`.
413
+
Since generators by design only iterate over a sequence once then stop, ``client.hosts`` should be re-assigned after each call to ``run_command`` when using generators as target of ``client.hosts``.
213
414
214
415
Overriding hosts list
215
416
----------------------
@@ -218,6 +419,8 @@ Hosts list can be modified in place. A call to ``run_command`` will create new c
``Parallel-SSH`` is a parallel SSH client library. It uses asynchronous SSH connections and is, to date, the only publicly available asynchronous SSH client library for Python, as well as the only asynchronous *parallel* SSH client library available for Python.
0 commit comments