Exemplo n.º 1
0
def db_update_address(connection,mac=False):
    """
    
    """
    global debug
    global verbose

    result = ""
    addr=""
    MacId=""
    GPS=""
    Address=""

    try:
            if mac:
                macid = db_get_id_from_mac(connection, mac)
                result = connection.execute("SELECT MacId,GPS,Address FROM Locations WHERE MacId="+str(macid)+";") 
            else:
                result = connection.execute("SELECT MacId,GPS,Address FROM Locations WHERE Address like \"NO ADDRESS RETRIEVED\" OR Address=\"\";") 
            if result:
                result = result.fetchall()
                print
                for (MacId,GPS,Address) in result:
                    if GPS != 'False':
                        if Address in ['NO ADDRESS RETRIEVED','']:
                            temp= getCoordinates(GPS)
                            temp= getCoordinates(str(GPS.strip("\'").strip("\'")))
                            addr = temp[1]
                            addr = addr.encode('utf-8')
                            result_update = connection.execute("UPDATE Locations SET Address=? WHERE MacId=? AND GPS=?;",(repr(addr),str(MacId),str(GPS))) 
                            print 'Updating {}'.format(mac)
                            print '   {} :: {} :: {}'.format(GPS, Address.encode('utf-8'), addr)
                        else:
                            print 'Address of the device already exists: {}'.format(mac)
                            print '   {} :: {}'.format(GPS, Address.encode('utf-8'))

                    else:
                        print 'No location stored for device {}'.format(mac)
                        print '   {} {} {}'.format(MacId, GPS, Address)
                    time.sleep(1)
                    print
            else:  
                print 'No results found with empty address to update.'
            connection.commit()
    
    except KeyboardInterrupt:
        print 'Exiting. It may take a few seconds.'
        sys.exit(1)
    except Exception as inst:
        print 'Exception in db_update_address(connection,mac=False) function'
        print 'Ending threads, exiting when finished'
        print type(inst) # the exception instance
        print inst.args # arguments stored in .args
        print inst # _str_ allows args to printed directly
        x, y = inst # _getitem_ allows args to be unpacked directly
        print 'x =', x
        print 'y =', y
        sys.exit(1)
Exemplo n.º 2
0
def get_address_from_gps(location_gps):
	global debug
	global verbose
	global address_cache
	global flag_internet
	global threadbreak
	global flag_fake_gps
	global global_location
	
	coordinates = ""
	address = ""
	try:
		if debug:
			print 'In get_address_from_gps(location_gps) function'
		if location_gps:
			if debug:
				print 'Coordinates: {}'.format(location_gps)
			try:
				# If the location is already stored, we get it.
				if location_gps:
					address = address_cache[location_gps]
			except:
				if flag_internet:
					[coordinates,address] = getCoordinatesFromAddress.getCoordinates(location_gps)
					if debug:
						print 'Coordinates: {} Address: {}'.format(coordinates,address)
					if coordinates == "" and address == "":
						if debug:
							print 'Returning empty coordinates and address'
						return [location_gps,address]

					if flag_fake_gps and coordinates and coordinates != global_location:
						global_location = coordinates
						location_gps = coordinates
					address = address.encode('utf-8')
					
					if coordinates and address:
						address_cache[coordinates] = address
				else:
					address = "Internet deactivated"
		return [location_gps,address]

	except KeyboardInterrupt:
		print 'Exiting. It may take a few seconds.'
		threadbreak = True
	except Exception as inst:
		print 'Exception in get_address_from_gps()'
		print 'Received coordinates: {}'.format(location_gps)
		print 'Retrieved coordinates: {}'.format(coordinates)
		print 'Retrieved Address: {}'.format(address)
		threadbreak = True
		print 'Ending threads, exiting when finished'
		print type(inst) # the exception instance
		print inst.args # arguments stored in .args
		print inst # _str_ allows args to printed directly
		x, y = inst # _getitem_ allows args to be unpacked directly
		print 'x =', x
		print 'y =', y
		sys.exit(1)
Exemplo n.º 3
0
def get_address_from_gps(location_gps):
    """
    This function gets an address from gps coordinates. 
    """
    global debug
    global verbose
    global address_cache
    global flag_internet
    global threadbreak

    coordinates = ""
    address = ""
    try:
        if location_gps:
            if debug:
                print 'Coordinates: {}'.format(location_gps)
            try:
                # If the location is already stored, we get it.
                address = address_cache[location_gps]
            except:
                if flag_internet:
                    # print location_gps
                    #[coordinates,address] = getCoordinatesFromAddress.getCoordinates(location_gps)
                    [coordinates, address] = getCoordinates(location_gps)
                    address = address.encode('utf-8')
                    if debug:
                        print 'Coordinates: {} Address: {}'.format(
                            coordinates, address)

                    address_cache[location_gps] = address
                else:
                    address = "Internet option deactivated"
        return address

    except KeyboardInterrupt:
        print 'Exiting received in get_address_from_gps(location_gps) function. It may take a few seconds.'
        threadbreak = True
    except Exception as inst:
        print 'Exception in get_address_from_gps(location_gps)'
        print 'Received coordinates: {}'.format(location_gps)
        print 'Retrieved coordinates: {}'.format(coordinates)
        print 'Retrieved Address: {}'.format(address)
        threadbreak = True
        print 'Ending threads, exiting when finished'
        print type(inst)  # the exception instance
        print inst.args  # arguments stored in .args
        print inst  # _str_ allows args to printed directly
        x, y = inst  # _getitem_ allows args to be unpacked directly
        print 'x =', x
        print 'y =', y
        sys.exit(1)
Exemplo n.º 4
0
def get_address_from_gps(location_gps):
    """
    This function gets an address from gps coordinates. 
    """
    global debug
    global verbose
    global address_cache
    global flag_internet
    global threadbreak
    
    coordinates = ""
    address = ""
    try:
        if location_gps:
            if debug:
                print 'Coordinates: {}'.format(location_gps)
            try:
                # If the location is already stored, we get it.
                address = address_cache[location_gps]
            except:
                if flag_internet:
                    # print location_gps
                    #[coordinates,address] = getCoordinatesFromAddress.getCoordinates(location_gps)
                    [coordinates,address] = getCoordinates(location_gps)
                    address = address.encode('utf-8')
                    if debug:
                        print 'Coordinates: {} Address: {}'.format(coordinates,address)
                    
                    address_cache[location_gps] = address
                else:
                    address = "Internet option deactivated"
        return address

    except KeyboardInterrupt:
        print 'Exiting received in get_address_from_gps(location_gps) function. It may take a few seconds.'
        threadbreak = True
    except Exception as inst:
        print 'Exception in get_address_from_gps(location_gps)'
        print 'Received coordinates: {}'.format(location_gps)
        print 'Retrieved coordinates: {}'.format(coordinates)
        print 'Retrieved Address: {}'.format(address)
        threadbreak = True
        print 'Ending threads, exiting when finished'
        print type(inst) # the exception instance
        print inst.args # arguments stored in .args
        print inst # _str_ allows args to printed directly
        x, y = inst # _getitem_ allows args to be unpacked directly
        print 'x =', x
        print 'y =', y
        sys.exit(1)
Exemplo n.º 5
0
def get_locations_and_dates(connection, mac):
    """
    Get a mac address and return all its locations, dates and vendor
    """
    try:
        global debug
        global verbose
        global quiet


        if debug:
            print '* In get locations and dates from device'
        addresslist = {}
        address = ""
        address_to_insert = ""

        locations_dates_results = db_locations_and_dates(connection,mac)

        # Update the mac vendor if it is not there.
        vendor = db_update_vendor(connection, str(mac))

        # Get the steamed version also
        steamed_vendor = steam_vendors_names(vendor)

        if not quiet:
            print "\tMAC Address: {0}. Vendor: {1}. Steamed Vendor: {2}".format(mac,vendor,steamed_vendor)

        # Get all the locations from the db for this mac
        for (Id,gps,fseen,lseen,name,address) in locations_dates_results:
            address = address.encode('utf-8')
            gps = str(gps)


            if debug:
                print '\n\n* This is the data that is currently being processed:'
                print '* Id: {}, GPS: {}, FirstSeen: {}, LastSeen: {}, Name: {}, Address: {}'.format(Id,gps,fseen,lseen,name,address)
                print


            #if (str(gps) != "False") or (str(gps) != ' ') or (str(gps) != 'GPS') or ('NO GPS' not in str(gps)):
            #if ( len(gps.split(',')) == 2 and len(gps.split(',')[0].split('.')) == 2 and len(gps.split(',')[1].split('.')) == 2 ):

            # Search for a proper gps position string using regular expresions
            import re
            gpspattern = re.compile('[0-9]+\.[0-9]+,[0-9]+\.[0-9]+')
            if ( gpspattern.search(gps) ):
                if debug:
                    print '* gps: {} is not \'False\' nor empty nor \'GPS\''.format(gps)
                    print 
                if ('NO ADDRESS' in address) or (len(address)<8):
                    if debug:
                        print '* address: {} is not \'NO ADDRESS\' nor empty'.format(address)
                        print
                    try:
                        address_to_insert = addresslist[gps]
                        if debug:
                            print '* Address to insert: {}'.format(address_to_insert)
                            print
                        print 'Address cached: {}'.format(address_to_insert)
                    except KeyboardInterrupt: 
                        print 'Exiting'
                        sys.exit(1)
                        break
                    except:
                        # This can be deleted. There is nothing here that is not a proper gps...
                        try:
                            gps.split(",")[1]
                        except:
                            print '\t\tNO GPS.'
                            print "\t\t\tIgnoring: {}: {}-{}, {} ({})".format(name, fseen, lseen, gps, str(address_to_insert))
                            break
                        #time.sleep(1)
                        a = gps
                        addr = getCoordinates(str(a.strip("\'").strip("\'")))
                        address_to_insert = addr[1]
                        address_to_insert = address_to_insert.encode('utf-8')
                        try:
                            addresslist[gps]=address_to_insert 
                        except:
                            print 'Cannot add new address to cache'
                    try:
                        if debug:
                            print '* Updating Locations table, setting address.'
                        if len(address_to_insert) > 5:
                                connection.execute("UPDATE Locations SET Address=\""+str(address_to_insert)+"\" WHERE Id="+str(Id))
                                connection.commit()
                                print "\t\t*{}: {}-{}, {} ({})".format(name, fseen, lseen, gps, str(address_to_insert))
                        else:
                                print '\t\tAddress content seems incorrect'
                                print "\t\t\tNot updating: {}: {}-{}, {} ({})".format(name, fseen, lseen, gps, str(address_to_insert))
                    except KeyboardInterrupt: 
                        print 'Exiting'
                        sys.exit(1)
                        break
                    except:
                        print "Exception updating device address"
                        print "{}".format(gps)
                        print "{}".format(address)
                        print "{}".format(address_to_insert)
                else:
                    if debug:
                        print 'Address already exists'
                    print "\t\t{}: {}-{}, {} ({})".format(name, fseen, lseen, gps, address)
            else:
                if debug:
                    print 'There is no GPS.'
                print "\t\t#{}: {}-{}, {} ({})".format(name, fseen, lseen, gps, address)


    except KeyboardInterrupt:
        print 'Exiting. It may take a few seconds.'
        sys.exit(1)
    except Exception as inst:
        print 'Exception in get_locations_and_dates() function'
        print 'Ending threads, exiting when finished'
        print type(inst) # the exception instance
        print inst.args # arguments stored in .args
        print inst # _str_ allows args to printed directly
        sys.exit(1)
Exemplo n.º 6
0
def get_locations_and_dates(connection, mac):
    """
    Get a mac address and return all its locations, dates and vendor
    """
    try:
        global debug
        global verbose
        global quiet

        if debug:
            print '* In get locations and dates from device'
        addresslist = {}
        address = ""
        address_to_insert = ""

        locations_dates_results = db_locations_and_dates(connection, mac)

        # Update the mac vendor if it is not there.
        vendor = db_update_vendor(connection, str(mac))

        # Get the steamed version also
        steamed_vendor = steam_vendors_names(vendor)

        if not quiet:
            print "\tMAC Address: {0}. Vendor: {1}. Steamed Vendor: {2}".format(
                mac, vendor, steamed_vendor)

        # Get all the locations from the db for this mac
        for (Id, gps, fseen, lseen, name, address) in locations_dates_results:
            address = address.encode('utf-8')
            gps = str(gps)

            if debug:
                print '\n\n* This is the data that is currently being processed:'
                print '* Id: {}, GPS: {}, FirstSeen: {}, LastSeen: {}, Name: {}, Address: {}'.format(
                    Id, gps, fseen, lseen, name, address)
                print

            #if (str(gps) != "False") or (str(gps) != ' ') or (str(gps) != 'GPS') or ('NO GPS' not in str(gps)):
            #if ( len(gps.split(',')) == 2 and len(gps.split(',')[0].split('.')) == 2 and len(gps.split(',')[1].split('.')) == 2 ):

            # Search for a proper gps position string using regular expresions
            import re
            gpspattern = re.compile('[0-9]+\.[0-9]+,[0-9]+\.[0-9]+')
            if (gpspattern.search(gps)):
                if debug:
                    print '* gps: {} is not \'False\' nor empty nor \'GPS\''.format(
                        gps)
                    print
                if ('NO ADDRESS' in address) or (len(address) < 8):
                    if debug:
                        print '* address: {} is not \'NO ADDRESS\' nor empty'.format(
                            address)
                        print
                    try:
                        address_to_insert = addresslist[gps]
                        if debug:
                            print '* Address to insert: {}'.format(
                                address_to_insert)
                            print
                        print 'Address cached: {}'.format(address_to_insert)
                    except KeyboardInterrupt:
                        print 'Exiting'
                        sys.exit(1)
                        break
                    except:
                        # This can be deleted. There is nothing here that is not a proper gps...
                        try:
                            gps.split(",")[1]
                        except:
                            print '\t\tNO GPS.'
                            print "\t\t\tIgnoring: {}: {}-{}, {} ({})".format(
                                name, fseen, lseen, gps,
                                str(address_to_insert))
                            break
                        #time.sleep(1)
                        a = gps
                        addr = getCoordinates(str(a.strip("\'").strip("\'")))
                        address_to_insert = addr[1]
                        address_to_insert = address_to_insert.encode('utf-8')
                        try:
                            addresslist[gps] = address_to_insert
                        except:
                            print 'Cannot add new address to cache'
                    try:
                        if debug:
                            print '* Updating Locations table, setting address.'
                        if len(address_to_insert) > 5:
                            connection.execute(
                                "UPDATE Locations SET Address=\"" +
                                str(address_to_insert) + "\" WHERE Id=" +
                                str(Id))
                            connection.commit()
                            print "\t\t*{}: {}-{}, {} ({})".format(
                                name, fseen, lseen, gps,
                                str(address_to_insert))
                        else:
                            print '\t\tAddress content seems incorrect'
                            print "\t\t\tNot updating: {}: {}-{}, {} ({})".format(
                                name, fseen, lseen, gps,
                                str(address_to_insert))
                    except KeyboardInterrupt:
                        print 'Exiting'
                        sys.exit(1)
                        break
                    except:
                        print "Exception updating device address"
                        print "{}".format(gps)
                        print "{}".format(address)
                        print "{}".format(address_to_insert)
                else:
                    if debug:
                        print 'Address already exists'
                    print "\t\t{}: {}-{}, {} ({})".format(
                        name, fseen, lseen, gps, address)
            else:
                if debug:
                    print 'There is no GPS.'
                print "\t\t#{}: {}-{}, {} ({})".format(name, fseen, lseen, gps,
                                                       address)

    except KeyboardInterrupt:
        print 'Exiting. It may take a few seconds.'
        sys.exit(1)
    except Exception as inst:
        print 'Exception in get_locations_and_dates() function'
        print 'Ending threads, exiting when finished'
        print type(inst)  # the exception instance
        print inst.args  # arguments stored in .args
        print inst  # _str_ allows args to printed directly
        sys.exit(1)
Exemplo n.º 7
0
def db_update_address(connection, mac=False):
    """
    
    """
    global debug
    global verbose

    result = ""
    addr = ""
    MacId = ""
    GPS = ""
    Address = ""

    try:
        if mac:
            macid = db_get_id_from_mac(connection, mac)
            result = connection.execute(
                "SELECT MacId,GPS,Address FROM Locations WHERE MacId=" +
                str(macid) + ";")
        else:
            result = connection.execute(
                "SELECT MacId,GPS,Address FROM Locations WHERE Address like \"NO ADDRESS RETRIEVED\" OR Address=\"\";"
            )
        if result:
            result = result.fetchall()
            print
            for (MacId, GPS, Address) in result:
                if GPS != 'False':
                    if Address in ['NO ADDRESS RETRIEVED', '']:
                        temp = getCoordinates(GPS)
                        temp = getCoordinates(str(GPS.strip("\'").strip("\'")))
                        addr = temp[1]
                        addr = addr.encode('utf-8')
                        result_update = connection.execute(
                            "UPDATE Locations SET Address=? WHERE MacId=? AND GPS=?;",
                            (repr(addr), str(MacId), str(GPS)))
                        print 'Updating {}'.format(mac)
                        print '   {} :: {} :: {}'.format(
                            GPS, Address.encode('utf-8'), addr)
                    else:
                        print 'Address of the device already exists: {}'.format(
                            mac)
                        print '   {} :: {}'.format(GPS,
                                                   Address.encode('utf-8'))

                else:
                    print 'No location stored for device {}'.format(mac)
                    print '   {} {} {}'.format(MacId, GPS, Address)
                time.sleep(1)
                print
        else:
            print 'No results found with empty address to update.'
        connection.commit()

    except KeyboardInterrupt:
        print 'Exiting. It may take a few seconds.'
        sys.exit(1)
    except Exception as inst:
        print 'Exception in db_update_address(connection,mac=False) function'
        print 'Ending threads, exiting when finished'
        print type(inst)  # the exception instance
        print inst.args  # arguments stored in .args
        print inst  # _str_ allows args to printed directly
        x, y = inst  # _getitem_ allows args to be unpacked directly
        print 'x =', x
        print 'y =', y
        sys.exit(1)