Created
January 27, 2023 08:44
-
-
Save shriyanss/1c0007c1822bec471e1d1c5c771f9bc9 to your computer and use it in GitHub Desktop.
Sort the output of scan.sh (iwlist in CSV) to be plotted on map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| gps_csv = "gps.csv" | |
| wifi_csv = "wifi.csv" | |
| # define start and end of kml file | |
| start_kml = """<?xml version="1.0" encoding="UTF-8"?> | |
| <kml xmlns="http://www.opengis.net/kml/2.2"> | |
| <Document> | |
| <name>WiFi APs</name> | |
| <Style id="icon-1899-0288D1-normal"> | |
| <IconStyle> | |
| <color>ffd18802</color> | |
| <scale>1</scale> | |
| <Icon> | |
| <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> | |
| </Icon> | |
| <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> | |
| </IconStyle> | |
| <LabelStyle> | |
| <scale>0</scale> | |
| </LabelStyle> | |
| </Style> | |
| <Style id="icon-1899-0288D1-highlight"> | |
| <IconStyle> | |
| <color>ffd18802</color> | |
| <scale>1</scale> | |
| <Icon> | |
| <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> | |
| </Icon> | |
| <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> | |
| </IconStyle> | |
| <LabelStyle> | |
| <scale>1</scale> | |
| </LabelStyle> | |
| </Style> | |
| <StyleMap id="icon-1899-0288D1"> | |
| <Pair> | |
| <key>normal</key> | |
| <styleUrl>#icon-1899-0288D1-normal</styleUrl> | |
| </Pair> | |
| <Pair> | |
| <key>highlight</key> | |
| <styleUrl>#icon-1899-0288D1-highlight</styleUrl> | |
| </Pair> | |
| </StyleMap> | |
| <Style id="icon-1899-A52714-normal"> | |
| <IconStyle> | |
| <color>ff1427a5</color> | |
| <scale>1</scale> | |
| <Icon> | |
| <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> | |
| </Icon> | |
| <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> | |
| </IconStyle> | |
| <LabelStyle> | |
| <scale>0</scale> | |
| </LabelStyle> | |
| </Style> | |
| <Style id="icon-1899-A52714-highlight"> | |
| <IconStyle> | |
| <color>ff1427a5</color> | |
| <scale>1</scale> | |
| <Icon> | |
| <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> | |
| </Icon> | |
| <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> | |
| </IconStyle> | |
| <LabelStyle> | |
| <scale>1</scale> | |
| </LabelStyle> | |
| </Style> | |
| <StyleMap id="icon-1899-A52714"> | |
| <Pair> | |
| <key>normal</key> | |
| <styleUrl>#icon-1899-A52714-normal</styleUrl> | |
| </Pair> | |
| <Pair> | |
| <key>highlight</key> | |
| <styleUrl>#icon-1899-A52714-highlight</styleUrl> | |
| </Pair> | |
| </StyleMap> | |
| <Style id="icon-1899-FBC02D-normal"> | |
| <IconStyle> | |
| <color>ff2dc0fb</color> | |
| <scale>1</scale> | |
| <Icon> | |
| <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> | |
| </Icon> | |
| <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> | |
| </IconStyle> | |
| <LabelStyle> | |
| <scale>0</scale> | |
| </LabelStyle> | |
| </Style> | |
| <Style id="icon-1899-FBC02D-highlight"> | |
| <IconStyle> | |
| <color>ff2dc0fb</color> | |
| <scale>1</scale> | |
| <Icon> | |
| <href>https://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href> | |
| </Icon> | |
| <hotSpot x="32" xunits="pixels" y="64" yunits="insetPixels"/> | |
| </IconStyle> | |
| <LabelStyle> | |
| <scale>1</scale> | |
| </LabelStyle> | |
| </Style> | |
| <StyleMap id="icon-1899-FBC02D"> | |
| <Pair> | |
| <key>normal</key> | |
| <styleUrl>#icon-1899-FBC02D-normal</styleUrl> | |
| </Pair> | |
| <Pair> | |
| <key>highlight</key> | |
| <styleUrl>#icon-1899-FBC02D-highlight</styleUrl> | |
| </Pair> | |
| </StyleMap>""" | |
| end_kml = """</Document> | |
| </kml>""" | |
| data_kml = "" | |
| # store gps data in a list | |
| gpss = [] | |
| with open(gps_csv, 'r') as file: | |
| for line in file: | |
| line = line.replace('\n', '') | |
| gpss.append(line.split(',')) | |
| # store wifi data in a list | |
| wifis = [] | |
| with open(wifi_csv, 'r') as file: | |
| for line in file: | |
| line = line.replace('\n', '') | |
| wifis.append(line.split(',')) | |
| # find all unique networks | |
| ap_names = [] | |
| for data in wifis: | |
| ap_names.append(data[1]) | |
| ap_names = list(dict.fromkeys(ap_names)) | |
| # start processing data | |
| for network in ap_names: | |
| wifi_data = [] | |
| channel = 0 | |
| encryption_status = "" | |
| encryption_type = "" | |
| for wifi in wifis: | |
| if wifi[1] == network: | |
| wifi_data.append(wifi) | |
| channel = int(wifi[2]) | |
| encryption_status = wifi[7] | |
| encryption_type = wifi[8] | |
| # check SSIDs | |
| ssids = [] | |
| for wifi in wifis: | |
| if wifi[1] == network: | |
| ssids.append(wifi[0]) | |
| ssids = list(dict.fromkeys(ssids)) | |
| if len(ssids) == 1: | |
| ssid = ssids[0] | |
| else: | |
| ssid = "" | |
| for i in ssids: | |
| if ssid == "": | |
| ssid = i | |
| else: | |
| ssid = ssid + "<br>" + i | |
| # find the highest quality | |
| highest_quality = "0/1" | |
| for wifi in wifi_data: | |
| quality = wifi[3].split('/') | |
| if int(quality[0])/int(quality[1]) > float(int(highest_quality.split('/')[0])/int(highest_quality.split('/')[1])): | |
| highest_quality = wifi[3] | |
| # find the highest signal strength | |
| highest_signal = -1000 | |
| timestamp = "" | |
| for wifi in wifi_data: | |
| signal = int(wifi[4]) | |
| if signal > highest_signal: | |
| highest_signal = signal | |
| timestamp = wifi[6] | |
| # extract the time | |
| time = re.search("[0-9]{2}:[0-9]{2}:[0-9]{2}", timestamp).group(0) | |
| # find GPS position | |
| latitude = "" | |
| longitude = "" | |
| for gps in gpss: | |
| if time in gps[1]: | |
| latitude = str(gps[2]) | |
| longitude = str(gps[3]) | |
| # prepare the output | |
| # following will be printed:- | |
| # - AP name as title | |
| # - Rest as description, that includes: | |
| # - SSID | |
| # - Channel | |
| # - Quality | |
| # - Signal level | |
| # - Encryption status | |
| # - Encryption type | |
| # - Timestamp | |
| if encryption_status == "on": | |
| style_url = ("#icon-1899-0288D1") | |
| else: | |
| style_url = ("#icon-1899-A52714") | |
| if encryption_type == "wep": | |
| style_url = ("#icon-1899-FBC02D") | |
| data_kml = data_kml + f"""<Placemark> | |
| <name>{network}</name> | |
| <description> | |
| <![CDATA[SSID: {ssid}<br>Channel: {channel}<br>Quality: {highest_quality}<br>Signal level: {highest_signal}<br>Encryption status: {encryption_status}<br>Encryption type: {encryption_type}<br>Latitude: {latitude}<br>Longitude: {longitude}]]> | |
| </description> | |
| <styleUrl>{style_url}</styleUrl> | |
| <Point> | |
| <coordinates> | |
| {longitude},{latitude},0 | |
| </coordinates> | |
| </Point> | |
| </Placemark>""" | |
| print(start_kml) | |
| print(data_kml) | |
| print(end_kml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment