Monday, October 17, 2016

Validating IP Address By Using Python (Conditional Statement)

Splitting IP Address string variable post defines the normal way how to deal with the variables and splitting them into different items. The same splitting can be used by using conditional statement if-else with while loop. This provides more simplicity for network engineers to shorten the python code.

Syntax of If-Else Statement:
if expression:

If expression is FALSE, in that case complier will move to the ELIF part. If ELIF becomes false also, the compiler will execute else block. The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

while True:
  ip_address = raw_input("Enter IP Address")
  ip_address_check = ip_address.split('.')

  if (len(ip_address_check) == 4) and (1 <= int(ip_address_check[0]) <= 223) and (int(ip_address_check[0]) != 127) and (int(ip_address_check[0]) != 169 or int(ip_address_check[1]) != 254) and (0 <= int(ip_address_check[1]) <= 255 and 0 <= int(ip_address_check[2]) <= 255 and 0 <= int(ip_address_check[3]) <= 255):

  print "\nThe IP address is INVALID! Please retry!\n"

print "Shivlu Jain It's Good to go with the given IP Address"

For more info on programming skill read Do I need to be programmer before learning Automation, SDN and NFV technologies?

Click Here To Read Rest Of The Post...

Friday, October 14, 2016

Splitting IP Address string variable in Python

Python variable types post defines the different type’s variable in python. In this post, I will be covering the basis stuff which is used to convert a string into an integer and use it for further validation. The main intent of this article is to write few lines of code which can ask user to input the “IP Address Field” and split the string into different octets by using split function. Once this is done, after that convert the string to integer type so that we can check whether the given input has length of 4 or not (In case of IP Address we have a.b.c.d).

The same lines of codes can become more meaningful and useful when it is applied with if-else or with any kind of loop statements.
The given code is plain code which is used for basic understanding.
# raw_input is used whenever we want user to provide the inputs. Storing the user values in ip_address variable
ip_address = raw_input( "Enter an IP Address = ")

# Check the whether the entered IP Address is string or integer. Python has inbuilt function called type which is useful to check the variable type.
ip_address_check = type(ip_address)

print "Entered IP Address type is %s" %ip_address_check

print "\nNow we are convering this IP Address to integer\n"

print "Lets split the IP Address into four octet by using dot.\n"

# split function is used divide the string into smaller one with delimiter of dot. If user enters, in this case whenever DOT is seen, string is divided into four different parts by using split function ip_address_split = ip_address.split(".")

# By using split function, we can store the given user inputs into four arrays. Ist element is pointing towards first location which is 0.
print "The first octet is %s" %ip_address_split[0] #----> Output will be 192
print "The second octet is %s"%ip_address_split[1] #----> Output will be 168
print "The Third octet is %s"%ip_address_split[2] #----> Output will be 1
print "The Fourth octet is %s"%ip_address_split[3] #----> Output will be 1

print "\nLets check whether the length is equal to four or not - Octet Validation\n"

# len is length function which is used to check the length of string after spliting it using split function. We can use the conditional statements to check it. In this case we are not using anything.

ip_address_length = len(ip_address_split)

print "The length of IP Address is %d" %ip_address_length

print "\nCurrently everything is in string, lets convert this IP Address to integer\n"

# int is integer function which is used to convert the first string element into integer. Once this is done, we can simply compare it with different IP Addresses classes and check that this IP address is falling in which class. We are not using it here but it can be used by using conditional statements.

ip_address_split_convert = int(ip_address_split[0])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion ist octet, type is changed to integrer %s" %ip_address_split_convert_check

ip_address_split_convert = int(ip_address_split[1])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion second octet, type is changed to integrer %s" %ip_address_split_convert_check

ip_address_split_convert = int(ip_address_split[2])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion third octet, type is changed to integrer %s" %ip_address_split_convert_check

ip_address_split_convert = int(ip_address_split[3])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion fourth octet, type is changed to integrer %s" %ip_address_split_convert_check

Click Here To Read Rest Of The Post...

Sunday, October 2, 2016

Python Variable Types

In last post “How To Start With Python For SDN” gives clear direction how to start with Python. In this post I am trying to cover the basic variable types which are must to know for every network engineer who wants to learn Python.

Variables are used to store values in specific memory locations. It means whenever you create a value python reserves some space in memory. Memory allocation happens based on the defined variable type. In Python we have different type of variables as given below:
1. Integer
2. Float
3. String
4. Tuple
5. List
6. Dictionary

Declare the name of the variable and use the assignment operator = to assign the value. We do not declare the variable type. The value assigned to the variable determines the type Integer, Floats, String, Lists, Dictionaries, and more types.

Let’s understand this with the help of below example. We have define a variable called ip_address and assign “” in double quotes. Now by typing type(ip_address) function help us to understand that the declared variable ip_address is of string type.

The same way we can declare the integers types by not putting them in quote as per below output.

List is collection of items, items are ordered, separated by commas and enclosed with in square brackets.

The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1.

Will cover more in the upcoming posts.

Click Here To Read Rest Of The Post...