コード例 #1
0
def get_inner_ip():
    import getifaddrs
    from socket import AF_INET
    data = dict()
    for t in getifaddrs.getifaddrs():
        if t.name != b'lo' and t.family == AF_INET:
            name = t.name.decode()
            ip = t.addr[0]
            data[name] = ip
    return data
コード例 #2
0
ファイル: netifaces.py プロジェクト: gvsurenderreddy/pylans
def _linux_ifaddresses(ifname=None):
    ifaddrs = getifaddrs()
    
    result = {}
    for addrs in ifaddrs:
        # sometimes there are entries with no addresses
        # -- this was preventing adapters with mac addrs but no IPs from being
        # returned
#        if addrs.addr is None:
#            continue

        # filter
        if ifname is not None and ifname != addrs.name:
            continue
            
            
        # hw address?
        s = socket.socket(type=socket.SOCK_DGRAM)   
        ifr = addrs.name + '\0'*(32-len(addrs.name))    
        ifs = ioctl(s.fileno(), SIOCGIFHWADDR, ifr)
        mac = ifs[18:24]

        if ifname is None and addrs.name not in result.get('AF_LINK', {}):        
            d = result.get('AF_LINK', {})
            d[addrs.name] = ':'.join(['{0:02x}'.format(x) for x in struct.unpack('!6B', mac)])
            result['AF_LINK'] = d
        elif ifname is not None:
            result['AF_LINK'] = {'addr':':'.join(['{0:02x}'.format(x) for x in struct.unpack('!6B', mac)])}
        
        if addrs.addr is not None:
            # ip addresses
            addr = {'address':addrs.addr[0],
                    'netmask':addrs.netmask[0],
                    'broadcast':addrs.broadcast[0] if addrs.broadcast is not None else None}
        else:
            addr = {}
                
        #if we're collecting them all...
        fam = __protocols.get(addrs.family, addrs.family)
        if ifname is None:
            x = result.get(fam, {})
            y = x.get(addrs.name, [])
            y.append(addr)
            x[addrs.name] = y
            result[fam] = x
        else:
            x = result.get(fam, [])
            x.append(addr)
            result[fam] = x
        
    if len(result) == 0:
        raise ValueError, 'You must specify a valid interface name.'
        
    return result
コード例 #3
0
ファイル: __init__.py プロジェクト: Nuvyyo/script.tablo
def _getInterfacesBSD():
    #name flags family address netmask
    interfaces = []
    import getifaddrs
    for info in getifaddrs.getifaddrs():
        if info.family == 2:
            i = Interface()
            i.name = info.name
            i.ip = info.address
            i.mask = info.netmask
            interfaces.append(i)
    return interfaces
コード例 #4
0
def _getInterfacesBSD():
    #name flags family address netmask
    interfaces = []
    import getifaddrs
    for info in getifaddrs.getifaddrs():
        if info.family == 2:
            i = Interface()
            i.name = info.name
            i.ip = info.address
            i.mask = info.netmask
            interfaces.append(i)
    return interfaces
コード例 #5
0
ファイル: netifaces.py プロジェクト: bj0/pylans
def _linux_ifaddresses(ifname=None):
    result = {}
    for addrs in getifaddrs():
        # filter
        if ifname is not None and ifname != addrs.name:
            continue

        # hw address?
        s = socket.socket(type=socket.SOCK_DGRAM)
        ifr = addrs.name + '\0' * (32 - len(addrs.name))
        ifs = ioctl(s.fileno(), SIOCGIFHWADDR, ifr)
        mac = ifs[18:24]

        af_link = result.setdefault('AF_LINK', {})
        if ifname is None and addrs.name not in af_link:
            af_link[addrs.name] = ':'.join(['{0:02x}'.format(x) for x in struct.unpack('!6B', mac)])
        elif ifname is not None and 'addr' not in af_link:
            af_link['addr'] = ':'.join(['{0:02x}'.format(x) for x in struct.unpack('!6B', mac)])

        if addrs.addr is not None:
            # ip addresses
            addr = {'address': addrs.addr[0],
                    'netmask': addrs.netmask[0],
                    'broadcast': addrs.broadcast[0] if addrs.broadcast is not None else None}
        else:
            addr = {}

        # if we're collecting them all...
        fam = __protocols.get(addrs.family, addrs.family)
        if ifname is None:
            result.setdefault(fam, {}).setdefault(addrs.name, []).append(addr)
        else:
            result.setdefault(fam, []).append(addr)

    if len(result) == 0:
        raise ValueError('You must specify a valid interface name.')

    return result
コード例 #6
0
ファイル: ssdp.py プロジェクト: 0xfab-ri/pydlnadms
 def update_multicast_membership(self):
     from getifaddrs import getifaddrs
     import struct, errno
     try:
         for ifaddr in getifaddrs():
             if ifaddr.family == self.socket.family:
                 self.logger.debug(
                     'Adding SSDPResponder socket to multicast group on interface %r',
                     ifaddr.addr[0],)
                 mreqn = struct.pack(
                     '4s4si',
                     socket.inet_aton(SSDP_MCAST_ADDR),
                     socket.inet_aton(ifaddr.addr[0]),
                     0)
                 try:
                     self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreqn)
                 except socket.error as exc:
                     if exc.errno == errno.EADDRINUSE:
                         self.logger.debug(exc)
                     else:
                         self.logger.exception(exc)
     finally:
         self.events.add(self.update_multicast_membership, delay=15)
コード例 #7
0
ファイル: ssdp.py プロジェクト: upnp-dlna/pydlnadms
 def update_multicast_membership(self):
     from getifaddrs import getifaddrs
     import struct, errno
     try:
         for ifaddr in getifaddrs():
             if ifaddr.family == self.socket.family:
                 self.logger.debug(
                     'Adding SSDPResponder socket to multicast group on interface %r',
                     ifaddr.addr[0],
                 )
                 mreqn = struct.pack('4s4si',
                                     socket.inet_aton(SSDP_MCAST_ADDR),
                                     socket.inet_aton(ifaddr.addr[0]), 0)
                 try:
                     self.socket.setsockopt(socket.IPPROTO_IP,
                                            socket.IP_ADD_MEMBERSHIP, mreqn)
                 except socket.error as exc:
                     if exc.errno == errno.EADDRINUSE:
                         self.logger.debug(exc)
                     else:
                         self.logger.exception(exc)
     finally:
         self.events.add(self.update_multicast_membership, delay=15)
コード例 #8
0
ファイル: netifaces.py プロジェクト: gvsurenderreddy/pylans
def _linux_interfaces():
    ifaddrs = getifaddrs()
    
    return list(set(x.name for x in ifaddrs))
コード例 #9
0
ファイル: ssdp.py プロジェクト: 0xfab-ri/pydlnadms
 def notify_interfaces(self):
     from getifaddrs import getifaddrs, IFF_LOOPBACK
     from socket import AF_INET
     for ifaddr in getifaddrs():
         if ifaddr.family == AF_INET: #and not ifaddr.flags & IFF_LOOPBACK:
             yield ifaddr.family, ifaddr.addr
コード例 #10
0
ファイル: ssdp.py プロジェクト: upnp-dlna/pydlnadms
 def notify_interfaces(self):
     from getifaddrs import getifaddrs, IFF_LOOPBACK
     from socket import AF_INET
     for ifaddr in getifaddrs():
         if ifaddr.family == AF_INET:  #and not ifaddr.flags & IFF_LOOPBACK:
             yield ifaddr.family, ifaddr.addr
コード例 #11
0
        print(
            'Usage:\n\tpython SimpleTCPRedirector <host> <port> <remote host> <remote port>'
        )
        print(
            'Example:\n\tpython SimpleTCPRedirector localhost 8080 www.google.com 80'
        )
        sys.exit(0)

    localHost = sys.argv[1]
    localPort = int(sys.argv[2])
    targetHost = sys.argv[3]
    targetPort = int(sys.argv[4])

    serverSocket = None
    if localHost == 'guess':
        nifs = getifaddrs.getifaddrs()
        for ni in nifs:
            if ni.name == b'br-lan' and ni.family == 10 and ni.addr[0][
                    0:4] == 'fe80':
                #print("Setting localhost %s" % (ni.addr[0]))
                #print("calling socket(%s,%s,%s)" % (ni.family, socket.SOCK_STREAM, socket.SOL_TCP))
                serverSocket = socket.socket(ni.family, socket.SOCK_STREAM,
                                             socket.SOL_TCP)
                nsa = (ni.addr[0], localPort, ni.addr[2], ni.addr[3])
                serverSocket.bind(nsa)
                serverSocket.listen(5)

    else:
        #print("looking up %s %s" % (localHost, localPort))
        for res in socket.getaddrinfo(localHost, localPort, socket.AF_INET6,
                                      socket.SOCK_STREAM, socket.SOL_TCP):