ipwithpython

Introduction to IP networking and manipulation using Python APIs.

contents

Overview

What is IP address ?

IP address versions and categories

How to Find Your IP Address?

  • Right click on the Start menu and open Command Prompt.
  • Type ipconfig and hit enter.
  • You will see IP address of your computer as below.
  • Press “Command + Spacebar” to open Spotlight Search.
  • Type “network utility” and open Network Utility app.
  • Select your network name under “Info” tab.
  • You can find the IP address of your Mac here.
IP Address in Mac

IP addresses: Networks and hosts

Subnet mask

IP Network classes

  • Class A networks use a default subnet mask of 255.0.0.0 and have 0–127 as their first octet. The address 10.52.36.11 is a class A address. Its first octet is 10, which is between 1 and 126, inclusive.
  • Class B networks use a default subnet mask of 255.255.0.0 and have 128–191 as their first octet. The address 172.16.52.63 is a class B address. Its first octet is 172, which is between 128 and 191, inclusive.
  • Class C networks use a default subnet mask of 255.255.255.0 and have 192–223 as their first octet. The address 192.168.123.132 is a class C address. Its first octet is 192, which is between 192 and 223, inclusive.

Subnetting

Default gateways

ipaddress — IPv4/IPv6 manipulation library

Creating Address/Network/Interface objects

In [3]: #import the library
In [3]: import ipaddress
# creating an IPv4 and IPv6 Addresses
ip4 = ipaddress.IPv4Address("192.168.1.1")
ip6 = ipaddress.IPv6Address("2001:db8::1")

Defining Networks

#defining ipv4 and v6 network
ipaddress.ip_network('192.0.2.0/24')
ipaddress.ip_network('2001:db8::0/96')

Inspecting IP address Networks

In [6]: addr4 = ipaddress.ip_address('192.0.2.1')
In [7]: addr4
Out[7]: IPv4Address('192.0.2.1')
In [11]: addr6 = ipaddress.ip_address('2001:db8::1')
In [12]: addr6
Out[12]: IPv6Address('2001:db8::1')
#you can just call the variable/ the variable.version
In [14]: addr6.version
Out[14]: 6
In [15]: host4 = ipaddress.ip_interface('192.0.2.1/24')In [16]: host4
Out[16]: IPv4Interface('192.0.2.1/24')
In [18]: host6 = ipaddress.ip_interface('2001:db8::1/96')
In [19]: host6
Out[19]: IPv6Interface('2001:db8::1/96')
In [14]: net4 = ipaddress.ip_network('192.0.2.0/24')
In [15]: net4
Out[15]: IPv4Network('192.0.2.0/24')
In [16]: net4.num_addresses
Out[16]: 256
In [22]: net6 = ipaddress.ip_network('2001:db8::0/96')
In [23]: net6
Out[23]: IPv6Network('2001:db8::/96')
In [24]: net6.num_addresses
Out[24]: 4294967296
#ipv4 addresses
In [26]: net4 = ipaddress.ip_network('192.0.2.0/24')
In [27]: for x in net4.hosts():
...: print(x)
...:
192.0.2.1
192.0.2.2
192.0.2.3
192.0.2.4
192.0.2.5
192.0.2.6
192.0.2.7
192.0.2.8
192.0.2.9
192.0.2.10

...
192.0.2.252
192.0.2.253
192.0.2.254
#ipv6 addresses
In [26]: net6 = ipaddress.ip_network('2001:db8::0/96')
In [27]: for x in net6.hosts():
...: print(x)
2001:db8::20:3993
2001:db8::20:3994
2001:db8::20:3995
2001:db8::20:3996
2001:db8::20:3997
2001:db8::20:3998
2001:db8::20:3999
2001:db8::20:399a
2001:db8::20:399b
................. to be continued 😋
net4 = ipaddress.ip_network('192.0.2.0/24')
>>> net4.netmask
IPv4Address('255.255.255.0')
>>> net4.hostmask
IPv4Address('0.0.0.255')
netV6 = ipaddress.ip_network('2001:db8::0/96')
>>> net6.netmask
IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::')
>>> net6.hostmask
IPv6Address('::ffff:ffff')
#Exploding and compressing the IPs
In [5]: addr6 = ipaddress.ip_address('2001:db8::1')
In [7]: addr6.exploded
Out[7]: '2001:0db8:0000:0000:0000:0000:0000:0001'
In [10]: addr6.compressed
Out[10]: '2001:db8::1'
#Exploding and compressing the networks
In [15]: netV6 = ipaddress.ip_network('2001:db8::0/96')
In [16]: netV6.exploded
Out[16]: '2001:0db8:0000:0000:0000:0000:0000:0000/96'
In [17]: netV6.compressed
Out[17]: '2001:db8::/96'

In [6]: addr4 = ipaddress.ip_address('192.0.2.1')
In [8]: addr4.exploded
Out[8]: '192.0.2.1'
.........work out the rest using the example above.💻

Networks as lists of Addresses

>>> net4[1]
IPv4Address('192.0.2.1')
>>> net4[-1]
IPv4Address('192.0.2.255')
>>> net6[1]
IPv6Address('2001:db8::1')
>>> net6[-1]
IPv6Address('2001:db8::ffff:ffff')
if address in network:
# do something
>>> addr4 = ipaddress.ip_address('192.0.2.1')
>>> addr4 in ipaddress.ip_network('192.0.2.0/24')
True
>>> addr4 in ipaddress.ip_network('192.0.3.0/24')
False

Comparisons

>>> ipaddress.ip_address('192.0.2.1') < ipaddress.ip_address('192.0.2.2')
True

Using IP Addresses with other modules

>>> addr4 = ipaddress.ip_address('192.0.2.1')
>>> str(addr4)
'192.0.2.1'
>>> int(addr4)
3221225985

Getting more detail when instance creation fails

>>> ipaddress.ip_address("192.168.0.256")
Traceback (most recent call last):
...
ValueError: '192.168.0.256' does not appear to be an IPv4 or IPv6 address
>>> ipaddress.IPv4Address("192.168.0.256")
Traceback (most recent call last):
...
ipaddress.AddressValueError: Octet 256 (> 255) not permitted in '192.168.0.256'
>>> ipaddress.ip_network("192.168.0.1/64")
Traceback (most recent call last):
...
ValueError: '192.168.0.1/64' does not appear to be an IPv4 or IPv6 network
>>> ipaddress.IPv4Network("192.168.0.1/64")
Traceback (most recent call last):
...
ipaddress.NetmaskValueError: '64' is not a valid netmask
try:
network = ipaddress.IPv4Network(address)
except ValueError:
print('address/netmask is invalid for IPv4:', address)

References:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store