當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。