ipcalc Documentation Release 0.5.1 Wijnand Modderman July 14, 2015 Contents 1 Example Usage 3 2 Bugs/Features 5 3 Download 3.1 ipcalc IP subnet calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 7 4 Indices and tables 11 Python Module Index 13 i ii ipcalc Documentation, Release 0.5.1 This module allows you to perform IP subnet calculations, there is support for both IPv4 and IPv6 CIDR notation. Contents 1 ipcalc Documentation, Release 0.5.1 2 Contents CHAPTER 1 Example Usage >>> import ipcalc >>> for x in ipcalc.Network('172.16.42.0/30'): ... print str(x) ... 172.16.42.0 172.16.42.1 172.16.42.2 172.16.42.3 >>> subnet = ipcalc.Network('2001:beef:babe::/48') >>> print str(subnet.network()) 2001:beef:babe:0000:0000:0000:0000:0000 >>> print str(subnet.netmask()) ffff:ffff:ffff:0000:0000:0000:0000:0000 >>> '192.168.42.23' in Network('192.168.42.0/24') True >>> long(IP('fe80::213:ceff:fee8:c937')) 338288524927261089654168587652869703991L 3 ipcalc Documentation, Release 0.5.1 4 Chapter 1. Example Usage CHAPTER 2 Bugs/Features You can issue a ticket in GitHub: https://github.com/tehmaze/ipcalc/issues 5 ipcalc Documentation, Release 0.5.1 6 Chapter 2. Bugs/Features CHAPTER 3 Download Get your copy of ipcalc from pypi: http://pypi.python.org/pypi/ipcalc 3.1 ipcalc IP subnet calculator Note: BSD License 3.1.1 About This module allows you to perform network calculations. 3.1.2 Changelog Date 2009-04-11 2009-03-23 2007-10-26 2007-10-25 Changes Added docstrings. Added IPv4 short-hand form support, thanks to VeXocide. Added IPv6 support, as well as a lot of other functions, refactored the calculations. Initial writeup, because I could not find any other workable implementation. 3.1.3 Todo Todo: • add CLI parser 3.1.4 References References: • http://www.estoile.com/links/ipv6.pdf • http://www.iana.org/assignments/ipv4-address-space • http://www.iana.org/assignments/multicast-addresses • http://www.iana.org/assignments/ipv6-address-space 7 ipcalc Documentation, Release 0.5.1 • http://www.iana.org/assignments/ipv6-tla-assignments • http://www.iana.org/assignments/ipv6-multicast-addresses • http://www.iana.org/assignments/ipv6-anycast-addresses 3.1.5 Thanks I wish to thank the following people for their input: • Bastiaan (trbs) • Peter van Dijk (Habbie) • Hans van Kranenburg (Knorrie) • Jeroen Habraken (VeXocide) • Torbjörn Lönnemark class ipcalc.IP(ip, mask=None, version=0) Represents a single IP address. Parameters ip (IP or str or long or int) – the ip address >>> localhost = IP("127.0.0.1") >>> print localhost 127.0.0.1 >>> localhost6 = IP("::1") >>> print localhost6 0000:0000:0000:0000:0000:0000:0000:0001 bin() Full-length binary representation of the IP address. >>> ip = IP("127.0.0.1") >>> print ip.bin() 01111111000000000000000000000001 clone() Return a new <IP> object with a copy of this one. >>> ip = IP('127.0.0.1') >>> ip.clone() <ipcalc.IP object at 0x...> hex() Full-length hexadecimal representation of the IP address. >>> ip = IP("127.0.0.1") >>> print ip.hex() 7f000001 info() Show IANA allocation information for the current IP address. >>> ip = IP("127.0.0.1") >>> print ip.info() CLASS A 8 Chapter 3. Download ipcalc Documentation, Release 0.5.1 to_ipv4() Convert (an IPv6) IP address to an IPv4 address, if possible. Only works for IPv4-compat (::/96) and 6-to-4 (2002::/16) addresses. >>> ip = IP('2002:c000:022a::') >>> print ip.to_ipv4() 192.0.2.42 to_ipv6(type=‘6-to-4’) Convert (an IPv4) IP address to an IPv6 address. >>> ip = IP('192.0.2.42') >>> print ip.to_ipv6() 2002:c000:022a:0000:0000:0000:0000:0000 to_tuple() Used for comparisons. version() IP version. >>> ip = IP("127.0.0.1") >>> print ip.version() 4 class ipcalc.Network(ip, mask=None, version=0) Network slice calculations. Parameters • ip (IP or str or long or int) – network address • mask (int or str) – netmask >>> localnet = Network('127.0.0.1/8') >>> print localnet 127.0.0.1 broadcast() Broadcast address. >>> localnet = Network('127.0.0.1/8') >>> print localnet.broadcast() 127.255.255.255 has_key(ip) Check if the given ip is part of the network. Parameters ip (IP or str or long or int) – the ip address >>> net = Network('192.0.2.0/24') >>> net.has_key('192.168.2.0') False >>> net.has_key('192.0.2.42') True host_first() First available host in this subnet. host_last() Last available host in this subnet. 3.1. ipcalc IP subnet calculator 9 ipcalc Documentation, Release 0.5.1 in_network(other) Check if the given IP address is within this network. netmask() Network netmask derived from subnet size. >>> localnet = Network('127.0.0.1/8') >>> print localnet.netmask() 255.0.0.0 network() Network address. >>> localnet = Network('127.128.99.3/8') >>> print localnet.network() 127.0.0.0 size() Number of ip’s within the network. >>> net = Network('192.0.2.0/24') >>> print net.size() 256 10 Chapter 3. Download CHAPTER 4 Indices and tables • genindex • modindex • search 11 ipcalc Documentation, Release 0.5.1 12 Chapter 4. Indices and tables Python Module Index i ipcalc, 7 13 ipcalc Documentation, Release 0.5.1 14 Python Module Index Index B bin() (ipcalc.IP method), 8 broadcast() (ipcalc.Network method), 9 C clone() (ipcalc.IP method), 8 H has_key() (ipcalc.Network method), 9 hex() (ipcalc.IP method), 8 host_first() (ipcalc.Network method), 9 host_last() (ipcalc.Network method), 9 I in_network() (ipcalc.Network method), 9 info() (ipcalc.IP method), 8 IP (class in ipcalc), 8 ipcalc (module), 7 N netmask() (ipcalc.Network method), 10 Network (class in ipcalc), 9 network() (ipcalc.Network method), 10 S size() (ipcalc.Network method), 10 T to_ipv4() (ipcalc.IP method), 8 to_ipv6() (ipcalc.IP method), 9 to_tuple() (ipcalc.IP method), 9 V version() (ipcalc.IP method), 9 15