Router or PC interface testing with Python and not using object oriented concepts of python
As we all know being in software field, why python is considered as a powerful programming language. Especially python can be a very useful tool for automating software QA Test cases. In this article, I am using python without object oriented concepts to automatically check my pc IP addresses.
With Windows we have "ipconfig" command to check ip addresses on your wifi or lan interface.
As part of below python code, I am using "re" module to use regular expression of python to parse and get ipv4 and ipv6 addresses on my pc interface.
In the same way one can use this logic to get ipv4 and ipv6 addresses on router or cpe equipment and check the cli cases. Please post below your requirement for getting the interface tested. We can prepare a logic for the same.
This article is used to showcase how easily we can get the interface of any equipment tested for assignment of ip addresses.
Python code as below :
import re # python re module to use regular experessions in python
import sys # python sys module, however this module is not used in this code
import subprocess #python subprocess module to execute cli commands on linux and windows
result = subprocess.run(['ipconfig'], stdout=subprocess.PIPE) # ipconfig command executed on windows.
result=result.stdout # Store the output in variable result
result=str(result) # type cast result variable as string for easy parsing later in code
result=result.split("\r\n") # Split the result variable for newline for easy parsing
ipv4_addr=[] # Declare list varibale for storing ipv4 addresses
ipv6_addr=[] # Declare list variable for storing ipv6 addresses
link_local=[] # Declare list variable for storing link local ipv6 addresses
for i in result: # iterate through the list variable result
if re.match(".*[0-9]+.[0-9]+.[0-9]+.[0-9]+.*",i): # regaular expression to match ipv4 addresses
ipv4_addr=(re.findall("[0-9]+.[0-9]+.[0-9]+.[0-9]+",i))
if re.match(".*[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+.*",i): # regular expression to match ipv6 addresses
ipv6_addr=re.findall("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+",i)
if re.match(".*fe80.*",i): # regular expression to match link local addresses of ipv6
link_local=re.findall("fe80::[0-9a-f:]+",i)
for i in ipv4_addr: # iterate ipv4_addr list variable for ipv4 addresses on the interface
Recommended by LinkedIn
if re.match("[0-9]+.[0-9]+.[0-9]+.[0-9]+",i) and not re.match("255.*",i) and not re.match(".*:.*",i):
j=i.split(".")
if not j[3] =="1":
print("Your ip address is :" + i)
else:
print("Your Default gateway is :" + i)
if re.match("255.*",i):
print("Your subnet mask is " + i)
print("IPv6 addresses on your interface for communication with global scope")
for i in ipv6_addr: # iterate ipv6_addr list variable for ipv6 addresses on the interface
print(i)
print("IPv6 addresses on your interface for communication with link local scope")
for i in link_local: # iterate link_local list variable for link local ipv6 addresses on the interface
print(i)
Output of above code is as below :
>python ipconfig.py
Your ip address is :192.168.129.14
Your subnet mask is 255.255.254.0
Your Default gateway is :192.168.128.1
IPv6 addresses on your interface for communication with global scope
Due to security reasons i have to hide my pc global ipv6 addresses.....
IPv6 addresses on your interface for communication with link local scope
fe80::1f41:a537:93af:4f95
fe80::22b8:2bff:fe13:2c22