You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

146 lines
3.6 KiB

#!/usr/bin/env python
import string
from copy import deepcopy
class ipv4addr():
"""Class for IPv4 address manipulation.\n\n"""\
"""Initialization:\n"""\
"""\tfrom addr import ipv4addr\n"""\
"""\tmyAddress = ipv4addr(str, str) # for example: myAddress = ipv4addr("192.168.0.1", "255.255.255.0")\n"""\
"""or:\n"""\
"""\tmyAddress = ipv4addr(str, int) # for example: myAddress = ipv4addr("192.168.0.1", 24)\n\n"""\
"""Functions:\n"""\
"""\tnetwork() # returns subnet address\n"""\
"""\tinMyNet(ipv4addr) # returns True or False\n"""\
"""\t # True - if the specified ipv4addr object belongs to the network\n"""\
"""\t # False - if the specified ipv4addr object not belongs to the network\n\n"""\
"""Variables:\n"""\
"""\tcidr # Subnet mask in cidr notation\n\n"""\
"""ipv4addr class object is an iterator.\n"""\
"""Examples of use ipv4addr object as iterator:\n"""\
"""\t>>> myAddress = ipv4addr("192.168.0.1", 24)\n"""\
"""\t>>> print(myAddress[0])\n"""\
"""\t192\n"""\
"""\t>>> print(myAddress[1])\n"""\
"""\t168\n"""\
"""\t>>> myAddress[2] = 1\n"""\
"""\t>>> print(myAddress)\n"""\
"""\t192.168.1.1/24\n"""
def __init__(self, addrstr = '0.0.0.0', netmask = 32):
octets = addrstr.split('.')
self._addr = None
self._setAddr([int(octets[0]),
int(octets[1]),
int(octets[2]),
int(octets[3])])
if type(netmask) == str:
self.cidr = self._strToMask(netmask)
else:
self.cidr = netmask
self.printMask = True
def _strToMask(self, netmask):
octets = netmask.split('.')
cidr = 0
for i in octets:
octet = int(i)
if octet > 0:
while((octet & 128) / 128):
cidr += 1
octet<<=1
return(cidr)
def _setAddr(self, octets):
self._addr = octets[0] * pow(256, 3) + octets[1] * pow(256, 2) + octets[2] * pow(256, 1) + octets[3] * pow(256, 0)
def _getOctet(self, key):
octet = self._addr
for i in xrange(3 - key):
octet >>= 8
octet = octet % 256
return(octet)
def _setOctet(self, key, value):
octets=[]
for i in xrange(4):
if i == key:
octets.append(value)
else:
octets.append(self._getOctet(i))
self._setAddr(octets)
def _mask(self):
mask = 0
for i in xrange(self.cidr):
mask = (mask << 1) + 1
for i in xrange(32-self.cidr):
mask <<= 1
return(mask)
def __str__(self):
result = str(self._getOctet(0)) + '.' + str(self._getOctet(1)) + '.' + str(self._getOctet(2)) + '.' + str(self._getOctet(3))
if self.printMask == True:
result+='/%s' % self.cidr
return(result)
def __repr__(self):
return(self.__str__())
def __iter__(self):
return(self)
def __getitem__(self, key):
return(self._getOctet(key))
def __setitem__(self, key, value):
self._setOctet(key, value)
def __len__(self):
return(4)
def __lt__(self, other):
if self._addr < other._addr:
return True
else:
return False
def __le__(self, other):
if self._addr <= other._addr:
return True
else:
return False
def __eq__(self, other):
if self._addr == other._addr:
return True
else:
return False
def __ne__(self, other):
if self._addr != other._addr:
return True
else:
return False
def __gt__(self, other):
if self._addr > other._addr:
return True
else:
return False
def __ge__(self, other):
if self._addr >= other._addr:
return True
else:
return False
def network(self):
network = deepcopy(self)
network._addr = self._addr & self._mask()
network.cidr = self.cidr
return(network)
def inMyNet(self, address):
if (address._addr & self._mask()) == self.network()._addr:
return(True)
else:
return(False)