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


Python netifaces.gateways方法代码示例

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


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

示例1: configure

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def configure(protocol, port, pipes, interface):
  remove_all()
  reactor.addSystemEventTrigger('after', 'shutdown', remove_all)

  # gets default (outward-facing) network interface (e.g. deciding which of
  # eth0, eth1, wlan0 is being used by the system to connect to the internet)
  if interface == "auto":
    interface = netifaces.gateways()['default'][netifaces.AF_INET][1]
  else:
    if interface not in netifaces.interfaces():
      raise ValueError("Given interface does not exist.", interface)

  add(protocol, port, interface)
  manager = libnetfilter_queue.Manager()

  manager.bind(UP_QUEUE, packet_handler(manager, pipes.up))
  manager.bind(DOWN_QUEUE, packet_handler(manager, pipes.down))

  reader = abstract.FileDescriptor()
  reader.doRead = manager.process
  reader.fileno = lambda: manager.fileno
  reactor.addReader(reader) 
开发者ID:google,项目名称:packet-queue,代码行数:24,代码来源:nfqueue.py

示例2: start_automatic

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def start_automatic(self, scan_filter: GatewayScanFilter):
        """Start GatewayScanner and connect to the found device."""
        gatewayscanner = GatewayScanner(self.xknx, scan_filter=scan_filter)
        gateways = await gatewayscanner.scan()

        if not gateways:
            raise XKNXException("No Gateways found")

        gateway = gateways[0]
        if gateway.supports_tunnelling and \
                scan_filter.routing is not True:
            await self.start_tunnelling(gateway.local_ip,
                                        gateway.ip_addr,
                                        gateway.port,
                                        self.connection_config.auto_reconnect,
                                        self.connection_config.auto_reconnect_wait)
        elif gateway.supports_routing:
            bind_to_multicast_addr = get_os_name() != "Darwin"  # = Mac OS
            await self.start_routing(gateway.local_ip, bind_to_multicast_addr) 
开发者ID:XKNX,项目名称:xknx,代码行数:21,代码来源:knxip_interface.py

示例3: get_iface

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_iface():
    '''
    Grabs an interface so we can grab the IP off that interface
    '''
    try:
        iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
    except:
        ifaces = []
        for iface in netifaces.interfaces():
            # list of ipv4 addrinfo dicts
            ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])

            for entry in ipv4s:
                addr = entry.get('addr')
                if not addr:
                    continue
                if not (iface.startswith('lo') or addr.startswith('127.')):
                    ifaces.append(iface)

        # Just get the first interface
        iface = ifaces[0]

    return iface 
开发者ID:DanMcInerney,项目名称:msf-autoshell,代码行数:25,代码来源:msf-autoshell.py

示例4: get_iface

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_iface():
    '''
    Gets the right interface
    '''
    try:
        iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
    except:
        ifaces = []
        for iface in netifaces.interfaces():
            # list of ipv4 addrinfo dicts
            ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])

            for entry in ipv4s:
                addr = entry.get('addr')
                if not addr:
                    continue
                if not (iface.startswith('lo') or addr.startswith('127.')):
                    ifaces.append(iface)

        iface = ifaces[0]

    return iface 
开发者ID:DanMcInerney,项目名称:smb-autopwn,代码行数:24,代码来源:smb-autopwn.py

示例5: get_iface

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_iface(self):
        '''
        Gets the interface with an IP address connected to a default gateway
        '''
        try:
            iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
        except:
            ifaces = []
            for iface in netifaces.interfaces():
                # list of ipv4 addrinfo dicts
                ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])

                for entry in ipv4s:
                    addr = entry.get('addr')
                    if not addr:
                        continue
                    if not (iface.startswith('lo') or addr.startswith('127.')):
                        ifaces.append(iface)

            iface = ifaces[0]

        return iface 
开发者ID:DanMcInerney,项目名称:MsfWrapper,代码行数:24,代码来源:MsfWrapper.py

示例6: run

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def run(self):

        while 1:
            gws = netifaces.gateways()
            cgw = gws['default'][netifaces.AF_INET][0]
            if not homenet.gateway:
                homenet.gateway = cgw
            else:
                if homenet.gateway != cgw:
                    utils.reconfigure_network(homenet.gateway, cgw)
                    homenet.gateway = cgw
                    try:
                        with lock:
                            utils.save_pkl_object(homenet, "homenet.pkl")
                    except Exception as e:
                        log.debug('FG-ERROR ' + str(e.__doc__) + " - " + str(e))
                    utils.reboot_appliance()
                else:
                    pass
            time.sleep(10) 
开发者ID:A3sal0n,项目名称:FalconGate,代码行数:22,代码来源:config.py

示例7: best_remote_address

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def best_remote_address():
    """
    Returns the "best" non-localback IP address for the local host, if
    possible.  The "best" IP address is that bound to either the default
    gateway interface, if any, else the arbitrary first interface found.

    IPv4 is preferred, but IPv6 will be used if no IPv4 interfaces/addresses
    are available.
    """
    default_gateway   = net.gateways().get("default", {})
    default_interface = default_gateway.get(net.AF_INET,  (None, None))[1] \
                     or default_gateway.get(net.AF_INET6, (None, None))[1] \
                     or net.interfaces()[0]

    interface_addresses = net.ifaddresses(default_interface).get(net.AF_INET)  \
                       or net.ifaddresses(default_interface).get(net.AF_INET6) \
                       or []

    addresses = [
        address["addr"]
            for address in interface_addresses
             if address.get("addr")
    ]

    return addresses[0] if addresses else None 
开发者ID:nextstrain,项目名称:cli,代码行数:27,代码来源:view.py

示例8: get_iface

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_iface():
    '''
    Gets the right interface for Responder
    '''
    try:
        iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
    except:
        ifaces = []
        for iface in netifaces.interfaces():
            # list of ipv4 addrinfo dicts
            ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])

            for entry in ipv4s:
                addr = entry.get('addr')
                if not addr:
                    continue
                if not (iface.startswith('lo') or addr.startswith('127.')):
                    ifaces.append(iface)

        iface = ifaces[0]

    return iface 
开发者ID:DanMcInerney,项目名称:msf-autopwn,代码行数:24,代码来源:msf-autopwn.py

示例9: get_default_gateway_ip

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_default_gateway_ip():
    """Return the default gateway IP."""
    gateways = netifaces.gateways()
    defaults = gateways.get("default")
    if not defaults:
        return

    def default_ip(family):
        gw_info = defaults.get(family)
        if not gw_info:
            return
        addresses = netifaces.ifaddresses(gw_info[1]).get(family)
        if addresses:
            return addresses[0]["addr"]

    return default_ip(netifaces.AF_INET) or default_ip(netifaces.AF_INET6) 
开发者ID:maas,项目名称:maas,代码行数:18,代码来源:snappy.py

示例10: get_machine_default_gateway_ip

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_machine_default_gateway_ip():
    """Return the default gateway IP for the machine."""
    gateways = netifaces.gateways()
    defaults = gateways.get("default")
    if not defaults:
        return

    def default_ip(family):
        gw_info = defaults.get(family)
        if not gw_info:
            return
        addresses = netifaces.ifaddresses(gw_info[1]).get(family)
        if addresses:
            return addresses[0]["addr"]

    return default_ip(netifaces.AF_INET) or default_ip(netifaces.AF_INET6) 
开发者ID:maas,项目名称:maas,代码行数:18,代码来源:ipaddr.py

示例11: get_default_gateway

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_default_gateway(interface="default"):
    if sys.version_info < (3, 0, 0):
        if type(interface) == str:
            interface = unicode(interface)
    else:
        if type(interface) == bytes:
            interface = interface.decode("utf-8")

    if platform.system() == "Windows":
        if interface == "default":
            default_routes = [r for r in get_ipv4_routing_table()
                              if r[0] == '0.0.0.0']
            if default_routes:
                return default_routes[0][2]

    try:
        gws = netifaces.gateways()
        if sys.version_info < (3, 0, 0):
            return gws[interface][netifaces.AF_INET][0].decode("utf-8")
        else:
            return gws[interface][netifaces.AF_INET][0]
    except:
        # This can also mean the machine is directly accessible.
        return None 
开发者ID:StorjOld,项目名称:pyp2p,代码行数:26,代码来源:lib.py

示例12: _get_my_ipv4_address

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def _get_my_ipv4_address():
    """Figure out the best ipv4
    """
    LOCALHOST = '127.0.0.1'
    gtw = netifaces.gateways()
    try:
        interface = gtw['default'][netifaces.AF_INET][1]
    except (KeyError, IndexError):
        LOG.info('Could not determine default network interface, '
                 'using 127.0.0.1 for IPv4 address')
        return LOCALHOST

    try:
        return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
    except (KeyError, IndexError):
        LOG.info('Could not determine IPv4 address for interface %s, '
                 'using 127.0.0.1',
                 interface)
    except Exception as e:
        LOG.info('Could not determine IPv4 address for '
                 'interface %(interface)s: %(error)s',
                 {'interface': interface, 'error': e})
    return LOCALHOST 
开发者ID:openstack,项目名称:oslo.utils,代码行数:25,代码来源:netutils.py

示例13: default_ipv4

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def default_ipv4():
    try:
        default_if = netifaces.gateways()['default'][netifaces.AF_INET][1]
        return netifaces.ifaddresses(default_if)[netifaces.AF_INET][0]['addr']
    except:
        traceback.print_exc()
    return None 
开发者ID:masmu,项目名称:pulseaudio-dlna,代码行数:9,代码来源:network.py

示例14: get_gateways

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_gateways():
    gateway_dict = {}
    gws = netifaces.gateways()
    for gw in gws:
        try:
            gateway_iface = gws[gw][netifaces.AF_INET]
            gateway_ip, iface = gateway_iface[0], gateway_iface[1]
            gw_list =[gateway_ip, iface]
            gateway_dict[gw]=gw_list
        except:
            pass
    return gateway_dict 
开发者ID:funkandwagnalls,项目名称:pythonpentest,代码行数:14,代码来源:msfrpc_smb.py

示例15: get_networks

# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import gateways [as 别名]
def get_networks(gateways_dict):
    networks_dict = {}
    for key, value in gateways.iteritems():
        gateway_ip, iface = value[0], value[1]
        hwaddress, addr, broadcast, netmask = get_addresses(iface)
        network = {'gateway': gateway_ip, 'hwaddr' : hwaddress, 'addr' : addr, '                                                                                        broadcast' : broadcast, 'netmask' : netmask}
        networks_dict[iface] = network
    return networks_dict 
开发者ID:funkandwagnalls,项目名称:pythonpentest,代码行数:10,代码来源:ssh_login.py


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