简体   繁体   English

在python中确定CONNECTED接口(linux)的IP地址

[英]Determine IP address of CONNECTED interface (linux) in python

On my linux machine, 1 of 3 network interfaces may be actually connected to the internet. 在我的linux机器上,3个网络接口中的1个可能实际上已连接到Internet。 I need to get the IP address of the currently connected interface, keeping in mind that my other 2 interfaces may be assigned IP addresses, just not be connected. 我需要获取当前连接的接口的IP地址,请记住,可能为我的其他2个接口分配了IP地址,只是没有连接。

I can just ping a website through each of my interfaces to determine which one has connectivity, but I'd like to get this faster than waiting for a ping time out. 我可以通过我的每个界面对网站进行ping操作,以确定哪个具有连接能力,但是与等待ping超时相比,我希望这样做更快。 And I'd like to not have to rely on an external website being up. 而且我不想不必依赖外部网站的建立。

Update: 更新:

All my interfaces may have ip addresses and gateways. 我所有的接口可能都有ip地址和网关。 This is for an embedded device. 这是针对嵌入式设备的。 So we allow the user to choose between say eth0 and eth1 . 因此,我们允许用户在eth0eth1之间进行选择。 But if there's no connection on the interface that the user tells us to use, we fall back to say eth2 which (in theory) will always work. 但是,如果用户告诉我们使用的接口上没有任何连接,我们可以回过头来说eth2 (理论上将一直有效)。

So what I need to do is first check if the user's selection is connected and if so return that IP. 因此,我需要做的是首先检查用户的选择是否已连接,如果已连接,则返回该IP。 Otherwise I need to get the ip of eth2 . 否则,我需要获取eth2的IP。 I can get the IPs of the interfaces just fine, it's just determining which one is actually connected. 我可以很好地获得接口的IP,这只是确定实际连接的接口。

如果系统的默认网关是可靠的,请从route -n的输出中获取,其中包含" UG " (请注意空格)还将包含网关的IP和活动接口的接口名称。

the solution is here : http://code.activestate.com/recipes/439093-get-names-of-all-up-network-interfaces-linux-only/ 解决方案在这里: http : //code.activestate.com/recipes/439093-get-names-of-all-up-network-interfaces-linux-only/


import fcntl
import array
import struct
import socket
import platform
"""
global constants.  If you don't like 'em here,
move 'em inside the function definition.
"""
SIOCGIFCONF = 0x8912
MAXBYTES = 8096

def localifs():
    """
    Used to get a list of the up interfaces and associated IP addresses
    on this machine (linux only).

    Returns:
    List of interface tuples.  Each tuple consists of
    (interface name, interface IP)
    """
    global SIOCGIFCONF
    global MAXBYTES

    arch = platform.architecture()[0]

    # I really don't know what to call these right now
    var1 = -1
    var2 = -1
    if arch == '32bit':
        var1 = 32
        var2 = 32
    elif arch == '64bit':
        var1 = 16
        var2 = 40
    else:
        raise OSError("Unknown architecture: %s" % arch)

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    names = array.array('B', '\0' * MAXBYTES)
    outbytes = struct.unpack('iL', fcntl.ioctl(
        sock.fileno(),
        SIOCGIFCONF,
        struct.pack('iL', MAXBYTES, names.buffer_info()[0])
        ))[0]

    namestr = names.tostring()
    return [(namestr[i:i+var1].split('\0', 1)[0], socket.inet_ntoa(namestr[i+20:i+24])) \
            for i in xrange(0, outbytes, var2)]


print localifs()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM