user2956248
user2956248

Reputation: 197

Using a list to search in textfile - Python

I'm new to Python, but I'm convinced that you should learn by doing. So here goes:

I'm trying to create a small CLI-application that takes two textfiles as input. It's then supposed to create a list of common SSIDs from the file SSID.txt, and then go through Kismet.nettxt to see how many of the Access Points that have common names.

Am I on the right track here at all? This is what I have so far, which imports the SSID.txt into a variable called "ssids"

f = open('SSID.txt', "r")
s = open('Kismet.nettxt', "r")


for line in f:
    ssids = line.strip()



s.close()
f.close()

Any tips on how I should proceed from here and what to look for?

This is how the files are formatted:

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

This is how the Kismet.nettxt is formatted:

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)

Upvotes: 0

Views: 309

Answers (2)

kylieCatt
kylieCatt

Reputation: 11039

This code should do everything you asked for in the 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]))

it outputs:

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

I added 'Internet' to the list of SSID's for testing purposes.

EDIT: I have updated the section that adds to the count to deal with keys that aren't in the dictionary. I don't knwo what you want to do with ones that aren't so for now I left a pass in there

Upvotes: 0

clutton
clutton

Reputation: 640

Here are a couple of tips of how I would go about it.

  1. Read SSID.txt, create a dict of names, so you have something fast to lookup each name and store a count. This also removes the duplicates if there are any in the SSID.txt file.
  2. Read Kismet.nettxt, if line starts with has "SSID :" then take the name and lookup in the dict, if found add to the count.
  3. At this point you will have an ssids dictionary with the name and count.

The code would look something like this:

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()

Upvotes: 1

Related Questions