Tim Roberts schrieb:
> As a self-proclaimed studly Win32 programmer, I'm embarrassed at not
> being able to find a neat solution to this.  I come to you humbly,
> seeking coolness.
> 
> I have a script on my laptop that runs after login and launches the
> tools that I use all the time, including the putty password agent, the
> Agent newsreader, and Firefox.  My wireless card takes about 15 seconds
> to sync up with my access point and get an IP address, by which time my
> script is done and the tools are running.  That means that Firefox
> always comes up complaining that it couldn't reach my home page.  So,
> I'd like to have my script wait for connectivity before launching the
> tools.  The question is, how can that be done?
> 
> I could use os.system to poll the "ipconfig" command, but that's icky
> and not at all studly.  I could loop on gethostaddr for localhost until
> it gets an IP, but even that's not studly, because there's no way to
> know that it's really the wireless adapter's IP address.
> 
> There must be a way for me to ask the musical question, "does the
> network adapter called 'Wireless Network Adapter' have an IP address?" 
> Does anybody know that tune?
> 

I have a module lying around that does this.  Should work on 2k and XP, at 
least.
You call it with the 'friendly name' of the network adaper, and it displays the
ip address and network mask (IIRC).  Should be easy to adapt to our needs, and 
more.

Thomas
# This module MUST also work on win2k, although some functions
# exported by iphlpapi are win XP only!
from ctypes import *
from ctypes.wintypes import DWORD, BOOL, HANDLE

MAX_ADAPTER_ADDRESS_LENGTH = 8
MAX_ADAPTER_DESCRIPTION_LENGTH = 128
MAX_ADAPTER_NAME_LENGTH = 256

IP_MASK_STRING = IP_ADDRESS_STRING = c_char * 16

class IP_ADDR_STRING(Structure):
    pass
IP_ADDR_STRING._fields_ = [("Next", POINTER(IP_ADDR_STRING)),
                           ("IpAddress", IP_ADDRESS_STRING),
                           ("IpMask", IP_MASK_STRING),
                           ("Context", DWORD)]

class IP_ADAPTER_INFO(Structure):
    pass
IP_ADAPTER_INFO._fields_ = [("Next", POINTER(IP_ADAPTER_INFO)),
                            ("ComboIndex", DWORD),
                            ("AdapterName", c_char * (MAX_ADAPTER_NAME_LENGTH + 
4)),
                            ("Description", c_char * 
(MAX_ADAPTER_DESCRIPTION_LENGTH + 4)),
                            ("AddressLength", c_uint),
                            ("Address", c_byte * MAX_ADAPTER_ADDRESS_LENGTH),
                            ("Index", DWORD),
                            ("Type", c_uint),
                            ("DhcpEnabled", c_uint),
                            ("CurrentIpAddress", POINTER(IP_ADDR_STRING)),
                            ("IpAddressList", IP_ADDR_STRING),
                            ("GateWayList", IP_ADDR_STRING),
                            ("DhcpServer", IP_ADDR_STRING),
                            ("HaveWins", BOOL),
                            # ... more
                ]
                

class AdapterError(Exception):
    pass

def GetAdapterInfo(friendlyName):
    # Return a list containing (ip address, netmask) tuples for an
    # adapter of the given friendly name (that's the name that is
    # shown in Network and Dial-up Connections)
    outbuflen = c_ulong()
    windll.iphlpapi.GetAdaptersInfo(None, byref(outbuflen))

    # hack, hack, hack
    cdll.msvcrt.malloc.restype = POINTER(IP_ADAPTER_INFO)
    p = cdll.msvcrt.malloc(outbuflen.value)

    windll.iphlpapi.GetAdaptersInfo(p, byref(outbuflen))

    h = HANDLE()
    windll.mprapi.MprConfigServerConnect(None, byref(h))

    result = []
    while 1:
        adapterName = (c_wchar * 256)()
        windll.mprapi.MprConfigGetFriendlyName(h,
                                               unicode(p[0].AdapterName),
                                               byref(adapterName),
                                               sizeof(adapterName))
        if adapterName.value.lower() == friendlyName.lower():
            a = p[0].IpAddressList
            while 1:
                result.append((a.IpAddress, a.IpMask))
                if a.Next:
                    a = a.Next[0]
                    continue
                return result

        p = p[0].Next
        if not p:
            raise AdapterError, "Network adapter '%s' not found" % friendlyName
    return result

if __name__ == "__main__":
    import sys
    print GetAdapterInfo(sys.argv[1])
_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to