创建数据库文件,没有数据/架构

时间:2017-06-22 03:23:34

标签: python sqlite scapy

.db文件在提供的路径中创建。打印输出显示在终端窗口中,但.db文件中没有数据/模式。没有错误出现,语法似乎正确。这是代码。我不知道为什么,conn.commit()处于正确的区域。任何信息将不胜感激。

from scapy.all import *
from scapy.layers import dhcp
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
runtime = logging.getLogger('scapy.runtime')
runtime.setLevel(logging.ERROR)
loading = logging.getLogger('scapy.loading')
loading.setLevel(logging.ERROR)
from scapy.layers.l2 import Ether
from scapy.layers.all import BOOTP
from scapy.layers.all import DHCP, DHCPTypes, DHCPOptions, DHCPRevOptions
from scapy import route
import urllib
import urllib3
from urllib.request import urlopen
import os
import sqlite3
from datetime import datetime
import sys


DIR_NAME = os.path.dirname(__file__)
db_path = os.path.join(DIR_NAME, "/home/dtman/Desktop/Secure_DHCP  /secureDHCP-DB.db")

conn = sqlite3.connect(db_path)
c = conn.cursor()
conn.commit()


def make_table():
   c.execute('CREATE TABLE IF NOT EXISTS secureDHCP(mac VARCHAR(50), vendorId VARCHAR(50), time VARCHAR(50), oData VARCHAR(50)')
   conn.commit()
   c.close()
   conn.close()


def the_data():
   mac = src_mac()
   vendorId = vendor()
   time = timeData()
   oData = optData()
   c.execute("INSERT INTO secureDHCP (mac, vendorId, time, oData) VALUES (?, ?, ?, ?)",
          (src_mac, vendor, timeDate, optData))
   conn.commit()


s=socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))

print("Input network interface")
interface = input()


def pkt_data(pkt):
    src_mac = pkt.getlayer(Ether).fields['src']
    url = 'http://api.macvendors.com/' + src_mac
    r = urlopen(url)
    vendor = r.read()
    timeData = str(datetime.now())
    full_options = pkt.getlayer(DHCP).fields['options']
    dhcp_options = [o for o in full_options if isinstance(o, tuple)]
    for x in dhcp_options:
        if x[0] in ('message-type', 'requested_addr', 'hostname'):
            optData = x[1]
            print('MAC: {} /// Vendor: {} /// Time {} /// optData: {}'.format(src_mac, vendor, timeData, optData))

sniff(iface=interface, prn=pkt_data, filter='udp port (67 or 68)', store=0)

终端输出:

`MAC: 00:00:00:00:00:00 /// Vendor: b'BRAND, Inc.' /// Other Data: 3
 MAC: 00:00:00:00:00:00 /// Vendor: b'BRAND, Inc.' /// Other Data: 192.168.1.4
 MAC: 00:00:00:00:00:00 /// Vendor: b'BRAND, Inc.' /// Other Data: b'HOSTNAME'`

1 个答案:

答案 0 :(得分:0)

首先,您没有调用类似make_table()的方法。其次,您致电make_table() 在其中,为什么要关闭connthe_data()如何插入日期?请给出最小样本。下面的内容永远不会出错。

conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS secureDHCP(mac VARCHAR(50), vendorId VARCHAR(50), time VARCHAR(50), oData VARCHAR(50)')
conn.commit()
c.execute("INSERT INTO secureDHCP (mac, vendorId, time, oData) VALUES (?, ?, ?, ?)",
      (src_mac, vendor, timeDate, optData))
conn.commit()
c.close()
conn.close()