当前位置: 首页>>代码示例>>Python>>正文


Python IPAddress.netmask_bits方法代码示例

本文整理汇总了Python中netaddr.IPAddress.netmask_bits方法的典型用法代码示例。如果您正苦于以下问题:Python IPAddress.netmask_bits方法的具体用法?Python IPAddress.netmask_bits怎么用?Python IPAddress.netmask_bits使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在netaddr.IPAddress的用法示例。


在下文中一共展示了IPAddress.netmask_bits方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ipv4_to_cidr

# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import netmask_bits [as 别名]
 def ipv4_to_cidr(address, netmask=None, prefixlen=None):
     a = IPAddress(address)
     n = IPNetwork(a)
     if netmask:
         assert prefixlen is None, 'Cannot provide both netmask and prefixlen'
         m = IPAddress(netmask)
         assert m.is_netmask(), 'A valid netmask is required' 
         n.prefixlen = m.netmask_bits() 
     else:
         assert prefixlen, 'Provide either netmask or prefixlen'
         n.prefixlen = int(prefixlen)
     return str(n.cidr)
开发者ID:PublicaMundi,项目名称:ansible-plugins,代码行数:14,代码来源:network_address.py

示例2: ping

# 需要导入模块: from netaddr import IPAddress [as 别名]
# 或者: from netaddr.IPAddress import netmask_bits [as 别名]
    def ping(self, targets=list(), filename=str(), status=str(), notDNS=False,
        elapsed=False
    ):
        """
        Attempt to ping a list of hosts or networks (can be a single host)
        :param targets: List - Name(s) or IP(s) of the host(s).
        :param filename: String - name of the file containing hosts to ping
        :param status: String - if one of ['alive', 'dead', 'noip'] then only
        return results that have that status. If this is not specified,
        then all results will be returned.
        :param notDNS: Bolean - If True turn on use of -A option for fping for
        not use dns resolve names. Default is False.
        :param elapsed: Bolean - If True turn on use of -e option for fping for
        show elapsed time on return packets. Default is False.
        :return: Type and results depends on whether status is specified:
                 if status == '': return dict: {targets: results}
                 if status != '': return list: targets if targets == status
        """

        if targets and filename:
            raise SyntaxError("You must specify only one of either targets=[] "
                              "or filename=''.")
        elif not targets and not filename:
            raise SyntaxError("You must specify either a list of targets or "
                              "filename='', but not both.")
        elif filename:
            targets = self.read_file(filename)

        my_targets = {'hosts': [], 'nets': []}
        addresses = []

        # Check for valid networks and add hosts and nets to my_targets
        for target in targets:
            # Targets may include networks in the format "network mask", or,
            # a file could contain multiple hosts or IP's on a single line.
            if len(target.split()) > 1:
                target_items = target.split()
                for item in target_items:
                    try:
                        ip = IPAddress(item)
                        # If it is an IPv4 address or mask put in in addresses
                        if ip.version == 4:
                            addresses.append(str(ip))
                    except AddrFormatError:
                        # IP Address not detected, so assume it's a host name
                        my_targets['hosts'].append(item)
                    except ValueError:
                        # CIDR network detected
                        net = IPNetwork(item)
                        # Make sure it is a CIDR address acceptable to fping
                        if net.ip.is_unicast() and net.version == 4 and \
                                net.netmask.netmask_bits() in range(8, 31):
                            my_targets['nets'].append(target_items[0])
                        else:
                            msg = str(str(net) + ':Only IPv4 unicast addresses'
                                      ' with bit masks\n               '
                                      ' from 8 to 30 are supported.')
                            raise AttributeError(msg)
                # Iterate over the IP strings in addresses
                while len(addresses) > 1:
                    ip = IPAddress(addresses[0])
                    mask = IPAddress(addresses[1])
                    # Test to see if IP is unicast, and mask is an actual mask
                    if ip.is_unicast() and mask.is_netmask():
                        net = IPNetwork(str(ip) + '/' + str(
                            mask.netmask_bits()))
                        # Convert ip and mask to CIDR and remove from addresses
                        my_targets['nets'].append(str(net.cidr))
                        addresses.pop(0)
                        addresses.pop(0)
                    elif ip.is_unicast() and not ip.is_netmask():
                        # mask was not a mask so only remove IP and start over
                        my_targets['hosts'].append(str(ip))
                        addresses.pop(0)
                # There could be one more item in addresses, so check it
                if addresses:
                    ip = IPAddress(addresses[0])
                    if ip.is_unicast() and not ip.is_netmask():
                        my_targets['hosts'].append(addresses[0])
                        addresses.pop()
            # target has only one item, so check it
            else:
                try:
                    ip = IPAddress(target)
                    if ip.version == 4 and ip.is_unicast() and \
                            not ip.is_netmask():
                        my_targets['hosts'].append(target)
                    else:
                        msg = str(target + 'Only IPv4 unicast addresses are '
                                  'supported.')
                        raise AttributeError(msg)
                except AddrFormatError:
                    # IP Address not detected, so assume it's a host name
                    my_targets['hosts'].append(target)
                except ValueError:
                    # CIDR network detected
                    net = IPNetwork(target)
                    if net.ip.is_unicast() and net.version == 4 and \
                            net.netmask.netmask_bits() in range(8, 31):
                        my_targets['nets'].append(target)
#.........这里部分代码省略.........
开发者ID:rctorr,项目名称:fping.py,代码行数:103,代码来源:fping.py


注:本文中的netaddr.IPAddress.netmask_bits方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。