Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin3!goblin.stu.neva.ru!newsfeed2.funet.fi!newsfeeds.funet.fi!news.cc.tut.fi!not-for-mail From: Anssi Saari Newsgroups: comp.lang.python Subject: Re: Get the IP address of WIFI interface Date: Mon, 16 May 2011 22:57:29 +0300 Lines: 59 Message-ID: References: NNTP-Posting-Host: pepper.modeemi.fi Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.cc.tut.fi 1305575849 20053 2001:708:310:3430:203:baff:fe7d:42dd (16 May 2011 19:57:29 GMT) X-Complaints-To: abuse@tut.fi NNTP-Posting-Date: Mon, 16 May 2011 19:57:29 +0000 (UTC) User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/21.3 (usg-unix-v) Cancel-Lock: sha1:NfVZRaFnr4O+wZNIfSIUGMYMKg8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5534 Neal Becker 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('