Esempio n. 1
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
Esempio n. 2
0
def Connect(rasEntryName, numRetries=5):
    """Make a connection to the specified RAS entry.
	
	Returns a tuple of (bool, handle) on success.
	- bool is 1 if a new connection was established, or 0 is a connection already existed.
	- handle is a RAS HANDLE that can be passed to Disconnect() to end the connection.
	
	Raises a ConnectionError if the connection could not be established.
	"""
    assert numRetries > 0
    for info in win32ras.EnumConnections():
        if info[1].lower() == rasEntryName.lower():
            print "Already connected to", rasEntryName
            return 0, info[0]

    dial_params, have_pw = win32ras.GetEntryDialParams(None, rasEntryName)
    if not have_pw:
        print "Error: The password is not saved for this connection"
        print "Please connect manually selecting the 'save password' option and try again"
        sys.exit(1)

    print "Connecting to", rasEntryName, "..."
    retryCount = numRetries
    while retryCount > 0:
        rasHandle, errCode = win32ras.Dial(None, None, dial_params, None)
        if win32ras.IsHandleValid(rasHandle):
            bValid = 1
            break
        print "Retrying..."
        win32api.Sleep(5000)
        retryCount = retryCount - 1

    if errCode:
        raise ConnectionError(errCode, win32ras.GetErrorString(errCode))
    return 1, rasHandle
Esempio n. 3
0
def ShowConnections():
    print("All phone-book entries:")
    for (name, ) in win32ras.EnumEntries():
        print(" ", name)
    print("Current Connections:")
    for con in win32ras.EnumConnections():
        print(" ", con)
Esempio n. 4
0
 def hangup(self, ensoapi, entryName):
     try:
         conn = (i for i in win32ras.EnumConnections()
                 if i[1] == entryName).next()
         win32ras.HangUp(conn[0])
     except:
         print "Couldn't hangup: %s" % entryName
Esempio n. 5
0
 def check_for_broadband(self):
     connections = win32ras.EnumConnections()
     if len(connections) == 0:
         self.print_log("系统未运行任何宽带连接")
         return
     else:
         self.print_log("系统正在运行%d个宽带连接" % len(connections))
         return connections
Esempio n. 6
0
def Check_for_Broadband():
    connections = []
    connections = win32ras.EnumConnections()
    if(len(connections) == 0):
        print "The system is not running any broadband connection."
        return
    else:
        print "The system is running %d broadband connection." % len(connections)
        return connections
Esempio n. 7
0
def Disconnect(handle):
    if type(handle) == type(''):  # have they passed a connection name?
        for info in win32ras.EnumConnections():
            if info[1].lower() == handle.lower():
                handle = info[0]
                break
        else:
            raise ConnectionError(0, "Not connected to entry '%s'" % handle)

    win32ras.HangUp(handle)
Esempio n. 8
0
def Disconnect(rasEntry):
    # Need to find the entry
    name = rasEntry.lower()
    for hcon, entryName, devName, devType in win32ras.EnumConnections():
        if entryName.lower() == name:
            win32ras.HangUp(hcon)
            print("Disconnected from", rasEntry)
            break
    else:
        print("Could not find an open connection to", entryName)
Esempio n. 9
0
def HangUpConnection( phonebook_entry ):
    connlist = win32ras.EnumConnections()
    #[(655360, 'Office PPTP', 'VPN', 'RASPPTPM')]
    for conninfo in connlist:
        if conninfo[1] == phonebook_entry:
            hrasconn = conninfo[0]
            win32ras.HangUp( hrasconn )
            break
    else:
        print 'Not connected.'
        #print "RAS connection '%s' not found" % phonebook_entry
        sys.exit(1)
Esempio n. 10
0
 def _disconnect_ras(self):
     """ 断开连接 """
     if self.ckp_task:
         self.ckp_task.stop()
     conns = win32ras.EnumConnections()
     if conns:
         for conn in conns:
             try:
                 win32ras.HangUp(conn[0])
             except:
                 pass
     self.session = None
Esempio n. 11
0
def GetRasIpAddress( phonebook_entry ):
    connlist = win32ras.EnumConnections()
    #[(655360, 'Office PPTP', 'VPN', 'RASPPTPM')]
    for conninfo in connlist:
        if conninfo[1] == phonebook_entry:
            break
    else:
        print "RAS connection '%s' not found" % phonebook_entry
        sys.exit(1)

    hrasconn = conninfo[0]
    structsize = struct.calcsize( 'L L 16s' )

    dw = windll.membuf( struct.calcsize( 'i' ) )
    dw.mb.write( struct.pack( 'i', structsize ) )

    buf = windll.membuf( structsize )
    buf.mb.write( struct.pack( 'L', structsize ) ) # MUST set struct member

    # on W2K/laptop, need a bit of a delay???
    maxretries = 10
    count = 0
    while count < maxretries:
        err = rasapi32.RasGetProjectionInfo( hrasconn, RASP_PppIp, buf, dw )
        if err == 0:
            break
        else:
            count = count + 1
            if count < maxretries:
                win32api.Sleep( 1000 )
            else:
                print 'RasGetProjectionInfo error %d' % err
                print 'go look up the error code in <ras.h>'
                sys.exit( 1 )

    dwSize, err, rawipaddr = struct.unpack( 'L L 16s', buf.mb.read() )
    ipaddr, junk = string.split( rawipaddr,'\0', 1 )
    #'192.168.91.4\000_ma'

    print 'IP address:', ipaddr
    return ipaddr
Esempio n. 12
0
 def get_conn(self):
     connections = win32ras.EnumConnections()
     return connections