Dev: VlansInventory.py

Dev: VlansInventory.py

Hi there, previously I shared a script that would get the vlans and show them on screen. Another useful requirement that I wanted to solve was to store that information on a CSV format as an inventory file.

from netmiko import ConnectHandler

import csv


# inventory with the ip of switches

devices = open('devices')


for ip in devices:

   session = {

       'ip': ip,

       'username': 'silesio',

       'password': 'cisco',

       'device_type': 'cisco_ios'

   }

   

   login = ConnectHandler(**session)

   # get the vlans

   output_vlan = login.send_command('show vlan brief', use_textfsm=True)

   # get the hostname

   output_hostname = login.send_command('show run | in hostname')

   hostname = output_hostname.split(' ')

   hostname = hostname[1]

   # add hostname to all dictionaries

   counter = 0

   size = len(output_vlan)

   for i in output_vlan:

       while counter < size:

           output_vlan[counter]['hostname'] = hostname

           counter += 1


   # the columns name

   columns = ['vlan_id', 'name', 'status', 'interfaces', 'hostname']

   # save the output to a new file

   with open('Vlans_Inventory.csv', 'a+', newline='') as output_file:

       inventory_file = csv.DictWriter(output_file, fieldnames=columns)

       inventory_file.writeheader()

       inventory_file.writerows(output_vlan)

   

print(output_vlan)


No alt text provided for this image


PS: I Just wanted to share a personal tip that may help you if you’re planning to start using python as well. If your background is anything else except Devops (html, java, python, c, c#...), my suggestion is to try these scripts and see the results. Don’t just take a course on Python or watch videos learning about tuples, dictionaries, thanos, loops, variables… Especially if you’re NE. Just start playing with 2 or 10 devices and automate most of the repetitive tasks that you do daily.

I hope you enjoyed this post, leave your comments below and I'll see you on the next one.


要查看或添加评论,请登录

Silésio Carvalho的更多文章

  • The Power of Visualization

    The Power of Visualization

    I am Silésio Carvalho, CCIE #65745 and I passed the CCIE lab exam on November 30 2021. If you paid attention to the…

    8 条评论
  • ForticlientEMS fails connection with Fortihate

    ForticlientEMS fails connection with Fortihate

    For those trying to play with Fortigate ZTNA, if you're using Fortigate on evaluation mode, one of the restrictions…

  • Why you won't become a CCIE

    Why you won't become a CCIE

    I'm not the author for the text below but I wanted to share this with you, in case you're thinking about taking the…

    10 条评论
  • Trobleshoot SD-WAN – Part 2

    Trobleshoot SD-WAN – Part 2

    Hi there, in the last article we saw how to troubleshoot control connection failure on Cisco SD-WAN, caused by…

  • Trobleshoot SD-WAN – Part 1

    Trobleshoot SD-WAN – Part 1

    Hi there, While deploying Cisco SD-WAN you might face control connection errors related to certificate. Here are some…

  • Como eu come?aria de novo, em Networking (Redes)?

    Como eu come?aria de novo, em Networking (Redes)?

    Estou nesta jornada a mais de uma década e até a data, tem sido uma caminhada nada fácil (por op??o minha) mas muito…

    6 条评论
  • CCIE - The Plan

    CCIE - The Plan

    Hi there, first of all, I’d like thank everyone for the congratulations messages. As a thank you back, I’ll share with…

    20 条评论
  • How To's - Deploy Checkpoint Remote Access VPN

    How To's - Deploy Checkpoint Remote Access VPN

    Hi there, in this post we'll see how to deploy remote access using Checkpoint Remote Access VPN client. Some…

    3 条评论
  • How To’s – Deploy DMVPN Front Door VRF (automating with ansible)

    How To’s – Deploy DMVPN Front Door VRF (automating with ansible)

    Hi there, in this post we're going to deploy DMVPN Front Door VRF and using ansible. Basically, we'll be using DMVPN…

    1 条评论
  • How To’s – Deploy DMVPN Dual Hub Dual Cloud

    How To’s – Deploy DMVPN Dual Hub Dual Cloud

    Hi there, in this post we’ll see how to deploy DMVPN Dual Hub Dual Cloud. Basically it means we’ll have two or more…

社区洞察

其他会员也浏览了