-
Notifications
You must be signed in to change notification settings - Fork 159
/
ssh.py
510 lines (428 loc) · 17.4 KB
/
ssh.py
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# pylint: disable=no-member
import tempfile
import logging
import subprocess
import os
from select import select
from functools import wraps
import attr
from ..driver.exception import ExecutionError
from .helper import get_free_port
__all__ = ['sshmanager', 'SSHConnection', 'ForwardError']
@attr.s
class SSHConnectionManager:
"""The SSHConnectionManager manages multiple SSH connections. This class
should not be directly instantiated, use the exported sshmanager from this
module instead.
"""
_connections = attr.ib(
default=attr.Factory(dict),
init=False,
validator=attr.validators.optional(attr.validators.instance_of(dict))
)
def __attrs_post_init__(self):
self.logger = logging.getLogger("{}".format(self))
def get(self, host: str):
"""Retrieve or create a new connection to a given host
Arguments:
host (str): host to retrieve the connection for
Returns:
:obj:`SSHConnection`: the SSHConnection for the host"""
instance = self._connections.get(host)
if instance is None:
# pylint: disable=unsupported-assignment-operation
self.logger.debug("Creating SSHConnection for %s", host)
instance = SSHConnection(host)
instance.connect()
self._connections[host] = instance
return instance
def add_connection(self, connection):
"""Add an existing SSHConnection to the manager.
This is useful for manually created connections which should also be
available via the manager.
Arguments:
connection (:obj:`SSHConnection`): SSHconnection to add to the manager
"""
# pylint: disable=unsupported-assignment-operation
assert isinstance(connection, SSHConnection)
if connection.host not in self._connections:
self._connections[connection.host] = connection
def remove_connection(self, connection):
# pylint: disable=unsupported-assignment-operation
assert isinstance(connection, SSHConnection)
if connection.isconnected():
raise ExecutionError("Can't remove connected connection")
del self._connections[connection.host]
def remove_by_name(self, name):
# pylint: disable=unsupported-assignment-operation
del self._connections[name]
def open(self, host):
return self.get(host)
def close(self, host):
con = self.get(host)
con.disconnect()
self.remove_connection(con)
def request_forward(self, host, dest, port):
con = self.get(host)
return con.add_port_forward(dest, port)
def remove_forward(self, host, dest, port):
con = self.get(host)
con.remove_port_forward(dest, port)
def put_file(self, host, local_file, remote_file):
con = self.get(host)
con.put_file(local_file, remote_file)
def get_file(self, host, remote_file, local_file):
con = self.get(host)
con.get_file(remote_file, local_file)
def close_all(self):
"""Close all open connections and remove them from the manager """
for name, connection in self._connections.items():
if connection.isconnected():
connection.disconnect()
names = self._connections.copy().keys()
for name in names:
self.remove_by_name(name)
@attr.s
class SSHConnection:
"""SSHConnections are individual connections to hosts managed by a control
socket. In addition to command execution this class also provides an
interface to manage port forwardings. These are used in the remote
infrastructure to tunnel multiple connections over one SSH link.
A public identity infrastructure is assumed, no extra username or passwords
are supported."""
host = attr.ib(validator=attr.validators.instance_of(str))
_connected = attr.ib(
default=False, init=False, validator=attr.validators.instance_of(bool)
)
_tmpdir = attr.ib(
default=attr.Factory(lambda: tempfile.mkdtemp(prefix="labgrid-connection-")),
init=False,
validator=attr.validators.instance_of(str)
)
_forwards = attr.ib(init=False, default=attr.Factory(dict))
def __attrs_post_init__(self):
self._logger = logging.getLogger("{}".format(self))
self._socket = None
self._master = None
self._keepalive = None
@staticmethod
def _get_ssh_base_args():
return ["-x", "-o", "LogLevel=ERROR", "-o", "PasswordAuthentication=no"]
def _get_ssh_control_args(self):
if self._socket:
return [
"-o", "ControlMaster=no",
"-o", "ControlPath={}".format(self._socket),
]
return []
def _get_ssh_args(self):
args = SSHConnection._get_ssh_base_args()
args += self._get_ssh_control_args()
return args
def _open_connection(self):
"""Internal function which appends the control socket and checks if the
connection is already open"""
if self._check_external_master():
self._logger.info("Using existing SSH connection to %s", self.host)
else:
self._start_own_master()
self._logger.info("Created new SSH connection to %s", self.host)
self._start_keepalive()
self._connected = True
def _run_socket_command(self, command, forward=None):
"Internal function to send a command to the control socket"
complete_cmd = ["ssh"] + self._get_ssh_args()
complete_cmd += ["-O", command]
if forward:
for item in forward:
complete_cmd.append(item)
complete_cmd.append(self.host)
res = subprocess.check_call(
complete_cmd,
stdin=subprocess.DEVNULL,
timeout=2,
)
return res
def _check_connected(func): # pylint: disable=no-self-argument
"""Check if an SSHConnection is connected as a decorator"""
@wraps(func)
def wrapper(self, *_args, **_kwargs):
if not self.isconnected():
raise ExecutionError(
"{} can not be called ({} is not connected)".format(
func.__qualname__, self
)
)
return func(self, *_args, **_kwargs) # pylint: disable=not-callable
return wrapper
@_check_connected
def get_prefix(self):
return ["ssh"] + self._get_ssh_args() + [self.host]
@_check_connected
def run(self, command, *, codec="utf-8", decodeerrors="strict",
force_tty=False, stderr_merge=False, stderr_loglevel=None,
stdout_loglevel=None):
"""Run a command over the SSHConnection
Args:
command (string): The command to run
codec (string, optional): output encoding. Defaults to "utf-8".
decodeerrors (string, optional): behavior on decode errors. Defaults
to "strict". Refer to stdtypes' bytes.decode for details.
force_tty (bool, optional): force allocate a tty (ssh -tt). Defaults
to False
stderr_merge (bool, optional): merge ssh subprocess stderr into
stdout. Defaults to False.
stdout_loglevel (int, optional): log stdout with specific log level
as well. Defaults to None, i.e. don't log.
stderr_loglevel (int, optional): log stderr with specific log level
as well. Defaults to None, i.e. don't log.
returns:
(stdout, stderr, returncode)
"""
complete_cmd = ["ssh"] + self._get_ssh_args()
if force_tty:
complete_cmd += ["-tt"]
complete_cmd += [self.host, command]
self._logger.debug("Sending command: %s", complete_cmd)
if stderr_merge:
stderr_pipe = subprocess.STDOUT
else:
stderr_pipe = subprocess.PIPE
try:
sub = subprocess.Popen(
complete_cmd, stdout=subprocess.PIPE, stderr=stderr_pipe,
stdin=subprocess.DEVNULL
)
except:
raise ExecutionError(
"error executing command: {}".format(complete_cmd)
)
stdout = []
stderr = []
readable = {
sub.stdout.fileno(): (sub.stdout, stdout, stdout_loglevel),
}
if sub.stderr is not None:
readable[sub.stderr.fileno()] = (sub.stderr, stderr, stderr_loglevel)
while readable:
for fd in select(readable, [], [])[0]:
stream, output, loglevel = readable[fd]
line = stream.readline().decode(codec, decodeerrors)
if line == "": # EOF
del readable[fd]
else:
line = line.rstrip('\n')
output.append(line)
if loglevel is not None:
self._logger.log(loglevel, line)
return stdout, stderr, sub.wait()
def run_check(self, command, *, codec="utf-8", decodeerrors="strict",
force_tty=False, stderr_merge=False, stderr_loglevel=None,
stdout_loglevel=None):
"""
Runs a command over the SSHConnection
returns the output if successful, raises ExecutionError otherwise.
Except for the means of returning the value, this is equivalent to
run.
Args:
command (string): The command to run
codec (string, optional): output encoding. Defaults to "utf-8".
decodeerrors (string, optional): behavior on decode errors. Defaults
to "strict". Refer to stdtypes' bytes.decode for details.
force_tty (bool, optional): force allocate a tty (ssh -tt). Defaults
to False
stderr_merge (bool, optional): merge ssh subprocess stderr into
stdout. Defaults to False.
stdout_loglevel (int, optional): log stdout with specific log level
as well. Defaults to None, i.e. don't log.
stderr_loglevel (int, optional): log stderr with specific log level
as well. Defaults to None, i.e. don't log.
Returns:
List[str]: stdout of the executed command if successful and
otherwise an ExecutionError Exception
"""
stdout, stderr, exitcode = self.run(
command, codec=codec, decodeerrors=decodeerrors, force_tty=force_tty,
stderr_merge=stderr_merge, stdout_loglevel=stdout_loglevel,
stderr_loglevel=stderr_loglevel
)
if exitcode != 0:
raise ExecutionError(command, stdout, stderr)
return stdout
@_check_connected
def get_file(self, remote_file, local_file):
"""Get a file from the remote host"""
complete_cmd = ["scp"] + self._get_ssh_control_args()
complete_cmd += [
"{}:{}".format(self.host, remote_file),
"{}".format(local_file)
]
self._logger.debug("Running command: %s", complete_cmd)
subprocess.check_call(
complete_cmd,
stdin=subprocess.DEVNULL,
)
@_check_connected
def put_file(self, local_file, remote_path):
"""Put a file onto the remote host"""
complete_cmd = ["rsync", "--compress=1", "--sparse", "--copy-links", "--verbose", "--progress", "--times", "-e",
" ".join(['ssh'] + self._get_ssh_args())]
complete_cmd += [
"{}".format(local_file),
"{}:{}".format(self.host, remote_path)
]
self._logger.debug("Running command: %s", complete_cmd)
subprocess.check_call(
complete_cmd,
stdin=subprocess.DEVNULL,
)
@_check_connected
def add_port_forward(self, remote_host, remote_port):
"""forward command"""
local_port = get_free_port()
destination = "{}:{}".format(remote_host, remote_port)
# pylint: disable=not-an-iterable
if destination in self._forwards:
return self._forwards[destination]
self._run_socket_command(
"forward", [
"-L"
"{local}:{destination}".
format(local=local_port, destination=destination)
]
)
self._forwards[destination] = local_port
return local_port
@_check_connected
def remove_port_forward(self, remote_host, remote_port):
"""cancel command"""
destination = "{}:{}".format(remote_host, remote_port)
local_port = self._forwards.pop(destination, None)
# pylint: disable=not-an-iterable
if local_port is None:
raise ForwardError("Forward does not exist")
self._run_socket_command(
"cancel", [
"-L"
"{local}:localhost:{remote}".
format(local=local_port, remote=remote_port)
]
)
def connect(self):
if not self._connected:
self._open_connection()
@_check_connected
def disconnect(self):
self._disconnect()
def isconnected(self):
return self._connected and self._check_keepalive()
def _check_external_master(self):
args = ["ssh", "-O", "check", "{}".format(self.host)]
# We don't want to confuse the use with SSHs output here, so we need to
# capture and parse it.
proc = subprocess.Popen(
args,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
stdout, _ = proc.communicate(timeout=60)
check = proc.wait()
if check == 0:
self._logger.debug("Found existing control socket")
return True
if b"No such file or directory" in stdout:
self._logger.debug("No existing control socket found")
return False
self._logger.debug("Unexpected ssh check output '%s'", stdout)
return False
def _start_own_master(self):
"""Starts a controlmaster connection in a temporary directory."""
control = os.path.join(self._tmpdir, 'control-{}'.format(self.host))
args = ["ssh"] + SSHConnection._get_ssh_base_args()
args += [
"-n", "-MN",
"-o", "ConnectTimeout=30",
"-o", "ControlPersist=300",
"-o", "ControlMaster=yes",
"-o", "ControlPath={}".format(control),
# We don't want to ask the user to confirm host keys here.
"-o", "StrictHostKeyChecking=yes",
self.host,
]
assert self._master is None
self._master = subprocess.Popen(
args,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
try:
if self._master.wait(timeout=30) != 0:
raise ExecutionError(
"failed to connect to {} with args {}, returncode={} {},{} ".format(
self.host, args, self._master.wait(),
self._master.stdout.readlines(),
self._master.stderr.readlines()
)
)
except subprocess.TimeoutExpired:
raise ExecutionError(
"failed to connect (timeout) to {} with args {} {},{}".format(
self.host, args, self._master.stdout.readlines(),
self._master.stderr.readlines()
)
)
if not os.path.exists(control):
raise ExecutionError("no control socket to {}".format(self.host))
self._socket = control
self._logger.debug('Connected to %s', self.host)
def _stop_own_master(self):
assert self._socket is not None
assert self._master is not None
try:
self._run_socket_command("cancel")
self._run_socket_command("exit")
# if the master doesn't terminate in less than 60 seconds,
# something is very wrong
self._master.wait(timeout=60)
finally:
self._socket = None
self._master = None
def _start_keepalive(self):
"""Starts a keepalive connection via the own or external master."""
args = ["ssh"] + self._get_ssh_args() + [self.host, "cat"]
assert self._keepalive is None
self._keepalive = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self._logger.debug('Started keepalive for %s', self.host)
def _check_keepalive(self):
return self._keepalive.poll() is None
def _stop_keepalive(self):
assert self._keepalive is not None
self._logger.debug('Stopping keepalive for %s', self.host)
try:
self._keepalive.communicate(timeout=60)
except subprocess.TimeoutExpired:
self._keepalive.kill()
try:
self._keepalive.wait(timeout=60)
finally:
self._keepalive = None
def _disconnect(self):
assert self._connected
try:
if self._socket:
self._logger.info("Closing SSH connection to %s", self.host)
self._stop_keepalive()
self._stop_own_master()
finally:
self._connected = False
sshmanager = SSHConnectionManager()
@attr.s
class ForwardError(Exception):
msg = attr.ib(validator=attr.validators.instance_of(str))