| Conditions | 5 |
| Total Lines | 55 |
| Code Lines | 36 |
| 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 |
||
| 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 | |||
| 101 | ssh_client_target.close() |
||