Null route IOSXR Python script

Python-logo-notext.svgIts been a while since my last post, mainly because I have been bitten by the Raspberry Pi bug. As a side effect  of this I have been trying to learn Python to implement all the cool things the Raspberry Pi can do. I haven’t touched any programming languages since i was a student and barely scraped through Software Development 1, mainly because at the time I had no interest in C++, preferring micro controllers like the PIC and Intel 8051 where I could physically see the results of my code (making an LED blink is insanely satisfying).

As a result of learning Python I am starting to look for things to use it for in my daily working life. One thing I have to do regularly on the network is Null route naughty boys and girls causing DDoS attacks on our Cisco ASR 9000 routers. Quite often there is more than one address that requires to be nulled at a time, and it can be quite painful to copy and paste the commands in gedit or notepad, I decided that was a perfect opportunity to put my new-found python skills to use.


I came up with a brief list of objectives that I wanted the script to achieve :

  1. Ability to add multiple IP’s in a single line.
  2. Verify that the IP’s input are valid.
  3. Print the commands to get into configuration mode on the Cisco ASR 9000.
  4. Print the null route command for each IP input.
  5. Add a description with the date to each Null route added.
  6. Print the commands to remove the Null route at a later date.

IP’s are entered with a space separating each individual IP, I used the socket module of Python to validate the IP’s that are input and then placed the values into a list (array) that could be used in a for loop to print out the commands for each IP.

Sample output:

The code still needs a bit of work to get the formatting the way I would like it but it is functional at the moment.

Future Upgrades

There are a few things that I would like to add to the script but just haven’t found the time to do it as yet:

  1. Add option to select IOS or IOSXR output.
  2. Add step to correct invalid IP’s before moving on to print the configuration commands.
  3. Add option to automatically log into the router and apply the Null route.
  4. Tweak the format of the way the output is displayed to make it more readable.

Leave a Reply