Ejemplo n.º 1
0
def Connect(entryName, bUseCallback):
    if bUseCallback:
        theCallback = Callback
        win32event.ResetEvent(callbackEvent)
    else:
        theCallback = None
    #       in order to *use* the username/password of a particular dun entry, one must
    #       explicitly get those params under win95....
    try:
        dp, b = win32ras.GetEntryDialParams( None, entryName )
    except:
        print("Couldn't find DUN entry: %s" % entryName)
    else:
        hras, rc = win32ras.Dial(None, None, (entryName, "", "", dp[ 3 ], dp[ 4 ], ""),theCallback)
    #       hras, rc = win32ras.Dial(None, None, (entryName, ),theCallback)
    #       print hras, rc
        if not bUseCallback and rc != 0:
            print("Could not dial the RAS connection:", win32ras.GetErrorString(rc))
            hras = HangUp( hras )
        #       don't wait here if there's no need to....
        elif bUseCallback and win32event.WaitForSingleObject(callbackEvent, 60000)!=win32event.WAIT_OBJECT_0:
            print("Gave up waiting for the process to complete!")
            #       sdk docs state one must explcitly hangup, even if there's an error....
            try:
                cs = win32ras.GetConnectStatus( hras )
            except:
                #       on error, attempt a hang up anyway....
                hras = HangUp( hras )
            else:
                if int( cs[ 0 ] ) == win32ras.RASCS_Disconnected:
                    hras = HangUp( hras )
    return hras, rc
Ejemplo n.º 2
0
def ShowIpAddress(handle):
    print win32ras.GetConnectStatus(handle)
    data = os.popen("ipconfig", "r").readlines()

    have_ppp = 0
    ip_str = None
    for line in data:
        #print line.decode('gbk')
        if line.find("宽带连接") >= 0:
            have_ppp = 1
        #if your system language is English, you should write like this:
        #if have_ppp and line.strip().startswith("IP Address"):
        #in othewords, replace the "IPv4 地址" to "IP Address"
        if have_ppp and line.strip().startswith("IPv4 地址"):
            ip_str = line.split(":")[1].strip()
            have_ppp = 0
            print ip_str

        if line.find("PPP") >= 0:
            have_ppp = 2
        #print line.strip()
        #if your system language is English, you should write like this:
        #if have_ppp and line.strip().startswith("IP Address"):
        #in othewords, replace the "IPv4 地址" to "IP Address"
        if have_ppp and line.strip().startswith("IPv4"):
            ip_str = line.split(":")[1].strip()
            have_ppp = 0
            print ip_str
Ejemplo n.º 3
0
def DialPhoneBookEntry( phonebook_entry ):
    isconnected = 0
    conns = win32ras.EnumConnections()
    for conn in conns:
        #print conn
        if conn[1] == phonebook_entry:
            isconnected = 1

    if isconnected:
        print 'Connected to', phonebook_entry
    else:
        print 'Dialing %s . . .' % phonebook_entry
        win32api.WinExec( 'rasphone -d \"%s\"' % phonebook_entry )
        # TODO: handle Cancel within rasphone
        status = RASCS_Disconnected
        while not isconnected:
            win32api.Sleep( 1000 )
            conns = win32ras.EnumConnections()
            for conn in conns:
                if conn[1] == phonebook_entry:
                    hConn = conn[0]
                    status = win32ras.GetConnectStatus( hConn )
                    # intermediate states 5 = RASCS_Authenticate, 14=RASCS_Authenticated
                    if status[0] == RASCS_Authenticate:
                        if status != status[0]:
                            status = status[0]
                            print 'Authenticating...'
                    elif status[0] == RASCS_Authenticated:
                        if status != status[0]:
                            status = status[0]
                            print 'Authenticated.'
                    elif status[0] == RASCS_Connected:
                        print 'Connected.'
                        isconnected = 1
                        break
                    else:
                        print 'status:', status
            else:
                # *** this only works in NT4
                # *** need to figure out equiv for W2K
                winver = win32api.LOWORD( win32api.GetVersion() )
                if winver < 5:
                    try:
                        hwnd = FindWindow( '#32770', 'Connecting to %s...' % phonebook_entry )
                    except win32api.error, err:
                        if err[0] == winerror.ERROR_PROC_NOT_FOUND:
                            print 'Connection cancelled.'
                            time.sleep( 1 )
                            return
Ejemplo n.º 4
0
def ShowIpAddress(handle):
    print win32ras.GetConnectStatus(handle)
    data = os.popen("ipconfig", "r").readlines()

    have_ppp = 0
    ip_str = None
    for line in data:
        if line.find("宽带连接") >= 0:
            have_ppp = 1
        if have_ppp and line.strip().startswith("IPv4 地址"):
            ip_str = line.split(":")[1].strip()
            have_ppp = 0
            print ip_str

        if line.find("PPP") >= 0:
            have_ppp = 2
        if have_ppp and line.strip().startswith("IPv4"):
            ip_str = line.split(":")[1].strip()
            have_ppp = 0
            print ip_str