The best place for network developers

Before I write a script

How to write a script and what we should consider ?

Every time I have to automate specific kind of a job I write down and answer project specific questions, which help me to prepare algorithm and general idea, how a new solution will work.

Of course, this list can be easily extended during algorithm preparation. A good approach is to prepare small working solution and extend it, every time you need it, by adding new functionality. Building small peaces and extracting specific functions will help in troubleshooting, reduce downtime during writing a code and of course, give a better planning and estimation of time when whole project will be finished.

Assume, that our network consists 20 routers and 100 switches – all devices from the same vendor.
We would like to know, how many free space is there on flash – i.e. for upload new firmware (I omit the fact, that probably there is a special vendor depended OID to get this information). My idea is to show you which factors can be included during design phase.
In this scenario I will use below questions:

1) What is the scope?
Login to a device – on error, throw exception. Log error, so we will know which device we can’t connect to. To those devices, engineer should login and check free space or the best solution is to fix device configuration so script can login next time.
  1. use command to show free space and parse output to get proper value. This value will be saved to variable, so it will be much easier to parse it in python.

  2. disconnect from a device

  3. parse output

  4. write a device name or IP and amount of free space to our database.

2) Is it one time task? Or will it be recurring task?
It is obvious that if it is a repeated task, we should have a script.
Creating a script will be helpful i.e. in patching process and work can be done much faster.
3) How long does it take to finish this job using a script or do it manually?
Consider what is more profitable for you. For a few devices it will be much faster to do job manually and save outputs.

In scenario where we have a complete list of devices and we use TACACS, we can assume doing job using a script, it will take:

  • login takes 8 seconds
  • send command – takes 3 seconds
  • find outputs and copy output – takes 4 seconds
  • paste in excel and save file – takes 5 seconds

Depends on many factors login to device can take 20 seconds.
Overall time needed to finish task: 20 seconds x 120 devices = 2400 seconds / 60 seconds = 40 minutes

It’s worth to mention that human will be frustrated, bored and will do mistakes.
Adding a few checks + time for coffee and the time needed to finish this task will be 60 minutes.

Doing the same with script:

  • login – 4 seconds
  • send commands – 1 second
  • find outputs and copy output – 2 second
  • paste in excel and save file – 2 seconds

Overall time needed to finish task: 9 seconds x 120 devices = 1080 seconds / 18 seconds = 18 minutes.

This time also can be reduced, when we run script using threads or when we run multiple instances of script but using different set of data.
Let’s compare those two time calculations.
We will save 22 minutes doing the job using script in the same way as human. Including brakes and other factors which influence on our productivity, we will save about 42 minutes.
For the first time, we should include time spent on writing code. This is dependent from your programming skills, but next time you will only save time.

4) Script for one kind of device or many different devices?
The most important thing is whether we can use the same commands for whole devices in our network.
I. e. for Cisco routers and switches we can use the same command to check free space on flash.
It is a good idea in multi vendor environment to split tasks for a few smaller and write vendor dependent classes or functions.
5) Will it be used by me or also by other people?
Why should we care about it? If only I used the script I could assign my login and password in a variable during execution and after  that I could just delete them.
In case, when we would like to share a code among team it would be nice to include a function in a script which asks executing engineer for credentials or checks credentials/passwords in central password repository.
If it is allowed, having separate account which is dedicated to run scripts is highly recommended, so in any tracking changes mechanism, it will be easy to debug/troubleshoot who did a change.
6) will it be used by me or also by other people?
What kind of data do I need at the beginning and where will I get them from?
Who will prepare data and how can we access them?
Before I start writing I will try to have/get data from database or simple csv file (treated as DB), however getting data directly from DB is not a problem.
What we should have there? From my point of view always the best is to have there: name of device, IP because both can be used to connection. Any other required fields can be added if you feel that they will be useful for any reason.  
In example we can also add information if device is a router or a switch, but in this scenario with Cisco devices we will not use it. However adding those fields allows us to split work for two different engineers – one will do router, second switches.
7) based on which factors will I make a decision?
Will we take any decisions based on gathered information from device?

In my example no, but we can expend it i.e. in the case where there is enough free space, we can download new firmware, when there is no space left we can run i.e. the cleaning script.

8) what will be the output and what we would like to log?
As the output I would like to have a list of devices with following information:
name of device, ip, login successful, free space.
We already have two first values, so let’s focus on the next two. In the column called ‘login successful’ we will put true if we can login to device, otherwise we put false. Using these information we can easily create report, where we can visualize status of our task. In this report we can include number of all devices compared to number of devices that we could log in to. Going further devices that we logged into how many of them have not enough free space for new firmware.
In output file the column ‘free space’ we will put amount of free space.



As you can see, before you start writing your script it is always a good idea to spend some time on consideration which factors are important for you and based on which factor you will make a decision. Writing it down and creating schema of algorithm will significantly reduce the time of thinking during development. Also it will give you a good overview in planing improvements/extensions for your script, so in future it will be much easier to implement changes and  maintain the code.
In scripts where we are going to combine many different functionalities is a good idea to learn object oriented programming and write a good class. At the beginning you will spend more time learning OOP  but in the future maintaining the code will be much easier.


Access Point – automatic report

Do you remember when last time local IT / your colleague / HelpDesk / anyone asked you for gathering a report about some devices which are connected in your network? Do you remember all the boring stuff that you had to do? Login into 5, 10, 15… 50 devices and gathering information like IP, port number, description, device name, mac address?

Building lab environment

Every time I have to automate specific kind of a job I write down and answer project specific questions, which help me to prepare algorithm and general idea, how a new solution will work.

Of course, this list can be easily extended during algorithm preparation. A good approach is to prepare small working solution and extend it, every time you need it, by adding new functionality. Building small peaces and extracting specific functions will help in troubleshooting, reduce downtime during writing a code and of course, give a better planning and estimation of time when whole project will be finished.

Automatic interface configuration

For all of us entering same commands / text into CLI becomes boring after some time. Do you remember your fascination when you created a VLAN 10, connected two switches together and they pinged each other? Yes, probably for most of us were very enthusiastic that time. But after some time copying same command, creating same VLANs can become a little bit boring. What can we do to make it faster, better and less prone to mistakes?