There are several network devices. The connection is established with each of them in turn. When there is a connection, a sign should appear - ok, when not - no. The problem is when there is no connection with one device, the waiting time expires, and the program simply does not reach the appearance of a window with "no". How can I limit the time in the code to wait for a response from the device, break the connection establishment? For example, if 5 seconds has passed and there is no answer, display such a window. The code is below. I use python3 and paramiko, Windows7.

ssh = SSHConfig() params = ssh.lookup(user_password_rnm) ssh = SSHClient() ssh.set_missing_host_key_policy(AutoAddPolicy()) ssh.connect(ipAddress_rnm, port=22, username=user_name_rnm, password=user_password_rnm) if ssh.invoke_shell(): st = ['with: ', name_rnm, ' - OK'] showinfo('Connection', ' '.join(st)) ssh.close() else: st = ['with: ', name_rnm, ' - NO CONNECT'] showinfo('Connection', ' '.join(st)) 

in if an error "timeout expired" occurs and the execution of the program does not reach else. How to make NO CONNECT window appear?

    1 answer 1

    A quick scan of the documentation showed that if the connection failed, invoke_shell throws an exception, and you don't handle it anywhere. You need to wrap invoke_shell in try … except (and connect in a good way too).

     try: ssh.connect(ipAddress_rnm, port=22, username=user_name_rnm, password=user_password_rnm) ssh.invoke_shell() st = 'with: {0} - OK'.format(name_rnm) showinfo('Connection', st) ssh.close() except (BadHostKeyException, AuthenticationException, SSHException) as e: st = 'with: {0} - NO CONNECT. Reason: {1}'.format(name_rnm, str(e)) showinfo('Connection', st) 
    • Thanks, changed the string like this: except TimeoutError as e: and it worked as it should. Thanks again. - Teit