以字典格式追加 Scapy 结果 Python
Append Scapy Results in Dictionary Format Python
我使用 python 编写了我的脚本并且它运行成功。我在终端中获取、识别并打印了 addr1,
addr2,
和 addr3
。但是,我 运行 在尝试将这些结果附加到字典格式的 CSV 文件时遇到了几个错误。
这是我之前在终端中打印结果时的脚本:
#!/usr/bin/env python3
from scapy.all import *
import os
devices = set()
interface = "wlan0mon"
iface = "wlan0mon"
def sniffmgmnt(p):
if p.haslayer(Dot11):
devices.add(p.addr1)
devices.add(p.addr2)
devices.add(p.addr3)
print(devices)
while True:
for channel in range(1, 14):
os.system("iwconfig" + iface + "channel" + str(channel))
sniff(iface=interface, prn=sniffmgmnt, count=3, timeout=3, store=0)
然后我尝试将这些结果附加到字典格式的 CSV 文件中,其中 addr3
是 key:
#!/usr/bin/env python3
from scapy.all import *
from snifferCSV import CSV
import os
aps = dict()
devices = set()
headers = ['addr3', 'addr2', 'addr1']
csvFile = CSV('data.csv', headers)
interface = "wlan0mon"
iface = "wlan0mon"
def sniffmgmnt(p):
if p.haslayer(Dot11):
source.append(p.addr1);
receiving.append(p.addr2);
destination.append(p.addr3);
csvFile.addToCSV( [addr3, addr2, addr1] )
while True:
for channel in range(1, 14):
os.system("iwconfig" + iface + "channel" + str(channel))
sniff(iface=interface, prn=sniffmgmnt, count=3, timeout=3, store=0)
但是脚本只在终端输出 headers,而我的 CSV 文件仍然是空的。
下面是我的 CSV class snifferCSV.py:
class CSV(object):
csvFile = None
fileName = data.csv
def __init__(self,fileName,headers):
self.fileName = fileName
self.prepareCSVFile(headers)
def prepareCSVFile(self,headers):
self.csvFile = open(self.fileName, 'w')
headerString = self.prepareValues(headers)
self.writeToCSV(headerString)
def prepareValues(self,valueList):
line = str()
for item in valueList[:-1]:
line = line + str(item) + ','
line += valueList[-1]
return line
def addToCSV(self,valueList):
valueLine = self.prepareValues(valueList)
self.writeToCSV(valueLine)
def writeToCSV(self,line):
print(line)
self.csvFile.write(line)
我的目标是将我的结果以字典格式附加到 CSV 文件中,其中 addr3
是键。但是,我对这个过程的逻辑是错误的。有人能给我指出正确的方向吗?
提前致谢
-JJ
我听取了@coder 的建议并进行了更改
open(self.fileName, 'w')
到 open(self.fileName, 'a')
但是只有我的 headers 被附加到 csv 文件。
我结束了我的 snifferCSV.py
class,而是在我的主文件中嵌入了字典函数,我在主文件中使用了成对的 dict 构造函数和 zip 函数将我的数据附加到 csv 文件中:
#!/usr/bin/env python3
from scapy.all import *
import csv
import os
interface = "wlan0mon"
iface = "wlan0mon"
def sniffmgmnt(p):
if p.haslayer(Dot11):
keys = [(p.addr3)]
values = [(p.addr1, p.addr2)]
my_dict = dict(zip(keys, values))
with open('data.csv','a') as f:
w = csv.writer(f)
w.writerows(my_dict.items())
while True:
for channel in range(1, 14):
os.system("iwconfig " + iface + " channel " + str(channel))
sniff(iface=interface, prn=sniffmgmnt)
我使用 python 编写了我的脚本并且它运行成功。我在终端中获取、识别并打印了 addr1,
addr2,
和 addr3
。但是,我 运行 在尝试将这些结果附加到字典格式的 CSV 文件时遇到了几个错误。
这是我之前在终端中打印结果时的脚本:
#!/usr/bin/env python3
from scapy.all import *
import os
devices = set()
interface = "wlan0mon"
iface = "wlan0mon"
def sniffmgmnt(p):
if p.haslayer(Dot11):
devices.add(p.addr1)
devices.add(p.addr2)
devices.add(p.addr3)
print(devices)
while True:
for channel in range(1, 14):
os.system("iwconfig" + iface + "channel" + str(channel))
sniff(iface=interface, prn=sniffmgmnt, count=3, timeout=3, store=0)
然后我尝试将这些结果附加到字典格式的 CSV 文件中,其中 addr3
是 key:
#!/usr/bin/env python3
from scapy.all import *
from snifferCSV import CSV
import os
aps = dict()
devices = set()
headers = ['addr3', 'addr2', 'addr1']
csvFile = CSV('data.csv', headers)
interface = "wlan0mon"
iface = "wlan0mon"
def sniffmgmnt(p):
if p.haslayer(Dot11):
source.append(p.addr1);
receiving.append(p.addr2);
destination.append(p.addr3);
csvFile.addToCSV( [addr3, addr2, addr1] )
while True:
for channel in range(1, 14):
os.system("iwconfig" + iface + "channel" + str(channel))
sniff(iface=interface, prn=sniffmgmnt, count=3, timeout=3, store=0)
但是脚本只在终端输出 headers,而我的 CSV 文件仍然是空的。
下面是我的 CSV class snifferCSV.py:
class CSV(object):
csvFile = None
fileName = data.csv
def __init__(self,fileName,headers):
self.fileName = fileName
self.prepareCSVFile(headers)
def prepareCSVFile(self,headers):
self.csvFile = open(self.fileName, 'w')
headerString = self.prepareValues(headers)
self.writeToCSV(headerString)
def prepareValues(self,valueList):
line = str()
for item in valueList[:-1]:
line = line + str(item) + ','
line += valueList[-1]
return line
def addToCSV(self,valueList):
valueLine = self.prepareValues(valueList)
self.writeToCSV(valueLine)
def writeToCSV(self,line):
print(line)
self.csvFile.write(line)
我的目标是将我的结果以字典格式附加到 CSV 文件中,其中 addr3
是键。但是,我对这个过程的逻辑是错误的。有人能给我指出正确的方向吗?
提前致谢 -JJ
我听取了@coder 的建议并进行了更改
open(self.fileName, 'w')
到 open(self.fileName, 'a')
但是只有我的 headers 被附加到 csv 文件。
我结束了我的 snifferCSV.py
class,而是在我的主文件中嵌入了字典函数,我在主文件中使用了成对的 dict 构造函数和 zip 函数将我的数据附加到 csv 文件中:
#!/usr/bin/env python3
from scapy.all import *
import csv
import os
interface = "wlan0mon"
iface = "wlan0mon"
def sniffmgmnt(p):
if p.haslayer(Dot11):
keys = [(p.addr3)]
values = [(p.addr1, p.addr2)]
my_dict = dict(zip(keys, values))
with open('data.csv','a') as f:
w = csv.writer(f)
w.writerows(my_dict.items())
while True:
for channel in range(1, 14):
os.system("iwconfig " + iface + " channel " + str(channel))
sniff(iface=interface, prn=sniffmgmnt)