Assigning Static IP with Python – 2

I recently took another crack at my station IP changer Python program and made some improvements to make the code more ‘pythonic’ by that I mean abstracting the code and trying to have it reusable as possible. For anyone that didn’t read my first post I wanted a simple way to change my work laptops Ethernet static IP address. I frequently visit many different sites in my work as a telecommunications technician and need to change my static IP to be in the same range as the voice vlan.

The first iteration, while functional was not very efficient. It was essentially a series of if and elif statements to check user input against a hard coded series of site names and IP addresses. Then using the python subprocess module to send arguments to the netsh command which is used to change IP settings(among other things). With over 50 sites this ended up being a very long bit of code that was VERY redundant. It looked something like this:

sitename=input(“Enter the name of the site”).
if sitename = “AAA”:
do xyz
elif sitename = “BBB”:
do zxy
elif sitename =”CCC”
do xzy
elif sitename = etc etc
etc
etc…

So Python basically needs to check the user input of EVERY single elif statement to see if it matches the conditional argument. This results in the script running a lot of unnecessary code thereby increasing the time for the script to complete its intended function. In a program of this scope the additional time to complete the operation is not really noticeable to a human however

I decided to instead use a dictionary map function which for anyone unfamiliar with Python data structures uses a key:value pair to retrieve values from buckets the values are stored in. A dictionary can contain strings, ints, floats, tuples and lists. For my purposes I needed to use lists within the dictionary as my key would have multiple values stored inside it. The dictionary format would be as follows:

mydict={
‘breakfast’: [“spam”, “bacon”, “eggs”],
‘lunch’: [“sandwich”, “hotdog”, “pizza”],
‘dinner’: [“lasagna”, “spaghetti”, “casserole”]
}

In the example above ‘breakfast’ is the key for the values ‘spam’ ‘bacon’ ‘eggs’

Using this data type I created the dictionary sitename_map and stored the IP address information as the value and used the site name as the key:

sitename_map = {

‘site 1’: [‘192.168.1.2’, ‘255.255.255.0’, ‘192.168.1.1’
‘site 2’: [‘192.168.1.3’, ‘255.255.255.0’, ‘192.168.1.1’,
‘site 3’: [‘192.168.1.4’, ‘255.255.255.0’,
}

Similar to the first iteration of the program the script will take user input asking for the site name and save it to a variable called ‘sitename’

The next variable is called ‘static_ip’ and it is assigned the values of dictionary sitename_map and the contents of the first variable ‘sitename’

static_ip = sitename_map[sitename]

This new variable static_ip will take the user input and find it in the dictionary. Which we will then use to pass to the subprocess module to activate the netsh command.

The program is on GitHub if you’d like to try the code out yourself!

Leave a Reply

Your email address will not be published. Required fields are marked *