| Conditions | 7 |
| Total Lines | 54 |
| Code Lines | 31 |
| Lines | 54 |
| Ratio | 100 % |
| 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 | # -*- coding: utf-8 -*- |
||
| 99 | def run_command(self, scan_id, host, cmd): |
||
| 100 | """ |
||
| 101 | Run a single command via SSH and return the content of stdout or |
||
| 102 | None in case of an Error. A scan error is issued in the latter |
||
| 103 | case. |
||
| 104 | |||
| 105 | For logging into 'host', the scan options 'port', 'username', |
||
| 106 | 'password' and 'ssh_timeout' are used. |
||
| 107 | """ |
||
| 108 | |||
| 109 | ssh = paramiko.SSHClient() |
||
| 110 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
||
| 111 | |||
| 112 | options = self.get_scan_options(scan_id) |
||
| 113 | |||
| 114 | port = int(options['port']) |
||
| 115 | timeout = int(options['ssh_timeout']) |
||
| 116 | |||
| 117 | # For backward compatibility, consider the legacy mode to get |
||
| 118 | # credentials as scan_option. |
||
| 119 | # First and second modes should be removed in future releases. |
||
| 120 | # On the third case it receives the credentials as a subelement of |
||
| 121 | # the <target>. |
||
| 122 | credentials = self.get_scan_credentials(scan_id, host) |
||
| 123 | if ('username_password' in options and |
||
| 124 | ':' in options['username_password']): |
||
| 125 | username, password = options['username_password'].split(':', 1) |
||
| 126 | elif 'username' in options and options['username']: |
||
| 127 | username = options['username'] |
||
| 128 | password = options['password'] |
||
| 129 | elif credentials: |
||
| 130 | cred_params = credentials.get('ssh') |
||
| 131 | username = cred_params.get('username', '') |
||
| 132 | password = cred_params.get('password', '') |
||
| 133 | else: |
||
| 134 | self.add_scan_error(scan_id, host=host, |
||
| 135 | value='Erroneous username_password value') |
||
| 136 | raise ValueError('Erroneous username_password value') |
||
| 137 | |||
| 138 | try: |
||
| 139 | ssh.connect(hostname=host, username=username, password=password, |
||
| 140 | timeout=timeout, port=port) |
||
| 141 | except (paramiko.ssh_exception.AuthenticationException, |
||
| 142 | socket.error) as err: |
||
| 143 | # Errors: No route to host, connection timeout, authentication |
||
| 144 | # failure etc,. |
||
| 145 | self.add_scan_error(scan_id, host=host, value=str(err)) |
||
| 146 | return None |
||
| 147 | |||
| 148 | _, stdout, _ = ssh.exec_command(cmd) |
||
| 149 | result = stdout.readlines() |
||
| 150 | ssh.close() |
||
| 151 | |||
| 152 | return result |
||
| 153 |