この質問が以前に行われた場合は申し訳ありませんが、この問題に関する記録は見つかりませんでした。完全な開示: 私は Python を数か月しか使用しておらず、MySQL を約 1 か月しか使用していません。
Wi-Fi パケットをスニッフィングし、信号強度情報を MySQL データベースに書き込む、Raspberry Pi (Raspbian Wheezy を実行) で短い Python スクリプトを作成しました。また、データベースから情報を取得してテーブルに表示する小さな PHP ファイルも作成しました (かなり基本的なものです)。この小さなシステムのすべてのコンポーネントは、計画どおりに機能しますが...
バックグラウンドで Python スクリプトを実行すると (sudo python my_script.py &)、MySQL データベースが新しい読み取り値で更新されないようです。それでも、エラーは発生せず、問題なくコンソールに出力されます (wifi パケットが傍受され、その RSSI がデータベースに追加されるたびに行が出力されます)。/etc/rc.local ファイルを使用して起動時にスクリプトを開始すると、同じ問題が発生しました。エラーはありませんが、データベースの更新もありません。
問題は Python 側にあるのでしょうか? 変更する必要がある MySQL の設定は? 私が完全に見逃しているものは他にありますか?
コードを追加するために編集:
#!/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)
ありがとう!カイル
PS ご不明な点がある場合: これは悪意のある使用を意図したものではありません。当社の倉庫で製品追跡技術を調査するために使用されています。