| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # (c) Alea-Soluciones (Bifer) 2007, 2008, 2009 |
|---|
| 4 | # Licensed under GPL v3 or later, see COPYING for the whole text |
|---|
| 5 | |
|---|
| 6 | ''' |
|---|
| 7 | $Id:$ |
|---|
| 8 | $URL:$ |
|---|
| 9 | Alea-Soluciones (Bifer) (c) 2007 |
|---|
| 10 | Created: eferro - 29/7/2007 |
|---|
| 11 | ''' |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | import boscliutils |
|---|
| 15 | |
|---|
| 16 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52304 |
|---|
| 17 | # Helper class to create static methods (class methods) |
|---|
| 18 | class Callable: |
|---|
| 19 | def __init__(self, anycallable): |
|---|
| 20 | self.__call__ = anycallable |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class Ip: |
|---|
| 24 | def __init__(self, str_ip, prefix = 24): |
|---|
| 25 | self._ip = str_ip |
|---|
| 26 | self._prefix = prefix |
|---|
| 27 | self._netbits = 32-prefix |
|---|
| 28 | |
|---|
| 29 | self._netmaskbits = ((1<<(self._netbits))-1) ^ 0xFFFFFFFF |
|---|
| 30 | self._netmask = self.__int_to_ip(self._netmaskbits) |
|---|
| 31 | self._network = self.__int_to_ip((self.__ip_to_int(self._ip) & self._netmaskbits)) |
|---|
| 32 | self._broadcast = self.__int_to_ip((self.__ip_to_int(self._ip) | (self._netmaskbits ^ 0xFFFFFFFF))) |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | def to_int(ip): |
|---|
| 36 | r = 0 |
|---|
| 37 | try: |
|---|
| 38 | bytes_ip = ip.split('.') |
|---|
| 39 | r = r | long(bytes_ip[0]) << 24 |
|---|
| 40 | r = r | long(bytes_ip[1]) << 16 |
|---|
| 41 | r = r | long(bytes_ip[2]) << 8 |
|---|
| 42 | r = r | long(bytes_ip[3]) << 0 |
|---|
| 43 | except: |
|---|
| 44 | raise ValueError |
|---|
| 45 | return r |
|---|
| 46 | |
|---|
| 47 | def to_str(int_ip): |
|---|
| 48 | r = [] |
|---|
| 49 | r.append(str((int_ip & 0xFF000000) >> 24)) |
|---|
| 50 | r.append(str((int_ip & 0x00FF0000) >> 16)) |
|---|
| 51 | r.append(str((int_ip & 0x0000FF00) >> 8)) |
|---|
| 52 | r.append(str((int_ip & 0x000000FF) >> 0)) |
|---|
| 53 | return ".".join(r) |
|---|
| 54 | |
|---|
| 55 | def validate_netmask(str_netmask): |
|---|
| 56 | """Return True if the netmask is valid |
|---|
| 57 | """ |
|---|
| 58 | bit_activated = False |
|---|
| 59 | data = Ip.to_int(str_netmask) |
|---|
| 60 | |
|---|
| 61 | # Test the Ip 32 bits |
|---|
| 62 | for i in range(1,33): |
|---|
| 63 | # get the last bit |
|---|
| 64 | bit = ((data & 0xFFFFFFFF) & 0x00000001 ) |
|---|
| 65 | if bit_activated and bit == 0: |
|---|
| 66 | return False |
|---|
| 67 | if not bit_activated and bit == 1: |
|---|
| 68 | bit_activated = True |
|---|
| 69 | |
|---|
| 70 | # shift one position |
|---|
| 71 | data = data >> 1 |
|---|
| 72 | return True |
|---|
| 73 | |
|---|
| 74 | def num_ips(start, end): |
|---|
| 75 | return Ip.to_int(end) - Ip.to_int(start) |
|---|
| 76 | |
|---|
| 77 | def ips(start, end): |
|---|
| 78 | return map(Ip.to_str, range(Ip.to_int(start), Ip.to_int(end) + 1)) |
|---|
| 79 | |
|---|
| 80 | to_str = Callable(to_str) |
|---|
| 81 | to_int = Callable(to_int) |
|---|
| 82 | num_ips = Callable(num_ips) |
|---|
| 83 | ips = Callable(ips) |
|---|
| 84 | validate_netmask = Callable(validate_netmask) |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | def ip(self): |
|---|
| 88 | return self._ip |
|---|
| 89 | def prefix(self): |
|---|
| 90 | return self._prefix |
|---|
| 91 | def netmask(self): |
|---|
| 92 | return self._netmask |
|---|
| 93 | def network(self): |
|---|
| 94 | return self._network |
|---|
| 95 | def broadcast(self): |
|---|
| 96 | return self._broadcast |
|---|
| 97 | def ip_min(self): |
|---|
| 98 | return self.__int_to_ip(self.__ip_to_int(self._network) + 1) |
|---|
| 99 | def ip_max(self): |
|---|
| 100 | return self.__int_to_ip(self.__ip_to_int(self._broadcast) - 1) |
|---|
| 101 | def num_ips(self): |
|---|
| 102 | return self.__ip_to_int(self._broadcast) - self.__ip_to_int(self._network) - 1 |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | def __ip_to_int(self, ip): |
|---|
| 106 | r = 0 |
|---|
| 107 | try: |
|---|
| 108 | bytes_ip = ip.split('.') |
|---|
| 109 | r = r | long(bytes_ip[0]) << 24 |
|---|
| 110 | r = r | long(bytes_ip[1]) << 16 |
|---|
| 111 | r = r | long(bytes_ip[2]) << 8 |
|---|
| 112 | r = r | long(bytes_ip[3]) << 0 |
|---|
| 113 | except: |
|---|
| 114 | raise ValueError |
|---|
| 115 | return r |
|---|
| 116 | |
|---|
| 117 | def __int_to_ip(self, int_ip): |
|---|
| 118 | r = [] |
|---|
| 119 | r.append(str((int_ip & 0xFF000000) >> 24)) |
|---|
| 120 | r.append(str((int_ip & 0x00FF0000) >> 16)) |
|---|
| 121 | r.append(str((int_ip & 0x0000FF00) >> 8)) |
|---|
| 122 | r.append(str((int_ip & 0x000000FF) >> 0)) |
|---|
| 123 | return ".".join(r) |
|---|
| 124 | |
|---|