Creates logs for APs in the network, in a temporary directory.
This file contains 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
#!/usr/bin/python | |
""" | |
A script to write log files in the /tmp/migrationlogs | |
directory, and reports changes using the difference between | |
the current AP state and the log file state, to provide | |
stations that made it into the AP range. | |
This information can be used to provide new stations that | |
have entered the AP range. | |
""" | |
from pathlib import Path | |
import os | |
import difflib | |
import re | |
import shutil | |
def log_it(wlan, content): | |
""" | |
This function creates log files and updates them | |
when changes are detected, and returns the diff. | |
""" | |
original_content = content | |
content = content.split("\n") | |
# creating a directory in the `tmp` folder to | |
# store the migration logs | |
dir_change = Path("/tmp/migrationlogs") | |
dir_change.mkdir(exist_ok=True) | |
os.chdir(dir_change) | |
# creating log files to monitor the network | |
path = Path(f"log-{wlan}.txt") | |
change_required = True | |
refreshed_stations = set() | |
# checks if the log file for the AP exists or | |
# not, and if it doesn't, create it. | |
if path.is_file(): | |
with open(f"log-{wlan}.txt", "r") as f: | |
file_contents = f.read().split("\n") | |
f.close() | |
# if no change in the contents of the log, | |
# then there are no changes required | |
if content == file_contents: | |
change_required = False | |
# If changes are requred, write to the log | |
# file, get the difference between the | |
# previous version and the new version. | |
if change_required == True: | |
with open(f"log-{wlan}.txt", "w") as f: | |
f.write(original_content) | |
f.close() | |
d = difflib.Differ() | |
# If a station has been added, append it | |
# to the list which is checked for changes | |
for entry in list(d.compare(file_contents, content)): | |
if entry[0] != "+": | |
continue | |
station_id = entry[10:27] | |
if re.search("..:..:..:..:..:..", station_id): | |
refreshed_stations.add(station_id) | |
else: | |
with open(f"log-{wlan}.txt", "w") as f: | |
f.write(original_content) | |
f.close() | |
return refreshed_stations | |
def book_it(): | |
""" | |
Remove the directory where the migration logs | |
are present entirely. This function is triggered | |
when the migrator is shut down. | |
""" | |
shutil.rmtree("/tmp/migrationlogs") | |
if __name__ == "__main__": | |
# log_it('wlan', 'testing123') | |
book_it() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment