<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bilal</title>
    <description>The latest articles on DEV Community by Bilal (@bilal2py).</description>
    <link>https://dev.to/bilal2py</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F10670%2FRRapqhHT.jpg</url>
      <title>DEV Community: Bilal</title>
      <link>https://dev.to/bilal2py</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bilal2py"/>
    <language>en</language>
    <item>
      <title>pyATS, the Cisco python automation tool</title>
      <dc:creator>Bilal</dc:creator>
      <pubDate>Fri, 18 Jun 2021 10:44:16 +0000</pubDate>
      <link>https://dev.to/bilal2py/pyats-the-cisco-python-automation-tool-4gof</link>
      <guid>https://dev.to/bilal2py/pyats-the-cisco-python-automation-tool-4gof</guid>
      <description>&lt;p&gt;Automation is a core element in the management of network devices. &lt;br&gt;
In this post, I tried to set up the pyATS toolset by Cisco. It's a library that can be used in full python code and has a CLI tool add-on. It's vendor agnostic, and you can run your tests or operations tasks.&lt;/p&gt;
&lt;h3&gt;
  
  
  The installation
&lt;/h3&gt;

&lt;p&gt;pyATS needs to be installed on Linux based system, as I'm using a Windows setup, I installed it on WSL.&lt;br&gt;
I installed pyATS with the Python package manager, pip :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install "pyats[full]"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installed, the libraries are available to import in your Python code along with the CLI tool.&lt;/p&gt;

&lt;h3&gt;
  
  
  Write the python code
&lt;/h3&gt;

&lt;p&gt;Your devices have to be loaded in a testbed instance.&lt;br&gt;
The said devices are defined in a YAML file.&lt;br&gt;
Once your target device is selected, you can extract and parse the result of a command.&lt;br&gt;
The pyATS ecosystem has built-in parser, for commands like &lt;em&gt;show inventory&lt;/em&gt;, but you can write your own parser for specific use-case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from genie.testbed import load

testbed = load('my_testbed.yaml')
dev = testbed.devices['nx-osv-1']
dev.connect()
p1 = dev.parse('show inventory')

print('Slot 1 serial number:' + p1['name']['Slot 1']['serial_number'])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  My devices to TestBed
&lt;/h3&gt;

&lt;p&gt;Devices information have to be stored in a yaml file, my_testbed.yaml in the above example. It contains device information like ip addresses, OS, hostname, login, password, and so on.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;devices:
  R1:
    connections:
      cli:
        ip: 10.10.10.11
        protocol: ssh
    credentials:
      default:
        password: password
        username: admin
      enable:
        password: password
    os: iosxe
    platform: asr1k
    type: iosxe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To avoid any typo, a csv(or xls) file can be used to store the devices data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhheqc5uh6uhzcfbag8r6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhheqc5uh6uhzcfbag8r6.png" alt="Devices information in CSV file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once all values completed, you can then convert the csv file to YAML format using the pyats cli:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(pyATS) ➜  pyats create testbed file --path ./my_devices.csv --output ./my_testbed.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>cisco</category>
      <category>automation</category>
      <category>network</category>
      <category>python</category>
    </item>
  </channel>
</rss>
