|
20 | 20 | and ParallelSSHClient. |
21 | 21 | """ |
22 | 22 |
|
23 | | -from pssh import SSHClient, ParallelSSHClient |
| 23 | +from pssh import SSHClient, ParallelSSHClient, utils |
24 | 24 | import logging |
| 25 | +from pprint import pprint |
25 | 26 |
|
26 | | -logger = logging.getLogger('pssh') |
27 | | - |
28 | | -def _setup_logger(_logger): |
29 | | - """Setup default logger""" |
30 | | - _handler = logging.StreamHandler() |
31 | | - log_format = logging.Formatter( |
32 | | - '%(name)s - %(asctime)s - %(levelname)s - %(message)s') |
33 | | - _handler.setFormatter(log_format) |
34 | | - _logger.addHandler(_handler) |
35 | | - _logger.setLevel(logging.DEBUG) |
| 27 | +utils.enable_host_logger() |
| 28 | +utils.enable_logger(utils.logger) |
36 | 29 |
|
37 | 30 | def test(): |
38 | 31 | """Perform ls and copy file with SSHClient on localhost""" |
39 | 32 | client = SSHClient('localhost') |
40 | 33 | channel, host, stdout, stderr = client.exec_command('ls -ltrh') |
41 | 34 | for line in stdout: |
42 | | - print line.strip() |
| 35 | + pprint(line.strip()) |
43 | 36 | client.copy_file('../test', 'test_dir/test') |
44 | 37 |
|
45 | 38 | def test_parallel(): |
46 | | - """Perform ls and copy file with ParallelSSHClient on localhost""" |
| 39 | + """Perform ls and copy file with ParallelSSHClient on localhost. |
| 40 | + |
| 41 | + Two identical hosts cause the same command to be executed |
| 42 | + twice on the same host in two parallel connections. |
| 43 | + In printed output there will be two identical lines per printed per |
| 44 | + line of `ls -ltrh` output as output is printed by host_logger as it |
| 45 | + becomes available and commands are executed in parallel |
| 46 | + |
| 47 | + Host output key is de-duplicated so that output for the two |
| 48 | + commands run on the same host(s) is not lost |
| 49 | + """ |
47 | 50 | client = ParallelSSHClient(['localhost', 'localhost']) |
48 | 51 | output = client.run_command('ls -ltrh') |
49 | | - print output |
50 | | - # cmds = client.copy_file('../test', 'test_dir/test') |
51 | | - # client.pool.join() |
| 52 | + client.join(output) |
| 53 | + pprint(output) |
| 54 | + cmds = client.copy_file('../test', 'test_dir/test') |
| 55 | + client.pool.join() |
52 | 56 |
|
53 | 57 | if __name__ == "__main__": |
54 | | - _setup_logger(logger) |
55 | | - # test() |
| 58 | + test() |
56 | 59 | test_parallel() |
0 commit comments