-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathssh.py
More file actions
340 lines (301 loc) · 9.98 KB
/
Copy pathssh.py
File metadata and controls
340 lines (301 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/lib/blob/main/CONTRIBUTING.md
"""Run commands and copy files over SSH.
Builds and executes `ssh` and `scp` command lines from individual options, so
consumers do not have to assemble them by hand. Commands are built as argument
lists (argv) and run without a local shell, so option and target values are never
subject to local shell interpretation. All functions return the same
`(success, result)` shape as `lib.shell.shell_exec()`.
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026091801'
from . import shell
def _check_target(host, username):
"""Reject a host or username that ssh could read as an option (leading `-`),
for example `-oProxyCommand=...`. Returns `(True, None)` when both are safe,
else `(False, error_message)`."""
for label, value in (('host', host), ('username', username)):
ok, msg = shell.safe_cli_value(value, f'ssh {label}')
if not ok:
return False, msg
return True, None
def _with_password(cmd, password):
"""Prefix a command with `sshpass` and return the `(cmd, env)` pair to run it with.
The password travels in the `SSHPASS` environment variable rather than in
`sshpass -p`, which sshpass itself labels "security unwise": an argv is world-
readable through `/proc/<pid>/cmdline` for the lifetime of the process. An
environment is only readable by the owner of the process and by root.
Parameters
----------
cmd : list
The command to run, as an argument list.
password : str or None
The SSH password. Falsy means no `sshpass` wrapper.
Returns
-------
tuple
`(cmd, env)`, where `env` is `None` unless a password is given.
"""
if not password:
return cmd, None
return ['sshpass', '-e', *cmd], {'SSHPASS': password}
def build_options(
configfile=None,
identity=None,
ssh_option=None,
ipv4=False,
ipv6=False,
quiet=False,
batch_mode=False,
connect_timeout=None,
log_level=None,
):
"""
Assemble the option tokens shared by `ssh` and `scp`.
The result intentionally omits the port, because `ssh` uses `-p` while `scp`
uses `-P`; `run()` and `scp()` add the port themselves.
Parameters
----------
configfile : str, optional
Path passed as `-F`.
identity : list, optional
Identity files, each passed as `-i`.
ssh_option : list, optional
Raw options, each passed as `-o`.
ipv4 : bool, optional
Force IPv4 (`-4`).
ipv6 : bool, optional
Force IPv6 (`-6`).
quiet : bool, optional
Quiet mode (`-q`).
batch_mode : bool, optional
Add `-o BatchMode=yes` to never prompt
for a password or passphrase (non-interactive runs).
connect_timeout : int, optional
Seconds for `-o ConnectTimeout`.
log_level : str, optional
Value for `-o LogLevel` (e.g. `ERROR` to
suppress the "Permanently added to known_hosts" warning).
Returns
-------
list
The assembled option tokens (may be empty).
Examples
--------
>>> build_options(identity=['~/.ssh/id_ed25519'], batch_mode=True)
['-i', '~/.ssh/id_ed25519', '-o', 'BatchMode=yes']
"""
parts = []
if ipv4:
parts.append('-4')
if ipv6:
parts.append('-6')
if configfile:
parts += ['-F', configfile]
for item in identity or []:
parts += ['-i', item]
for item in ssh_option or []:
parts += ['-o', item]
if quiet:
parts.append('-q')
if batch_mode:
parts += ['-o', 'BatchMode=yes']
if log_level:
parts += ['-o', f'LogLevel={log_level}']
if connect_timeout is not None:
parts += ['-o', f'ConnectTimeout={connect_timeout}']
return parts
def target(host, username=None):
"""
Build the `user@host` (or bare `host`) token.
Leaving `username` empty lets `ssh`/`scp` determine the user from
`~/.ssh/config` (or fall back to the current local user), so host aliases
keep working.
Parameters
----------
host : str
Hostname, IP address or `~/.ssh/config` alias.
username : str, optional
Login user. If falsy, omitted.
Returns
-------
str
e.g. `root@host` or `host`.
"""
if username:
return f'{username}@{host}'
return host
def run(
host,
command,
username=None,
port=None,
options=None,
disable_pseudo_terminal=False,
password=None,
timeout=None,
):
"""
Run `command` on `host` over SSH.
The local command line is built as an argv list and run without a local
shell, so `host`, `username`, `port` and `options` are passed verbatim. The
remote `command` is sent to the host as a single argument; the remote shell
expands `~`, `$VAR`, `&&`, pipes etc.
Parameters
----------
host : str
Target host (name, IP or alias).
command : str
Command to run on the remote host.
username : str, optional
Login user (see `target()`).
port : int or str, optional
Remote port (`-p`).
options : list, optional
Option tokens from `build_options()`.
disable_pseudo_terminal : bool, optional
Add `-T`.
password : str, optional
If set, run the command through `sshpass`
(requires `sshpass`). The password is handed over in the environment, so it
does not show up in the process list.
timeout : int, optional
Overall timeout in seconds.
Returns
-------
tuple
`(True, (stdout, stderr, retc))` on success, else
`(False, error_message)`. Same contract as `lib.shell.shell_exec()`.
"""
ok, msg = _check_target(host, username)
if not ok:
return False, msg
cmd = ['ssh', *(options or [])]
if port:
cmd += ['-p', str(port)]
if disable_pseudo_terminal:
cmd.append('-T')
cmd += [target(host, username), command]
cmd, env = _with_password(cmd, password)
return shell.shell_exec(cmd, env=env, timeout=timeout)
def scp(
host,
local,
remote,
username=None,
port=None,
options=None,
password=None,
timeout=None,
recursive=False,
):
"""
Copy the local path `local` to `remote` on `host` via scp.
scp shares ssh's options except that the port flag is `-P`, not `-p`.
Parameters
----------
host : str
Target host (name, IP or alias).
local : str
Local source path (a directory if `recursive=True`).
remote : str
Remote destination path (relative paths are resolved
against the login home).
username : str, optional
Login user (see `target()`).
port : int or str, optional
Remote port (`-P`).
options : list, optional
Option tokens from `build_options()`.
password : str, optional
If set, run the command through `sshpass`
(requires `sshpass`). The password is handed over in the environment, so it
does not show up in the process list.
timeout : int, optional
Overall timeout in seconds.
recursive : bool, optional
Copy a directory tree (`-r`), preserving
modes (`-p`). Useful when the target lacks `tar`. Defaults to `False`.
Returns
-------
tuple
`(True, (stdout, stderr, retc))` on success, else
`(False, error_message)`.
"""
ok, msg = _check_target(host, username)
if not ok:
return False, msg
cmd = ['scp']
if recursive:
cmd += ['-r', '-p']
cmd += options or []
if port:
cmd += ['-P', str(port)]
cmd += [local, f'{target(host, username)}:{remote}']
cmd, env = _with_password(cmd, password)
return shell.shell_exec(cmd, env=env, timeout=timeout)
def rsync(
host,
local,
remote,
username=None,
port=None,
options=None,
password=None,
timeout=None,
sudo=False,
):
"""
Copy a directory tree to `remote` on `host` with rsync over SSH.
rsync is faster than `scp -r` for trees with many files, but requires rsync
to be installed on both ends. The contents of `local` are mirrored into
`remote` (both are treated as directories). Callers that cannot guarantee
rsync on the target should fall back to `scp(..., recursive=True)`.
Parameters
----------
host : str
Target host (name, IP or alias).
local : str
Local source directory.
remote : str
Remote destination directory.
username : str, optional
Login user (see `target()`).
port : int or str, optional
Remote port.
options : list, optional
ssh option tokens from `build_options()`,
passed through to rsync via `--rsh`.
password : str, optional
If set, run the command through `sshpass`
(requires `sshpass`). The password is handed over in the environment, so it
does not show up in the process list.
timeout : int, optional
Overall timeout in seconds.
sudo : bool, optional
Run the remote rsync via `sudo`
(`--rsync-path="sudo rsync"`), so files land root-owned and writes are
privileged. Requires password-less sudo on the target. Defaults to `False`.
Returns
-------
tuple
`(True, (stdout, stderr, retc))` on success, else
`(False, error_message)`.
"""
ok, msg = _check_target(host, username)
if not ok:
return False, msg
# rsync's --rsh takes the remote-shell command as a single string.
rsh = ' '.join(['ssh', *(options or [])]) + (f' -p {port}' if port else '')
cmd = ['rsync', '--archive']
if sudo:
cmd += ['--rsync-path', 'sudo rsync']
cmd += ['--rsh', rsh, f'{local}/', f'{target(host, username)}:{remote}/']
cmd, env = _with_password(cmd, password)
return shell.shell_exec(cmd, env=env, timeout=timeout)