Python dictionary creation

Python dictionary creation is very useful just like list creation for further reference in the python script.

Below Code for extracting IP address from ipconfig and then store the data as dictionary. Dictionary creation and update are highlighted in bold.

Code :

import re # python re module to use regular expressions 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 variable 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): # regular 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)

ip_config={} #Dict creation

for i in ipv4_addr: # iterate ipv4_addr list variable for ipv4 addresses on the interface

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)

ip_config['ipaddress']=i #Dict update for ip address

else:

print("Your Default gateway is :" + i)

ip_config['gateway']=i #Dict update for gateway

if re.match("255.*",i):

print("Your subnet mask is " + i)

ip_config['subnet_mask']=i #Dict update for Subnet mask

print("IPv6 addresses on your interface for communication with global scope")

j=1

for i in ipv6_addr: # iterate ipv6_addr list variable for ipv6 addresses on the interface

print(i)

ip_config['global_ipv6'+str(j)]=i #dict update for global ipv6 address

j=j+1

print("IPv6 addresses on your interface for communication with link local scope")

j=1

for i in link_local: # iterate link_local list variable for link local ipv6 addresses on the interface

print(i)

ip_config['linklocal_ipv6'+str(j)]=i #dict update for link local address ipv6 address

j=j+1

print(ip_config)


Output of the code :

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

hidden for security purpose

IPv6 addresses on your interface for communication with link local scope

fe80::1f41:a537:93af:4f95

fe80::22b8:2bff:fe13:2c22

{'ipaddress': '192.168.129.14', 'subnet_mask': '255.255.254.0', 'gateway': '192.168.128.1', 'global_ipv61': 'hidden', 'global_ipv62': 'hidden', 'linklocal_ipv61': 'fe80::1f41:a537:93af:4f95', 'linklocal_ipv62': 'fe80::22b8:2bff:fe13:2c22'}

Good luck



To view or add a comment, sign in

More articles by Sathyanarayana Venkataramanappa

Others also viewed

Explore content categories