Attempt to connect to SSH using only the supplied credentials. Retries up to 3 times on banner/connection errors with backoff.
(self, adresse_ip, user, password)
| 124 | ssh = paramiko.SSHClient() |
| 125 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 126 | try: |
| 127 | ssh.connect( |
| 128 | adresse_ip, |
| 129 | username=user, |
| 130 | password=password, |
| 131 | port=22, |
| 132 | timeout=15, |
| 133 | auth_timeout=15, |
| 134 | banner_timeout=15, |
| 135 | look_for_keys=False, |
| 136 | allow_agent=False |
| 137 | ) |
| 138 | logger.debug(f"SSH login succeeded for {adresse_ip} using {user}:{password}") |
| 139 | return True |
| 140 | except paramiko.AuthenticationException: |
| 141 | logger.debug(f"SSH authentication failed for {adresse_ip} | user={user}") |
| 142 | return False |
| 143 | except (socket.error, paramiko.SSHException, EOFError) as exc: |
| 144 | if attempt < max_retries - 1: |
| 145 | delay = 2 + attempt * 2 # 2s, 4s backoff |
| 146 | logger.debug(f"SSH banner/connection error to {adresse_ip} (attempt {attempt+1}/{max_retries}), retrying in {delay}s: {exc}") |
| 147 | time.sleep(delay) |
| 148 | else: |
| 149 | logger.warning(f"SSH connection error to {adresse_ip} after {max_retries} attempts: {exc}") |
| 150 | return False |
| 151 | finally: |
| 152 | ssh.close() |
| 153 | |
| 154 | @staticmethod |
| 155 | def _get_local_ips(): |
| 156 | """Get all IP addresses belonging to this machine.""" |
| 157 | local_ips = {'127.0.0.1', '::1'} |
| 158 | try: |
| 159 | hostname = socket.gethostname() |
| 160 | for info in socket.getaddrinfo(hostname, None): |
| 161 | local_ips.add(info[4][0]) |
| 162 | except socket.gaierror: |