Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5420 > unrolled thread

Re: Get the IP address of WIFI interface

Started byNeal Becker <ndbecker2@gmail.com>
First post2011-05-15 07:04 -0400
Last post2011-05-16 22:57 +0300
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Get the IP address of WIFI interface Neal Becker <ndbecker2@gmail.com> - 2011-05-15 07:04 -0400
    Re: Get the IP address of WIFI interface Anssi Saari <as@sci.fi> - 2011-05-16 22:57 +0300

#5420 — Re: Get the IP address of WIFI interface

FromNeal Becker <ndbecker2@gmail.com>
Date2011-05-15 07:04 -0400
SubjectRe: Get the IP address of WIFI interface
Message-ID<mailman.1591.1305457483.9059.python-list@python.org>
Far.Runner wrote:

> Hi python experts:
> There are two network interfaces on my laptop: one is 100M Ethernet
> interface, the other is wifi interface, both are connected and has an ip
> address.
> The question is: How to get the ip address of the wifi interface in a python
> script without parsing the output of a shell command like "ipconfig" or
> "ifconfig"?
> 
> OS: Windows or Linux
> 
> F.R

Here's some useful snippits for linux:

def get_default_if():
    f = open('/proc/net/route')
    for i in csv.DictReader(f, delimiter="\t"):
        if long(i['Destination'], 16) == 0:
            return i['Iface']
    return None

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

[toc] | [next] | [standalone]


#5534

FromAnssi Saari <as@sci.fi>
Date2011-05-16 22:57 +0300
Message-ID<vg3pqni5sgm.fsf@pepper.modeemi.fi>
In reply to#5420
Neal Becker <ndbecker2@gmail.com> writes:

> Here's some useful snippits for linux:
>
> def get_default_if():
>     f = open('/proc/net/route')
>     for i in csv.DictReader(f, delimiter="\t"):
>         if long(i['Destination'], 16) == 0:
>             return i['Iface']
>     return None
>
> def get_ip_address(ifname):
>     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>     return socket.inet_ntoa(fcntl.ioctl(
>         s.fileno(),
>         0x8915,  # SIOCGIFADDR
>         struct.pack('256s', ifname[:15])
>     )[20:24])

One possible solution in Linux is asking NetworkManager, if it's in
use. It knows which interfaces are active and what kind they are (LAN,
WLAN, WWAN etc.) NetworkManager communicates via dbus and even
includes python example scripts. So here's my scriptlet based on
NetworkManager example nm-state.py. This one prints out all active
devices and their type and IP address. Easily modified to print only
WLAN types.

import dbus, socket, struct

bus = dbus.SystemBus()

proxy = bus.get_object("org.freedesktop.NetworkManager", 
"/org/freedesktop/NetworkManager")
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")

# Get device-specific state
devices = manager.GetDevices()
for d in devices:
    dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
    prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")

    # Get the device's current state and interface name
    state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
    name = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
    ifa = "org.freedesktop.NetworkManager.Device"
    type = prop_iface.Get(ifa, "DeviceType")
    addr = prop_iface.Get(ifa, "Ip4Address")

    # and print them out
    if state == 8:   # activated
        addr_dotted = socket.inet_ntoa(struct.pack('<L', addr))

        s = "Device %s is activated and has type %s and address %s" 
        print s % (name, type, addr_dotted)
    else:
        print "Device %s is not activated" % name


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web