Network Automation

Using if, elif & else Statements

< – means it is less than

> – means it is greater than

<= – means it is less than or equal to.

>= – means it is more than or equal to.

Example below is basically

2. The else Statement

2.1 Using else for Alternative Actions

Adding ‘else‘ to the ‘if’ statement, allows you to return another action. I have two examples where if the device is reachable then return with a messgage to confirm, if not then return a message to say it isnt reachable.

3.1 Using elif for Multiple Conditions

‘elif’ statements are used for multiple options if the conditions require more options. In this example, if the link/cpu is less than 50 print it is degraded, if it is equal to or less than 50 an it is equal to or below 75 then print is is moderate. Anything else print it is high.

4. Nested if Statements

Nested statements are useful when you want to check multuple arguements before making a decision, such as measuring the CPU and Memory then taking action.

4.1 Using Nested if Statements

The example below is basically checking if the device is reachable AND the Memory utilisation, if it is below 70 and device is reachable then display “Device is reachable and memory is OK”, However if it is reachable but 70 or more then display “Device is reachable but memory is HIGH”. The last else statement basically displays the message “Device is fucked” if it is not reachable.

Another example is if we changed the ‘ram’ measurements to <= (highlighted in the example below) then the script would display “Device is reachable and memory is OK” instead of the first example above.

You can use if-elif-else statements to monitor multiple devices.

The example below is calling from a dictonary with key value pairs such as the ‘Router-MCR’ which is the key and ‘up’ which is the value. Remember a dictionary will always have KEY VALUE pairs.

In the ‘for’ loop script the loop is called ‘device’ which is the key (Router -MCR) and ‘status’ is the value (up) which is used to store temporarily from the dictionary called from the devices.items().

Breaking this down, .items() is calling the dictionary and my example the dictionary is ‘devices’. So I call the ‘devices’.items() dictionary, once this is called the if-elif-else statement needs a place to store that dictionary whilst it will begin running in a loop until it is completed. So to hold the dictonary from ‘devices’.items() this then stores the dictionary in a temp holding space called ‘device’ (key) ‘status’ (value), the if-elif-else statement will run in a loop until it goes through all the dictionary key value pairs.

‘device’ and ‘status’ could be anything if you wanted to name it ‘hello’ and ‘test’ you can do.

In a nutshell ‘device’ in the ‘for’ loop will hold the ‘key’ (Router-MCR) and ‘value’ will hold the ‘value’status’ (up), if the value (status) is ‘up’ display:

Router-MCR is up and working.

elif section checks the key(‘device’) (‘Switch-Lon’) and if the value(‘status’) (‘ok’) then display the following:

Switch-Lon is running OK.

else section basically if it doesn’t match anything else in the value(‘status’) then just displaythe following

Firewall-HK is down.

The loop will check the key all the time until the value matches to whatever you have in the script, so it will check Router-MCR to see if it is up, if this matches then display the message, same goes for Switch-Lon and Firewall-HK, it will check to see if the value is ‘up’ or ‘ok’ until it finds a match. if it doesn’t match then display it is down.

Another example of for loops:

6. Combining Conditional Logic with Network Automation Tools

6.1 Example with Dynamic Status Check

You can obtain a list of devices and their statuses then write a script to produce an output of the device status.

Example below is we have a list of devices which we need to check the status of the devices and then print out whatever the device is and what action is needed based on the status.

‘def’ is a function, functions are used to save time instead of re-writing code you can call it again to if repetition is needed. The example below, we defined a function called ‘get_device_status’ and when this is called we have a placeholder called ‘device_name’ and when the dictionary ‘statuses’ is needed it will then take the key ‘Router1’ and allows the function to utilise the key. Without the ‘device_name’ parameter, the function wouldn’t know how to call the key in the dictionary.

In other words, it would look like the below screenshot, the “Router1” in Green is equivalent to ‘device_name’.



The below screenshot finishes the function, this is where it will call out the key ‘device_name’ ‘Router1’

statuses.get(): – the ‘get()’ method accesses the value that is part of the key i.e ‘Router1’ and ‘up’

‘device_name’ – as mentioned before, it looks into the ‘statuses’ dictionary to pull the key value pair. For example, if device_name is "Router1", the get() method will look for "Router1" in the statuses dictionary.

“unknown” – The “unknown” section of the function is used as a backup should none of the dictionary key values are matched, this will give a cleaner output of the result of the code. Instead of stating “Status of Router5 is unknown” it will be “Status of unknown_device is unknown”. However, I have tried this in my lab and no error pops up when i remove this “unknown” section.

The rest of the code is to create a list of devices and these do not need to match the dictionary, because in the list of ‘devices’ it will run the list against the dictionary, and if the status isn’t listed or matching the and if-elif statement it will fall into the ‘else’ statement which is unknown.

Another example is adding another device which will require another ‘elif’ statement. I have purposely removed the “unknown” in the script where you return the status of the devices in the dictionary. to see what the issue would be, no problem as the ‘else’ catches it in the for loop section.