<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic pyATS: How to handle exception with dev.connect() in Tools</title>
    <link>https://community.cisco.com/t5/tools/pyats-how-to-handle-exception-with-dev-connect/m-p/4947291#M3420</link>
    <description>&lt;P&gt;How would I handle this exception with dev.connect()&lt;/P&gt;&lt;P&gt;When I try to connect to a device and the device is not available, the code crashes with the following exception&lt;/P&gt;&lt;P&gt;How would I handle the exception, so it does not crash and continue&lt;/P&gt;&lt;P&gt;I am rebooting the router, and I have a loop to try to connect every 60sec&lt;/P&gt;&lt;P&gt;If the connection fails, try to connect again, until the connection is establish&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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')
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 "&amp;lt;stdin&amp;gt;", line 2, in &amp;lt;module&amp;gt;
  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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 25 Oct 2023 04:44:11 GMT</pubDate>
    <dc:creator>assadniang</dc:creator>
    <dc:date>2023-10-25T04:44:11Z</dc:date>
    <item>
      <title>pyATS: How to handle exception with dev.connect()</title>
      <link>https://community.cisco.com/t5/tools/pyats-how-to-handle-exception-with-dev-connect/m-p/4947291#M3420</link>
      <description>&lt;P&gt;How would I handle this exception with dev.connect()&lt;/P&gt;&lt;P&gt;When I try to connect to a device and the device is not available, the code crashes with the following exception&lt;/P&gt;&lt;P&gt;How would I handle the exception, so it does not crash and continue&lt;/P&gt;&lt;P&gt;I am rebooting the router, and I have a loop to try to connect every 60sec&lt;/P&gt;&lt;P&gt;If the connection fails, try to connect again, until the connection is establish&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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')
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 "&amp;lt;stdin&amp;gt;", line 2, in &amp;lt;module&amp;gt;
  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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 04:44:11 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/pyats-how-to-handle-exception-with-dev-connect/m-p/4947291#M3420</guid>
      <dc:creator>assadniang</dc:creator>
      <dc:date>2023-10-25T04:44:11Z</dc:date>
    </item>
    <item>
      <title>Re: pyATS: How to handle exception with dev.connect()</title>
      <link>https://community.cisco.com/t5/tools/pyats-how-to-handle-exception-with-dev-connect/m-p/4947335#M3421</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This should work:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;from unicon.core.errors import TimeoutError, StateMachineError, ConnectionError

try:
    # do your connection here

except (TimeoutError, StateMachineError, ConnectionError):
    # handle the error here&lt;/LI-CODE&gt;
&lt;P&gt;HTH&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 06:15:49 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/pyats-how-to-handle-exception-with-dev-connect/m-p/4947335#M3421</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2023-10-25T06:15:49Z</dc:date>
    </item>
  </channel>
</rss>

