#!/usr/bin/python #sqlite3 bluespam.sql "CREATE TABLE log(id INTEGER PRIMARY KEY ASC, timestamp INT, name TEXT, mac TEXT)" import os import string import time import sqlite3 import sys #cfg_file="file.txt" cfg_file=sys.argv[1] cfg_looptime=120 cfg_offset=600 start_bluetooth_dev="echo 1 > /sys/bus/platform/devices/neo1973-pm-bt.0/power_on; echo 0 > /sys/bus/platform/devices/neo1973-pm-bt.0/reset" os.system(start_bluetooth_dev) time.sleep(1) start_bluetooth_d="/etc/init.d/bluetooth start" os.system(start_bluetooth_d) time.sleep(1) con = sqlite3.connect("./bluespam.sql") table_name=str.replace(cfg_file, ".", "_") table_name=str.replace(table_name, "-", "") cur = con.cursor() query="CREATE TABLE IF NOT EXISTS " + table_name + "(id INTEGER PRIMARY KEY ASC, timestamp INT, name TEXT, mac TEXT)" cur.execute(query) con.commit() while 1>0: dev_scan = "hcitool scan" print time.ctime() + " - scanning for device..." list_dev=os.popen(dev_scan) tot= list_dev.read() tot= tot.split("\n") tot.pop(0) tot.reverse() tot.pop(0) for dev in tot: dev=dev.split("\t") get_channel = "sdptool search --bdaddr " + dev[1] + " OPUSH | sed 's/ //g' | grep Channel | cut -d: -f 2" channel_scan=os.popen(get_channel) channel=channel_scan.read() print time.ctime() + " - Found \"" + dev[2] + "\"", if channel != "": date=time.strftime("%s") send_file="obextool push " + cfg_file + " " + dev[1] + " " + channel cur = con.cursor() query="SELECT COUNT(*) FROM " + table_name + " WHERE timestamp<" + str(date) + " AND timestamp>" + str(int(date)-cfg_offset) + " AND mac = \"" + dev[1] + "\"" row=cur.execute(query) row = cur.fetchone() con.commit() if row[0] == 0: print "not present in database. try to send " + cfg_file elif row[0] > 0: print "present in database. not sending file" if row[0] == 0: send=os.popen(send_file) send_output=send.read() if send_output[:-1] == "Sending object ...": cur = con.cursor() query = "INSERT INTO " + table_name + " VALUES(NULL, '" + date + "', '" + dev[2] + "', '" + dev[1] + "');" cur.execute(query) con.commit() print time.ctime() + " - " + cfg_file + " sent to \"" + dev[2] + "\"" elif send_output[:-1] == "Can't connect. Connection refused (111)": print time.ctime() + " - \"" + dev[2] + "\" reject " + cfg_file elif send_output[:-1] == "Can't connect. Operation now in progress (115)": print time.ctime() + " - \"" + dev[2] + "\" connection time out." time.sleep(cfg_looptime)