2

I apologize if this question has been asked before, but I was unable to find any record of this issue. Full disclosure: I've only been using Python for a few months and MySQL for about 1 month.

I've written a short Python script on a Raspberry Pi (running Raspbian Wheezy) that sniffs wifi packets and writes signal strength info to a MySQL database. I've also created a small PHP file that grabs the info from the database and presents it in a table (pretty basic). All components of this little system work exactly as planned, however...

When I run the Python script in the background (sudo python my_script.py &) it does not appear to update the MySQL database with new readings. Yet it also throws no errors and outputs to the console without a problem (I have a line printed each time a wifi packet is intercepted and its RSSI is added to the database). I encountered the same problem when starting the script at boot up using the /etc/rc.local file. No errors, but no updates in the database either.

Is the problem on the Python side of things? A MySQL setting that I need to change? Is there something else I'm completely missing?

EDITED TO ADD CODE:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb as mdb
import sys
from scapy.all import *

# Create connection to MySQL database called 'DATABASE' at localhost with username 'USERNAME' and password 'PASSWORD'
HOST = "localhost"
USER = "USERNAME"
PW = "PASSWORD"
DB = "DATABASE"

con = mdb.connect(HOST, USER, PW, DB)

# set interface that will be used to monitor wifi
interface = "mon0"

with con:

        cur = con.cursor()

        # This function will be called every time a packet is intercepted. Packet is passed to function as 'p'
        def sniffmgmt(p):

                # These are the 3 types of management frames that are sent exclusively by clients (allows us to weed out packets sent by APs)
                stamgmtstypes = (0, 2, 4)

                if p.haslayer(Dot11):

                        # Make sure packet is a client-only type
                        if p.subtype in stamgmtstypes:

                                # Calculate RSSI
                                sig_str = -(256-(ord(p.notdecoded[-4:-3])))

                                # Update database with most recent detection
                                cur.execute("REPLACE INTO attendance(mac_address, rssi, timestamp) VALUES('%s', %d, NOW())" % (p.addr2, sig_str))

                                # Print MAC address that was detected (debugging only)
                                print "MAC Address (%s) has been logged" % (p.addr2)

        # Tell scapy what interface to use (see above) and which function to call when a packet is intercepted. lfilter limits packets to management frames.
        sniff(iface=interface, prn=sniffmgmt, store=0, lfilter=lambda x:x.type==0)

Thanks! Kyle

P.S. For those who are wondering: this is not intended for malicious use, it is being used to investigate product tracking techniques at our warehouse.

5
  • You could help us in helping you, if you could provide some snippet. BTW trying to do something similar myself, wondering what library you are using. Commented Apr 26, 2013 at 15:15
  • I'd be happy to do that, @Nipun, I'm just not sure what I can include that will be helpful. Would it be overkill to post the entire python script? It's fairly basic, so it shouldn't take long to read through. Commented Apr 26, 2013 at 15:18
  • You may put some relevant portions such as the code pushing to DB in the post and maybe link the entire code on Pastebin Commented Apr 26, 2013 at 15:23
  • Just to clarify: When running in foreground your code works fine and inserts data in the DB? Commented Apr 26, 2013 at 15:28
  • It works like a charm when running in the foreground. Although I think adding con.commit() after cur.execute() solved my problem. I'm testing right now. Commented Apr 26, 2013 at 15:33

1 Answer 1

2

I expect you're not calling commit on the db transaction.

Sign up to request clarification or add additional context in comments.

7 Comments

If commit is missing,how would it push data into DB when not run in background as the OP says
I've never heard of commit but I'll look into it. I've edited my question to include my python script.
@NipunBatra I don't see anywhere that the OP says that.
I think commit solved my problem, I'll reply in a few minutes with the results!
@DanielRoseman: This line confused me: All components of this little system work exactly as planned, however...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.