我是Python的新手,但我确信你应该通过实践来学习。所以这里:
我正在尝试创建一个小型CLI应用程序,它将两个文本文件作为输入。 然后应该从文件SSID.txt创建一个公共SSID列表,然后通过Kismet.nettxt查看有多少具有通用名称的访问点。
我在这里的正确轨道吗?这就是我到目前为止所做的,它将SSID.txt导入名为“ssids”的变量
f = open('SSID.txt', "r")
s = open('Kismet.nettxt', "r")
for line in f:
ssids = line.strip()
s.close()
f.close()
有关如何从这里开始以及寻找什么的任何提示?
这是文件的格式:
SSID.txt:
linksys
<no ssid>
default
NETGEAR
Wireless
WLAN
Belkin54g
MSHOME
home
hpsetup
smc
tsunami
ACTIONTEC
orange
USR8054
101
tmobile
<hidden ssid>
SpeedStream
linksys-g
3Com
这就是Kismet.nettxt的格式:
Network 3: BSSID REMOVED
Manuf : Siemens
First : Sun Dec 29 20:59:46 2013
Last : Sun Dec 29 20:59:46 2013
Type : infrastructure
BSSID : REMOVED
SSID 1
Type : Beacon
SSID : "Internet"
First : Sun Dec 29 20:59:46 2013
Last : Sun Dec 29 20:59:46 2013
Max Rate : 54.0
Beacon : 10
Packets : 2
Encryption : WPA+PSK
Encryption : WPA+TKIP
Channel : 5
Frequency : 2432 - 2 packets, 100.00%
Max Seen : 1000
LLC : 2
Data : 0
Crypt : 0
Fragments : 0
Retries : 0
Total : 2
Datasize : 0
Last BSSTS :
Seen By : wlan0 (wlan0mon)
答案 0 :(得分:1)
以下是关于如何实现这一目标的几个提示。
代码看起来像这样:
f = open('SSID.txt', "r")
s = open('Kismet.nettxt', "r")
ssids = {} # Create dictionary
for line in f:
# Add each to the dictionary,
# if there are duplicates this effectively removes them
ssids[line.strip()] = 0
for line in s:
# Check the lines in the kismet file that start with the SSID we are after
if line.strip().startswith('SSID :'):
# Break the entry at : and take the second part which is the name
kismet = line.split(':')[1].strip()
# Remove the " marks from front and back and lookup in the ssids
# add to the count if you find it.
if kismet[1:-1] in ssids:
ssids[kismet[1:-1]] += 1
s.close()
f.close()
答案 1 :(得分:0)
此代码应该执行您在OP中要求的所有内容:
try:
with open('SSID.txt', 'r') as s:
ssid_dict = {}
for each_line in s:
ssid_dict[each_line.strip()] = 0 #key: SSID value: count
except FileNotFoundError:
pass
try:
with open('kissmet.nettext', 'r') as f:
try:
for each_line in f:
each_line = each_line.strip()
if each_line.startswith("SSID") and ':' in each_line: #checks for a line that starts with 'SSID' and contains a ':'
val = each_line.split(':')[1].replace('"', '').strip() #splits the line to get the SSID, removes the quotes
if ssid_dict[val]:
ssid_dict[val] += 1 #adds one to the count in the dictionary
else:
pass#I don't know what you want to do here
except KeyError as err:
print("Key error" + str(err))
except FileNotFoundError:
pass
for key in ssid_dict:
print(str(key) + " " + str(ssid_dict[key]))
输出:
Wireless 0
101 0
Belkin54g 0
tsunami 0
tmobile 0
<hidden ssid> 0
linksys-g 0
smc 0
hpsetup 0
ACTIONTEC 0
SpeedStream 0
Internet 1
3Com 0
home 0
USR8054 0
<no ssid> 0
WLAN 0
NETGEAR 0
default 0
MSHOME 0
linksys 0
orange 0
为了测试目的,我将“Internet”添加到SSID列表中。
编辑:我更新了添加到计数的部分,以处理字典中没有的键。我不知道你想要做什么不是现在我在那里留下pass