Conditions | 6 |
Total Lines | 67 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | #!/usr/bin/env python3 |
||
39 | def load_ssh_client(ssh): |
||
40 | """ |
||
41 | Initializing the given ssh client |
||
42 | :param ssh: String |
||
43 | :return: |
||
44 | """ |
||
45 | _host_name = helper.get_ssh_host_name(ssh, True) |
||
46 | _ssh_client = paramiko.SSHClient() |
||
47 | _ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
||
48 | |||
49 | _ssh_port = system.config[ssh]['port'] if 'port' in system.config[ssh] else 22 |
||
50 | _ssh_key = None |
||
51 | _ssh_password = None |
||
52 | |||
53 | # Check authentication |
||
54 | if 'ssh_key' in system.config[ssh]: |
||
55 | _authentication_method = f'{output.CliFormat.BLACK} - ' \ |
||
56 | f'(authentication: key){output.CliFormat.ENDC}' |
||
57 | _ssh_key = system.config[ssh]['ssh_key'] |
||
58 | elif 'password' in system.config[ssh]: |
||
59 | _authentication_method = f'{output.CliFormat.BLACK} - ' \ |
||
60 | f'authentication: password){output.CliFormat.ENDC}' |
||
61 | _ssh_password = system.config[ssh]['password'] |
||
62 | elif 'ssh_agent' in system.config: |
||
63 | _authentication_method = f'{output.CliFormat.BLACK} - ' \ |
||
64 | f'(authentication: key){output.CliFormat.ENDC}' |
||
65 | else: |
||
66 | sys.exit( |
||
67 | output.message( |
||
68 | output.Subject.ERROR, |
||
69 | 'Missing SSH authentication. Neither ssh key nor ssh password given.', |
||
70 | False |
||
71 | ) |
||
72 | ) |
||
73 | |||
74 | # Try to connect to remote client via paramiko |
||
75 | try: |
||
76 | _ssh_client.connect(hostname=system.config[ssh]['host'], |
||
77 | username=system.config[ssh]['user'], |
||
78 | key_filename=_ssh_key, |
||
79 | password=_ssh_password, |
||
80 | port=_ssh_port, |
||
81 | compress=True, |
||
82 | timeout=default_timeout, |
||
83 | sock=get_jump_host_channel(ssh)) |
||
84 | # |
||
85 | # Workaround for long-lasting requests |
||
86 | # https://stackoverflow.com/questions/50009688/python-paramiko-ssh-session-not-active-after-being-idle-for-many-hours |
||
87 | # |
||
88 | _ssh_client.get_transport().set_keepalive(60) |
||
89 | |||
90 | except paramiko.ssh_exception.AuthenticationException: |
||
91 | sys.exit( |
||
92 | output.message( |
||
93 | output.Subject.ERROR, |
||
94 | f'SSH authentication for {_host_name} failed', |
||
95 | False |
||
96 | ) |
||
97 | ) |
||
98 | |||
99 | output.message( |
||
100 | output.host_to_subject(ssh), |
||
101 | f'Initialize remote SSH connection {_host_name}{_authentication_method}', |
||
102 | True |
||
103 | ) |
||
104 | |||
105 | return _ssh_client |
||
106 | |||
190 |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.