Network Automation with Python

Anyone working on Python for automating Network tasks. Need to discuss on ideas to implement,

Hi Sandeep,

I’m currently researching this. Instead of python, have you checked out Ansible yet?

Ansible uses python but has a lot of built-in plugins/modules that make it easier to create scripts than writing python scripts from scratch.

Rene

1 Like

Hey Rene,

Thanks for prompt response.

I heard about Ansible but never used in my job but python we use it on our daily tasks but few challenges on writing functions regex, will check Ansible and see how will it help.

Please share your thoughts and progress with Ansible. I am sure most of your users are waiting for lessons on it.

Regards
Sandeep Paul

Hi Sandeep,

What I like about Ansible is that it’s agentless and works through SSH. You don’t have to install anything on the device(s) you want to control.

One of the things I like about Ansible is that it has a lot of logic built in. For example, let’s say you want to install Apache on a bunch of servers. You have 10 servers:

  • 5 are new servers with nothing installed.
  • 2 are running Apache but an older version.
  • 3 are running the latest Apache version.

When you create a simple playbook in Ansible (that’s what they call scripts) to install Apache, it will automatically check if Apache is installed and if it should be upgraded or not. Of course, you can do this in python too but you’ll have to code all the different checks yourself.

The same thing with Cisco devices. I’m currently testing/learning Ansible but I’ll add tutorials for this later. Here’s a quick preview:

This is a playbook to get CDP neighbors:

$ cat show_cdp_neighbors.yml 
---
- hosts: CAMPUS
  gather_facts: no
  connection: local

  tasks:
  - name: Include Login Credentials
    include_vars: secrets.yml

  - name: Define Provider
    set_fact:
      provider:
        host: "{{ ansible_host }}"
        username: "{{ creds['username'] }}"
        password: "{{ creds['password'] }}"

  - name: RUN 'show cdp neighbors'
    ios_command:
      provider: "{{ provider }}"
      commands:
        - show cdp neighbors
    register: cdp

  - debug: var=cdp.stdout_lines

Let’s run it:

$ ansible-playbook show_cdp_neighbors.yml 

PLAY [CAMPUS] **********************************************************************************************************************************************

TASK [Include Login Credentials] ***************************************************************************************************************************
ok: [DSW1]
ok: [DSW2]
ok: [ASW1]
ok: [ASW2]

TASK [Define Provider] *************************************************************************************************************************************
ok: [DSW1]
ok: [DSW2]
ok: [ASW1]
ok: [ASW2]

TASK [RUN 'show cdp neighbors'] ****************************************************************************************************************************
ok: [DSW1]
ok: [DSW2]
ok: [ASW1]
ok: [ASW2]

TASK [debug] ***********************************************************************************************************************************************
ok: [DSW1] => {
    "cdp.stdout_lines": [
        [
            "Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge", 
            "                  S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone, ", 
            "                  D - Remote, C - CVTA, M - Two-port Mac Relay ", 
            "", 
            "Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID", 
            "Switch           Gig 0/2           80              R S I            Gig 0/2", 
            "Switch           Gig 0/1           80              R S I            Gig 0/1", 
            "DSW2.NWL.LOCAL   Gig 0/2           138             R S I            Gig 0/2", 
            "DSW2.NWL.LOCAL   Gig 0/1           137             R S I            Gig 0/1", 
            "DSW2.NWL.LOCAL   Gig 0/0           148             R S I            Gig 0/0", 
            "ASW1.NWL.LOCAL   Gig 0/0           129             R S I            Gig 0/0", 
            "ASW1.NWL.LOCAL   Gig 1/0           179             R S I            Gig 0/2", 
            "ASW1.NWL.LOCAL   Gig 0/3           172             R S I            Gig 0/1", 
            "ASW2.NWL.LOCAL   Gig 0/0           134             R S I            Gig 0/0", 
            "ASW2.NWL.LOCAL   Gig 1/2           134             R S I            Gig 1/0", 
            "ASW2.NWL.LOCAL   Gig 1/1           178             R S I            Gig 0/3", 
            "DSW2             Gig 0/2           81              R S I            Gig 0/2", 
            "DSW2             Gig 0/1           81              R S I            Gig 0/1", 
            "", 
            "Total cdp entries displayed : 13"
        ]
    ]
}
ok: [DSW2] => {
    "cdp.stdout_lines": [
        [
            "Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge", 
            "                  S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone, ", 
            "                  D - Remote, C - CVTA, M - Two-port Mac Relay ", 
            "", 
            "Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID", 
            "DSW1.NWL.LOCAL   Gig 0/0           140             R S I            Gig 0/0", 
            "DSW1.NWL.LOCAL   Gig 0/2           136             R S I            Gig 0/2", 
            "DSW1.NWL.LOCAL   Gig 0/1           136             R S I            Gig 0/1", 
            "ASW1.NWL.LOCAL   Gig 0/0           128             R S I            Gig 0/0", 
            "ASW1.NWL.LOCAL   Gig 1/1           127             R S I            Gig 0/3", 
            "ASW1.NWL.LOCAL   Gig 1/2           174             R S I            Gig 1/0", 
            "ASW2.NWL.LOCAL   Gig 0/0           132             R S I            Gig 0/0", 
            "ASW2.NWL.LOCAL   Gig 1/0           175             R S I            Gig 0/2", 
            "ASW2.NWL.LOCAL   Gig 0/3           178             R S I            Gig 0/1", 
            "DSW1             Gig 0/2           80              R S I            Gig 0/2", 
            "DSW1             Gig 0/1           80              R S I            Gig 0/1", 
            "", 
            "Total cdp entries displayed : 11"
        ]
    ]
}
ok: [ASW1] => {
    "cdp.stdout_lines": [
        [
            "Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge", 
            "                  S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone, ", 
            "                  D - Remote, C - CVTA, M - Two-port Mac Relay ", 
            "", 
            "Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID", 
            "Switch           Gig 1/0           71              R S I            Gig 1/2", 
            "Switch           Gig 0/3           71              R S I            Gig 1/1", 
            "Switch           Gig 0/2           70              R S I            Gig 1/0", 
            "Switch           Gig 0/1           70              R S I            Gig 0/3", 
            "Switch           Gig 0/0           71              R S I            Gig 0/0", 
            "DSW1.NWL.LOCAL   Gig 0/2           134             R S I            Gig 1/0", 
            "DSW1.NWL.LOCAL   Gig 0/1           142             R S I            Gig 0/3", 
            "DSW1.NWL.LOCAL   Gig 0/0           138             R S I            Gig 0/0", 
            "DSW2.NWL.LOCAL   Gig 1/0           134             R S I            Gig 1/2", 
            "DSW2.NWL.LOCAL   Gig 0/3           142             R S I            Gig 1/1", 
            "DSW2.NWL.LOCAL   Gig 0/0           145             R S I            Gig 0/0", 
            "ASW2.NWL.LOCAL   Gig 0/0           130             R S I            Gig 0/0", 
            "DSW1             Gig 0/2           72              R S I            Gig 1/0", 
            "DSW1             Gig 0/1           72              R S I            Gig 0/3", 
            "DSW1             Gig 0/0           72              R S I            Gig 0/0", 
            "DSW2             Gig 1/0           73              R S I            Gig 1/2", 
            "DSW2             Gig 0/3           73              R S I            Gig 1/1", 
            "DSW2             Gig 0/0           73              R S I            Gig 0/0", 
            "ASW2             Gig 0/0           65              R S I            Gig 0/0", 
            "", 
            "Total cdp entries displayed : 19"
        ]
    ]
}
ok: [ASW2] => {
    "cdp.stdout_lines": [
        [
            "Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge", 
            "                  S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone, ", 
            "                  D - Remote, C - CVTA, M - Two-port Mac Relay ", 
            "", 
            "Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID", 
            "Switch           Gig 0/2           71              R S I            Gig 1/0", 
            "Switch           Gig 0/1           71              R S I            Gig 0/3", 
            "Switch           Gig 1/0           69              R S I            Gig 1/2", 
            "Switch           Gig 0/3           69              R S I            Gig 1/1", 
            "Switch           Gig 0/0           71              R S I            Gig 0/0", 
            "DSW1.NWL.LOCAL   Gig 1/0           133             R S I            Gig 1/2", 
            "DSW1.NWL.LOCAL   Gig 0/3           131             R S I            Gig 1/1", 
            "DSW1.NWL.LOCAL   Gig 0/0           137             R S I            Gig 0/0", 
            "DSW2.NWL.LOCAL   Gig 0/0           144             R S I            Gig 0/0", 
            "DSW2.NWL.LOCAL   Gig 0/2           133             R S I            Gig 1/0", 
            "DSW2.NWL.LOCAL   Gig 0/1           134             R S I            Gig 0/3", 
            "ASW1.NWL.LOCAL   Gig 0/0           179             R S I            Gig 0/0", 
            "DSW1             Gig 1/0           72              R S I            Gig 1/2", 
            "DSW1             Gig 0/3           72              R S I            Gig 1/1", 
            "DSW1             Gig 0/0           72              R S I            Gig 0/0", 
            "DSW2             Gig 0/2           72              R S I            Gig 1/0", 
            "DSW2             Gig 0/1           72              R S I            Gig 0/3", 
            "DSW2             Gig 0/0           72              R S I            Gig 0/0", 
            "", 
            "Total cdp entries displayed : 18"
        ]
    ]
}

PLAY RECAP *************************************************************************************************************************************************
ASW1                       : ok=4    changed=0    unreachable=0    failed=0   
ASW2                       : ok=4    changed=0    unreachable=0    failed=0   
DSW1                       : ok=4    changed=0    unreachable=0    failed=0   
DSW2                       : ok=4    changed=0    unreachable=0    failed=0   

This is just a simple example but the playbook is short and to the point.

Rene

1 Like

Hi Rene,

Great example of automation.
I am CCNA certified thanks to your lessons. Working on CCNP now. How do I start in automation. I am trying to make myself marketable since I have been unemploy for over 8 months with more than 6 years of experience. Or should I focus on Cybersecurity instead? Where should i start?
Thanks in advance for your input. Pete

Hello Pedro

It’s great to hear how useful Networklessons has been for you in getting your CCNA. Good luck and I wish you success on your way to the CCNP!

As for where to start, I think that it is very important to set out a path for your future. Where do you want to go? Do you want to deal primarily with Routing and Switching? Do you want to specialise in automation or security? Work on your CCNP, which is a great springboard from which to go into other specialised areas, and then branch out from there.

I think the most important aspects of your decisions should be the following two things:

  1. What is required in the market today as far as networking expertise goes? Both automation and cybersecurity are important aspects of networking in demand today. There are many more as well such as IoT, network architecture and planning, voice and collaboration applications as well as VoD and wireless.
  2. The second aspect, and in my opinion the most important one, is to choose what you like. It’s so important to enjoy what you do, because then you’ll be more proficient in what you do and you’ll be more prone to spend the time to learn and continue your training for the duration of your career.

I hope these thoughts will help you out to be able to continue in your endeavour to advance your career. I wish you success in all you do!!

I hope this has been helpful!

Laz