cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
451
Views
0
Helpful
1
Replies

pyATS: How to handle exception with dev.connect()

assadniang
Level 1
Level 1

How would I handle this exception with dev.connect()

When I try to connect to a device and the device is not available, the code crashes with the following exception

How would I handle the exception, so it does not crash and continue

I am rebooting the router, and I have a loop to try to connect every 60sec

If the connection fails, try to connect again, until the connection is establish

 

 

from pyats.topology import loader

testbed = loader.load('devices.yaml')

dev = testbed.devices['rtr1']
dev.connect()
---------------------- This is what I have tried -----------------
try:
    dev.connect()
except OSError as error:
    print('error')

 

 

 

ssh: connect to host f207:a185:172:30:7:0:513:1 port 22: No route to host


2023-10-25 00:17:20,520: %UNICON-INFO: +++ connection to spawn: ssh -l root f207:a185:172:30:7:0:513:1, id: 140184778444464 +++

2023-10-25 00:17:20,521: %UNICON-INFO: connection to ptr513-u210
Traceback (most recent call last):
  File "src/unicon/eal/backend/pty_backend.py", line 498, in unicon.eal.backend.pty_backend.RawPtySpawn._read
OSError: [Errno 5] Input/output error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "src/unicon/statemachine/statemachine.py", line 737, in unicon.statemachine.statemachine.StateMachine.go_to
  File "src/unicon/statemachine/statetransition.py", line 469, in unicon.statemachine.statetransition.AnyStateTransition.do_transitions
  File "src/unicon/eal/backend/pty_backend.py", line 342, in unicon.eal.backend.pty_backend.RawSpawn.expect
  File "src/unicon/eal/backend/pty_backend.py", line 135, in unicon.eal.backend.pty_backend.RawSpawn.read_update_buffer
  File "src/unicon/eal/backend/pty_backend.py", line 120, in unicon.eal.backend.pty_backend.RawSpawn.read
  File "src/unicon/eal/backend/pty_backend.py", line 501, in unicon.eal.backend.pty_backend.RawPtySpawn._read
unicon.core.errors.EOF: ('Unable to read. Connection closed or not available', OSError(5, 'Input/output error'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "src/unicon/bases/connection.py", line 792, in unicon.bases.connection.Connection.connect
  File "src/unicon/bases/routers/connection_provider.py", line 227, in unicon.bases.routers.connection_provider.BaseSingleRpConnectionProvider.connect
  File "src/unicon/bases/routers/connection_provider.py", line 253, in unicon.bases.routers.connection_provider.BaseSingleRpConnectionProvider.establish_connection
  File "src/unicon/statemachine/statemachine.py", line 740, in unicon.statemachine.statemachine.StateMachine.go_to
unicon.core.errors.StateMachineError: Failed while bringing device to "any" state

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "src/pyats/connections/manager.py", line 453, in pyats.connections.manager.ConnectionManager.connect
  File "src/unicon/bases/connection.py", line 799, in unicon.bases.connection.Connection.connect
unicon.core.errors.ConnectionError: failed to connect to ptr513-u210
Failed while bringing device to "any" state

 

 

1 Accepted Solution

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

Hi 

This should work:

from unicon.core.errors import TimeoutError, StateMachineError, ConnectionError

try:
    # do your connection here

except (TimeoutError, StateMachineError, ConnectionError):
    # handle the error here

HTH

View solution in original post

1 Reply 1

Marcel Zehnder
Spotlight
Spotlight

Hi 

This should work:

from unicon.core.errors import TimeoutError, StateMachineError, ConnectionError

try:
    # do your connection here

except (TimeoutError, StateMachineError, ConnectionError):
    # handle the error here

HTH