Package Products :: Package ZenUtils :: Module IpUtil
[hide private]
[frames] | no frames]

Module IpUtil

source code

Classes [hide private]
IpAddressError
InvalidIPRangeError
Attempted to parse an invalid IP range.
Functions [hide private]
 
ipwrap(ip)
Convert IP addresses to a Zope-friendly format.
source code
 
ipunwrap(ip)
Convert IP addresses from a Zope-friendly format to a real address.
source code
 
ipstrip(ip)
strip interface off link local IPv6 addresses
source code
 
ipunwrap_strip(ip)
ipunwrap + strip interface off link local IPv6 addresses
source code
 
bytesToCanonIpv6(byteString)
SNMP provides a 16-byte index to table items, where the index represents the IPv6 address.
source code
 
isip(ip) source code
 
checkip(ip)
Check that an IPv4 address is valid.
source code
 
numbip(ip)
Convert a string IP to a decimal representation easier for calculating netmasks etc.
source code
 
ipToDecimal(ip)
Convert a string IP to a decimal representation easier for calculating netmasks etc.
source code
 
ipFromIpMask(ipmask)
Get just the IP address from an CIDR string like 1.1.1.1/24
source code
 
strip(ip)
Convert a numeric IP address to a string
source code
 
decimalIpToStr(ip)
Convert a decimal IP address (as returned by ipToDecimal) to a regular IPv4 dotted quad address.
source code
 
hexToBits(hex)
Convert hex netbits (0xff000000) to decimal netmask (8)
source code
 
hexToMask(hex)
Converts a netmask represented in hex to octets represented in decimal.
source code
 
maskToBits(netmask)
Convert string rep of netmask to number of bits
source code
 
bitsToMaskNumb(netbits)
Convert integer number of netbits to a decimal number
source code
 
bitsToDecimalMask(netbits)
Convert integer number of netbits to a decimal number
source code
 
bitsToMask(netbits)
Convert netbits into a dotted-quad subnetmask
source code
 
getnet(ip, netmask)
Deprecated in favour of decimalNetFromIpAndNet()
source code
 
decimalNetFromIpAndNet(ip, netmask)
Get network address of IP as string netmask as in the form 255.255.255.0
source code
 
getnetstr(ip, netmask)
Deprecated in favour of netFromIpAndNet()
source code
 
netFromIpAndNet(ip, netmask)
Return network number as string
source code
 
asyncNameLookup(address, uselibcresolver=True)
Turn IP addreses into names using deferreds
source code
 
asyncIpLookup(name)
Look up an IP based on the name passed in.
source code
 
generateAddrInfos(hostname)
generator for dicts from addrInfo structs for hostname
source code
 
getHostByName(hostname, preferredIpVersion=None)
Look up an IP based on the name passed in, synchronously.
source code
 
parse_iprange(iprange)
Turn a string specifying an IP range into a list of IPs.
source code
 
getSubnetBounds(ip)
Given a string representing the lower limit of a subnet, return decimal representations of the first and last IP of that subnet.
source code
 
ensureIp(ip)
Given a partially formed IP address this will return a complete Ip address with four octets with the invalid or missing entries replaced by 0
source code
Variables [hide private]
  __doc__ = """IpU...
  IP_DELIM = '..'
  INTERFACE_DELIM = '...'
Function Details [hide private]

bytesToCanonIpv6(byteString)

source code 

SNMP provides a 16-byte index to table items, where the index represents the IPv6 address. Return an empty string or the canonicalized IPv6 address.

>>> bytesToCanonIpv6( ['254','128','0','0','0','0','0','0','2','80','86','255','254','138','46','210'])
'fe80::250:56ff:fe8a:2ed2'
>>> bytesToCanonIpv6( ['253','0','0','0','0','0','0','0','0','0','0','0','10','175','210','5'])
'fd00::aaf:d205'
>>> bytesToCanonIpv6( ['hello','world'])
''
>>> bytesToCanonIpv6( ['253','0','0','0','0','0','0','0','0','0','0','0','10','175','210','5'])
'fd00::aaf:d205'

checkip(ip)

source code 

Check that an IPv4 address is valid. Return true or raise an exception(!)

>>> checkip('10.10.20.5')
True
>>> try: checkip(10)
... except IpAddressError, ex: print ex
10 is an invalid address
>>> try: checkip('10')
... except IpAddressError, ex: print ex
10 is an invalid address
>>> try: checkip('10.10.20.500')
... except IpAddressError, ex: print ex
10.10.20.500 is an invalid address
>>> checkip('10.10.20.0')
True
>>> checkip('10.10.20.255')
True

numbip(ip)

source code 

Convert a string IP to a decimal representation easier for calculating netmasks etc.

Deprecated in favour of ipToDecimal()

ipToDecimal(ip)

source code 

Convert a string IP to a decimal representation easier for calculating netmasks etc.

>>> ipToDecimal('10.10.20.5')
168432645L
>>> try: ipToDecimal('10.10.20.500')
... except IpAddressError, ex: print ex
10.10.20.500 is an invalid address

ipFromIpMask(ipmask)

source code 

Get just the IP address from an CIDR string like 1.1.1.1/24

>>> ipFromIpMask('1.1.1.1')
'1.1.1.1'
>>> ipFromIpMask('1.1.1.1/24')
'1.1.1.1'

strip(ip)

source code 

Convert a numeric IP address to a string

Deprecated in favour of decimalIpToStr()

decimalIpToStr(ip)

source code 

Convert a decimal IP address (as returned by ipToDecimal) to a regular IPv4 dotted quad address.

>>> decimalIpToStr(ipToDecimal('10.23.44.57'))
'10.23.44.57'

hexToBits(hex)

source code 

Convert hex netbits (0xff000000) to decimal netmask (8)

>>> hexToBits("0xff000000")
8
>>> hexToBits("0xffffff00")
24

hexToMask(hex)

source code 

Converts a netmask represented in hex to octets represented in decimal.

>>> hexToMask("0xffffff00")
'255.255.255.0'
>>> hexToMask("0xffffffff")
'255.255.255.255'
>>> hexToMask("0x00000000")
'0.0.0.0'
>>> hexToMask("trash")
'255.255.255.255'

maskToBits(netmask)

source code 

Convert string rep of netmask to number of bits

>>> maskToBits('255.255.255.255')
32
>>> maskToBits('255.255.224.0')
19
>>> maskToBits('0.0.0.0')
0

bitsToMaskNumb(netbits)

source code 

Convert integer number of netbits to a decimal number

Deprecated in favour of bitsToDecimalMask()

bitsToDecimalMask(netbits)

source code 

Convert integer number of netbits to a decimal number

>>> bitsToDecimalMask(32)
4294967295L
>>> bitsToDecimalMask(19)
4294959104L
>>> bitsToDecimalMask(0)
0L

bitsToMask(netbits)

source code 

Convert netbits into a dotted-quad subnetmask

>>> bitsToMask(12)
'255.240.0.0'
>>> bitsToMask(0)
'0.0.0.0'
>>> bitsToMask(32)
'255.255.255.255'

decimalNetFromIpAndNet(ip, netmask)

source code 

Get network address of IP as string netmask as in the form 255.255.255.0

>>> getnet('10.12.25.33', 24)
168564992L
>>> getnet('10.12.25.33', '255.255.255.0')
168564992L

netFromIpAndNet(ip, netmask)

source code 

Return network number as string

>>> netFromIpAndNet('10.12.25.33', 24)
'10.12.25.0'
>>> netFromIpAndNet('250.12.25.33', 1)
'128.0.0.0'
>>> netFromIpAndNet('10.12.25.33', 16)
'10.12.0.0'
>>> netFromIpAndNet('10.12.25.33', 32)
'10.12.25.33'

asyncIpLookup(name)

source code 

Look up an IP based on the name passed in. We use gethostbyname to make sure that we use /etc/hosts as mentioned above.

This hasn't been tested.

getHostByName(hostname, preferredIpVersion=None)

source code 

Look up an IP based on the name passed in, synchronously. Not using socket.gethostbyname() because it does not support IPv6.

preferredIpVersion should equal something like socket.AF_INET or socket.AF_INET6

parse_iprange(iprange)

source code 

Turn a string specifying an IP range into a list of IPs.

Parameters:
  • iprange (str
    >>> parse_iprange('10.0.0.1-5')
    ['10.0.0.1', '10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5']
    >>> parse_iprange('10.0.0.2-5')
    ['10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5']
    >>> parse_iprange('10.0.0.1')
    ['10.0.0.1']
    >>> try: parse_iprange('10.0.0.1-2-3')
    ... except InvalidIPRangeError: print "Invalid"
    Invalid
    >>> parse_iprange('fd00::aaf:d201-d203')
    ['fd00::aaf:d201', 'fd00::aaf:d202', 'fd00::aaf:d203']
    ) - The range string, in the format '10.0.0.a-b'

getSubnetBounds(ip)

source code 

Given a string representing the lower limit of a subnet, return decimal representations of the first and last IP of that subnet.

0 is considered to define the beginning of a subnet, so x.x.x.0 represents a /24, x.x.0.0 represents a /16, etc. An octet of 0 followed by a non-zero octet, of course, is not considered to define a lower limit.

>>> map(decimalIpToStr, getSubnetBounds('10.1.1.0'))
['10.1.1.0', '10.1.1.255']
>>> map(decimalIpToStr, getSubnetBounds('10.1.1.1'))
['10.1.1.1', '10.1.1.1']
>>> map(decimalIpToStr, getSubnetBounds('10.0.1.0'))
['10.0.1.0', '10.0.1.255']
>>> map(decimalIpToStr, getSubnetBounds('0.0.0.0'))
['0.0.0.0', '255.255.255.255']
>>> map(decimalIpToStr, getSubnetBounds('10.0.0.0'))
['10.0.0.0', '10.255.255.255']
>>> map(decimalIpToStr, getSubnetBounds('100.0.0.0'))
['100.0.0.0', '100.255.255.255']
>>> map(decimalIpToStr, getSubnetBounds('::100.0.0.0'))
['100.0.0.0', '100.255.255.255']

ensureIp(ip)

source code 

Given a partially formed IP address this will return a complete Ip address with four octets with the invalid or missing entries replaced by 0

@param ip partially formed ip (will strip out alpha characters) @return valid IP address field

>>> from Products.ZenUtils.IpUtil import ensureIp
>>> ensureIp('20')
'20.0.0.0'
>>> ensureIp('2000')
'0.0.0.0'
>>> ensureIp('10.175.X')
'10.175.0.0'
>>> ensureIp('10.0.1')
'10.0.1.0'
>>>

Variables Details [hide private]

__doc__

Value:
"""IpUtil

IPv4 and IPv6 utility functions

Internally, IPv6 addresses will use another character rather than ':' \
as an underscore
is representable in URLs.  Zope object names *MUST* be okay in URLs.

...