contrib/automation/hgautomation/ssh.py
changeset 43076 2372284d9457
parent 42285 65b3ef162b39
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
     9 
     9 
    10 import socket
    10 import socket
    11 import time
    11 import time
    12 import warnings
    12 import warnings
    13 
    13 
    14 from cryptography.utils import (
    14 from cryptography.utils import CryptographyDeprecationWarning
    15     CryptographyDeprecationWarning,
       
    16 )
       
    17 import paramiko
    15 import paramiko
    18 
    16 
    19 
    17 
    20 def wait_for_ssh(hostname, port, timeout=60, username=None, key_filename=None):
    18 def wait_for_ssh(hostname, port, timeout=60, username=None, key_filename=None):
    21     """Wait for an SSH server to start on the specified host and port."""
    19     """Wait for an SSH server to start on the specified host and port."""
       
    20 
    22     class IgnoreHostKeyPolicy(paramiko.MissingHostKeyPolicy):
    21     class IgnoreHostKeyPolicy(paramiko.MissingHostKeyPolicy):
    23         def missing_host_key(self, client, hostname, key):
    22         def missing_host_key(self, client, hostname, key):
    24             return
    23             return
    25 
    24 
    26     end_time = time.time() + timeout
    25     end_time = time.time() + timeout
    27 
    26 
    28     # paramiko triggers a CryptographyDeprecationWarning in the cryptography
    27     # paramiko triggers a CryptographyDeprecationWarning in the cryptography
    29     # package. Let's suppress
    28     # package. Let's suppress
    30     with warnings.catch_warnings():
    29     with warnings.catch_warnings():
    31         warnings.filterwarnings('ignore',
    30         warnings.filterwarnings(
    32                                 category=CryptographyDeprecationWarning)
    31             'ignore', category=CryptographyDeprecationWarning
       
    32         )
    33 
    33 
    34         while True:
    34         while True:
    35             client = paramiko.SSHClient()
    35             client = paramiko.SSHClient()
    36             client.set_missing_host_key_policy(IgnoreHostKeyPolicy())
    36             client.set_missing_host_key_policy(IgnoreHostKeyPolicy())
    37             try:
    37             try:
    38                 client.connect(hostname, port=port, username=username,
    38                 client.connect(
    39                                key_filename=key_filename,
    39                     hostname,
    40                                timeout=5.0, allow_agent=False,
    40                     port=port,
    41                                look_for_keys=False)
    41                     username=username,
       
    42                     key_filename=key_filename,
       
    43                     timeout=5.0,
       
    44                     allow_agent=False,
       
    45                     look_for_keys=False,
       
    46                 )
    42 
    47 
    43                 return client
    48                 return client
    44             except socket.error:
    49             except socket.error:
    45                 pass
    50                 pass
    46             except paramiko.AuthenticationException:
    51             except paramiko.AuthenticationException: