Creating a Python Script from Scratch – Rapid Prototyping

By on 05/07/2015.

python-poweredIf you recall from the first post in this series we are going to work through the process of creating a script that will automatically scan VMware vSphere and check for any functionality that may be degraded due to blocked ports. In this post we will be focusing on quickly creating a prototype to get us started. In order to get the most out of the post I highly recommend manually replicating the code on your own machine.

When working with Python one of your most valuable resources is going to be The Python Standard Library which houses built-in modules that provide access to various system functionality. These modules are a large part of the ‘batteries included’ philosophy of Python and allow you to quickly add complex functionality to your code. An easily digestible example of utilizing the standard library is printing the mathematical constant pi. Instead of manually defining pi you would simply use the code below and your off to the races.

https://gist.github.com/anonymous/6249b8678b62c354aa75

For our script we are going to be taking advantage of the socket standard library which will allow us to create TCP communications between a client (our script) and a server (a vSphere Object). The first thing we need to do is create a new socket which just creates a new communication session. Since we are going to be creating a new socket for each port we check we are going to assign the relevant code to a variable (sock) for easy reuse.

https://gist.github.com/anonymous/99fe112c5e9e3b0a92f2

Now that we have the socket created let’s connect to a vSphere object and check whether or not a specific port is blocked. To accomplish this we are going to utilize the connect_ex function of the socket module which will return a value of 0 if the connection was successful. To check for the returned value we will assign the relevant code to a variable (result) and then use an if statement to let us know if a port is open or blocked.

https://gist.github.com/drew-russell/f560b2cbc7514f1464b0

With just 7 lines of code we now have a working port scan prototype up and running!

https://gist.github.com/anonymous/356d5ec391571f818223

With the prototype up and running let’s start adding some basic functionality to the script. If you take a look at the code above you’ll notice that we have two variables, an IP address and a port number, that are required for the script to run. Instead of having to manually change the code each time we want to check a new vSphere Object or port, let’s have Python do the heavy lifting for us.

Since the vSphere Object IP address will be different for each environment, we are going to utilize the raw_input function to prompt the end user to enter the relevant IP address. Ports on the other hand will remain constant so we are going to utilize a list, which is simply a set of comma separated values stored between brackets, to keep track of all of the relevant ports. For the sake of saving space I’m not going to include all of the ports that vCenter utilizes until the final version is posted at the end.

Once the new IP address (raw_input) and vCenter (a list) Python objects are created, we can update the result variable to reference the new information.

https://gist.github.com/anonymous/c4d7ec8ae200908dd374

If we run the code in its current form we are going to receive an error message related to the vcenter_ports variable being a list and not a individual port. To fix this we are going to have Python iterate through the vcenter_ports list and pull out the individual ports one at a time.

Since we now have multiple ports that we are checking let’s also update the message that we are printing to the console. For string concatenation will use {} as a placeholder and then specify the specific value by adding .format after the string.

You may also notice that the script takes a few minutes to check just four ports. Once we populate the full list of ports and check multiple vSphere Objects at once the script may take an hour plus to run which would take away its usefulness. To speed things up we can change the default socket timeout value to two seconds.

https://gist.github.com/anonymous/ab87b847a2d269890534

After adding that last bit of functionality we now have a finished prototype which looks like:

https://gist.github.com/anonymous/4cfe2486ee80c92c3aba

The output from the script will look similar to:

https://gist.github.com/anonymous/f1dfa1661c06c81c7c62

If you worked through the code you should now be familiar with Python variables, lists, if statements, for loops, modules, gathering user input and more! As you continue to work through creating scripts you’ll find that these few items will make up the backbone of your code. For the next post in the series we will be adding the ability to check multiple vSphere objects at once by introducing functions to our code. We will also be working with dictionaries to add more detailed on the ports that are blocked.

Stay tuned!

Drew Russell
As a member of the World Wide Technology (WWT) Professional Services Data Center team, Drew Russell has the opportunity to see first hand what is currently being discussed and deployed in the environments of some of the largest organizations in the country. When not traveling, you can often find Drew in WWTs Advanced Technology Center (ATC) vetting the latest product offerings from the OEMs.

No Comments