|
1
|
|
|
#!/usr/bin/env python3 |
|
|
|
|
|
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
import sys |
|
5
|
|
|
import paramiko |
|
6
|
|
|
from db_sync_tool.utility import mode, system, helper, output |
|
7
|
|
|
from db_sync_tool.remote import utility |
|
8
|
|
|
|
|
9
|
|
|
ssh_client_origin = None |
|
|
|
|
|
|
10
|
|
|
ssh_client_target = None |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def load_ssh_client_origin(): |
|
14
|
|
|
""" |
|
15
|
|
|
Loading the origin ssh client |
|
16
|
|
|
:return: |
|
17
|
|
|
""" |
|
18
|
|
|
global ssh_client_origin |
|
|
|
|
|
|
19
|
|
|
ssh_client_origin = load_ssh_client(mode.Client.ORIGIN) |
|
20
|
|
|
utility.run_before_script(mode.Client.ORIGIN) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def load_ssh_client_target(): |
|
24
|
|
|
""" |
|
25
|
|
|
Loading the target ssh client |
|
26
|
|
|
:return: |
|
27
|
|
|
""" |
|
28
|
|
|
global ssh_client_target |
|
|
|
|
|
|
29
|
|
|
ssh_client_target = load_ssh_client(mode.Client.TARGET) |
|
30
|
|
|
utility.run_before_script(mode.Client.TARGET) |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def load_ssh_client(ssh): |
|
34
|
|
|
""" |
|
35
|
|
|
Initializing the given ssh client |
|
36
|
|
|
:param ssh: String |
|
37
|
|
|
:return: |
|
38
|
|
|
""" |
|
39
|
|
|
_host_name = helper.get_ssh_host_name(ssh, True) |
|
40
|
|
|
_ssh_client = paramiko.SSHClient() |
|
41
|
|
|
_ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
|
42
|
|
|
|
|
43
|
|
|
_ssh_port = _ssh_port = system.config[ssh]['port'] if 'port' in system.config[ssh] else 22 |
|
44
|
|
|
_ssh_key = None |
|
45
|
|
|
_ssh_password = None |
|
46
|
|
|
|
|
47
|
|
|
# Check authentication |
|
48
|
|
|
if 'ssh_key' in system.config[ssh]: |
|
49
|
|
|
_authentication_method = f'{output.CliFormat.BLACK} - (authentication: key){output.CliFormat.ENDC}' |
|
|
|
|
|
|
50
|
|
|
_ssh_key = system.config[ssh]['ssh_key'] |
|
51
|
|
|
elif 'password' in system.config[ssh]: |
|
52
|
|
|
_authentication_method = f'{output.CliFormat.BLACK} - (authentication: password){output.CliFormat.ENDC}' |
|
|
|
|
|
|
53
|
|
|
_ssh_password = system.config[ssh]['password'] |
|
54
|
|
|
else: |
|
55
|
|
|
sys.exit( |
|
56
|
|
|
output.message( |
|
57
|
|
|
output.Subject.ERROR, |
|
58
|
|
|
f'Missing SSH authentication. Neither ssh key nor ssh password given.', |
|
|
|
|
|
|
59
|
|
|
False |
|
60
|
|
|
) |
|
61
|
|
|
) |
|
62
|
|
|
|
|
63
|
|
|
# Try to connect to remote client via paramiko |
|
64
|
|
|
try: |
|
65
|
|
|
_ssh_client.connect(hostname=system.config[ssh]['host'], |
|
66
|
|
|
username=system.config[ssh]['user'], |
|
67
|
|
|
key_filename=_ssh_key, |
|
68
|
|
|
password=_ssh_password, |
|
69
|
|
|
port=_ssh_port, |
|
70
|
|
|
compress=True) |
|
71
|
|
|
|
|
72
|
|
|
except paramiko.ssh_exception.AuthenticationException: |
|
73
|
|
|
sys.exit( |
|
74
|
|
|
output.message( |
|
75
|
|
|
output.Subject.ERROR, |
|
76
|
|
|
f'SSH authentication for {_host_name} failed', |
|
77
|
|
|
False |
|
78
|
|
|
) |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
output.message( |
|
82
|
|
|
output.host_to_subject(ssh), |
|
83
|
|
|
f'Initialize remote SSH connection {_host_name}{_authentication_method}', |
|
84
|
|
|
True |
|
85
|
|
|
) |
|
86
|
|
|
|
|
87
|
|
|
return _ssh_client |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
def close_ssh_clients(): |
|
91
|
|
|
""" |
|
92
|
|
|
Closing ssh client sessions |
|
93
|
|
|
:return: |
|
94
|
|
|
""" |
|
95
|
|
|
utility.run_after_script(mode.Client.ORIGIN) |
|
96
|
|
|
if not ssh_client_origin is None: |
|
97
|
|
|
ssh_client_origin.close() |
|
98
|
|
|
|
|
99
|
|
|
utility.run_after_script(mode.Client.TARGET) |
|
100
|
|
|
if not ssh_client_target is None: |
|
101
|
|
|
ssh_client_target.close() |
|
|
|
|
|