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

Wednesday, January 11, 2017

Network Automation via python




Python is used by top tech companies right now , but Before saying python , we should ask our selves what are the Use Cases exist in our Network ? !
How you will deal with these simple use cases?
  • If you added a new Route Reflector at your fixed or BB Network , and you have around 300 PEs need to be configured to point to it.
  • If you added a large Number of New devices and you want to push the day 1 configuration(AAA, Tacacs,local credentials,SNMP,Syslog,…).
  • check the administrative down interfaces on a large number of devices and collecting them on a file.
  • A new customer wants to join your MPLS network and he will connect his branches to around 30 PEs of your MPLS network , what about the vrf that will be pushed over all these devices , to make it once or to make it 30 times , it's your choice :D
  • Your Network based on full mesh RSVP TE instead of LDP , and you will add a new PE router that needs to make 199 RSVP TE tunnels to all the PEs exist in the network.
if you can solve the above examples and you didn't fail a pain , so you are for sure a python :)
How as a Network Engineer to start studying the python ?
My recommendations for that :
Finally keep tuned for the upcoming posts , i will discuss on it :
  • How to telnet and ssh on the routers at your network via python modules telnetlib , paramiko and netmiko.
  • How to collect information (show commands) from different devices.
  • How to configure multiple devices from a configuration existing in a file.
Regards ,
Mostafa Hassan Ahmed