Followers

Friday, January 13, 2017

Network Automation via Python Use Case No.1 : Collect Administratively Down interfaces from all the Network



Dears,
Python in Action now.
As i stated in my last Article that i will deep dive in the Real Network Use Cases.
Here is the First Use Case to collect Administratively down GigabitEthernet and Ethernet interfaces from all or part of my routers in my lab.
Before the beginning I want to let you know How i setup the environment :

1) I built my network on the GNS3 as follow.


2) I Connected a cloud to the P1 router interface G5/0 and i assigned to this cloud VMnet1 with the below configuration.


Don't assign a default gateway to avoid the misleading may be occurred because of existence of another default route with less metric.
If there is another default route (related for USB modem for e.g.) with less metric than that to 100.100.100.1 ( P1 interface ).
The ping to the the lab routers may loose the way.
Makes it statically to all the loopbacks of routers that we will deal with.
Hint : to add static routes we should run the cmd window as administrator.
-p : to be permanent , to survive the reboot.



3) Then i assigned ip for GigabitEthernet 5/0 of P1 : 100.100.100.1 at the same network of VMnet1 , then i advertised that interface in IGP (ISIS) , now P1 Giga5/0 should be like the below :

4) Ensuring that every thing is connected , the prefix now is advertised to all routers via isis.
Ping from both sides are working properly now ( command window and routers at lab).


 5) The below python code is applied only on Cisco Native ios and xe like CSR1000v ,7200 and ASR1k.
firstly we should describe our routers via a dictionary in a separate file acting as a module:
the dictionary is a python Datatype that contains of keys and values.
the below is only for P1 , P2 , P3 and P4 routers




Then here is the code , simply I imported the needed modules


re : regular expression module with functions like findall() to search for specific pattern in all the string output.
netmiko : Module to ssh on routers and to enter the configuration mode with,
ConnectHandler() is a factory function selects the correct netmiko class based upon the device_type exists at the dictionary.
find_prompt() return the current router prompt , i used it only to get the hostname of the router to put it in the beginning of the output.
send_command() to send configuration to routers.
my_ccie_routers : it is the module at which i insert in my dictionaries
for further information about netmiko you can get it from here.
\d : the meta character that represents the digit
for further information about regular expressions you can get it from here.

----------------------------------------------------------------------------------------------------------
Here is the output of the code :)


C:\Python27\python.exe "F:/MPLS_TE/try ssh.py"

# For Device with hostname : P1 and ip 1.1.1.1 #
--------------------------------------------------------------------------
these are the administrative down  interfaces  :
['GigabitEthernet1/0', 'GigabitEthernet3/0', 'GigabitEthernet4/0']
['Ethernet0/0']

# For Device with hostname : P2 and ip 2.2.2.2 #
--------------------------------------------------------------------------
these are the administrative down  interfaces  :
['GigabitEthernet3/0', 'GigabitEthernet5/0']
['Ethernet0/0']

# For Device with hostname : P3 and ip 3.3.3.3 #
--------------------------------------------------------------------------
these are the administrative down  interfaces  :
['GigabitEthernet0/0', 'GigabitEthernet1/0', 'GigabitEthernet2/0']
['Ethernet0/0']

# For Device with hostname : P4 and ip 4.4.4.4 #
--------------------------------------------------------------------------
these are the administrative down  interfaces  :
['GigabitEthernet0/0', 'GigabitEthernet3/0', 'GigabitEthernet5/0']
['Ethernet0/0']

# For Device with hostname : PE1 and ip 11.11.11.11 #
--------------------------------------------------------------------------
these are the administrative down  interfaces  :
['GigabitEthernet0/0', 'GigabitEthernet2/0']
['Ethernet0/0']

# For Device with hostname : PE2 and ip 22.22.22.22 #
--------------------------------------------------------------------------
these are the administrative down  interfaces  :
['GigabitEthernet0/0', 'GigabitEthernet1/0', 'GigabitEthernet4/0', 'GigabitEthernet5/0']
['Ethernet0/0']

Process finished with exit code 0
 
 
Finally i think that will really save a lot of time on us ☺ ,
I hope that post would be informative to you , and if anyone stuck in anything ping me and i will support him till will tackle the problems.
Keep tuned for the coming post , it will discuss how to configure multiple devices☺

Regards,
Mostafa Hassan Ahmed

No comments:

Post a Comment