-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacup.py
More file actions
162 lines (139 loc) · 6.04 KB
/
Copy pathmacup.py
File metadata and controls
162 lines (139 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
# Project Name: macup
# Developer: Nolan Stone
# Date: 2026-02-11
# Purpose: This app allows users to match and record MAC prefixes
#
import requests
import os
import pathlib
import json
import random
import argparse
import sys
import platform
import datetime
plt = platform.system()
paths = {
"Linux": "/usr/local/lib/macup",
"Windows": "%USERPROFILE%\\Documents\\macup"
}
db_path = os.path.join(paths[plt], "mac-vendor.json")
db_mirror_url = "https://maclookup.app/downloads/json-database/get-db"
def main():
parser = argparse.ArgumentParser(description="macup is a (mostly) offline lookup tool for MAC address prefixes and their respective vendors.")
parser.add_argument("-i", "--init", action = "store_true", help="Initializes the utility and downloads the database. This is the only time macup requires a network connection.")
parser.add_argument("-p", "--prefix", help="Specify a prefix and return the vendor associated with that prefix")
parser.add_argument("-v", "--vendor", help="Specify a vendor and return all associated prefixes")
parser.add_argument("-r", "--rand", action = "store_true", help="Generate a random prefix and its vendor (for subversion testing purposes)")
parser.add_argument("-s", "--outfile", default="", help="Save output to file (only with -v, no extension)")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
check_database_uptodate()
if args.init:
init()
if args.prefix:
matchPrefix(args.prefix)
if args.vendor and args.outfile:
matchVendor(args.vendor, args.outfile)
if args.vendor:
matchVendor(args.vendor)
if args.rand:
randomPrefix()
# def helpMsg():
# print("macup is a (mostly) offline lookup tool for MAC address prefixes and their respective vendors.")
# print("Syntax: macup <-pvrish> <prefix/vendor>")
# print("Options:")
# print("-i | Initializes the utility and downloads the database. This is the only time macup requires a network connection.")
# print("-p | Specify a prefix and return the vendor associated with that prefix")
# print("-v | Specify a vendor and return all associated prefixes")
# print("-s | Save output to specified file (only with -v)")
# print("-r | Generate a random prefix and its vendor (for subversion testing purposes)")
# print("-h | Display this help message and end execution")
def check_database_uptodate():
path = pathlib.Path(f"{paths[plt]}/mac-vendor.json")
try:
stat = path.stat()
except Exception:
return
try:
timestamp = stat.st_birthtime
except AttributeError:
timestamp = stat.st_ctime
diff = datetime.datetime.now() - datetime.datetime.fromtimestamp(timestamp)
if diff.days > 14:
print(f"Warning: Database is out-of-date ({diff.days} days)! Please refetch with: sudo macup -i")
def init():
try:
try:
resDir = pathlib.Path(paths[plt])
resDir.mkdir()
except FileExistsError:
print("Directory already exists. Ignoring mkdir...")
print(f"Requesting database from mirror at: {db_mirror_url}")
response = requests.get(db_mirror_url)
print(f"Response received! Response status: {response.status_code}")
if response.status_code == 200:
print(f"Beginning database write to: {db_path}")
with open(db_path, "wb") as f:
print("Writing...")
f.write(response.content)
print("Database written!")
else:
print(f"Failed to download database. Server responded with status code: {response.status_code}")
except FileExistsError:
print("File already exists. Updating...")
print(f"Removing database found at: {db_path}")
os.remove(db_path)
print(f"Requesting database from mirror at: {db_mirror_url}")
response = requests.get("https://maclookup.app/downloads/json-database/get-db")
print(f"Response received! Response status: {response.status_code}")
if response.status_code == 200:
print(f"Beginning database write to: {db_path}")
with open(db_path, "wb") as f:
print("Writing...")
f.write(response.content)
print("Database written!")
else:
print(f"Failed to download database. Server responded with status code: {response.status_code}")
except PermissionError:
print("Permission denied. Please try again, running as root.")
except Exception as e:
print(f"An error occurred while attempting to initialize the database: {e}")
def matchPrefix(p):
try:
with open(db_path, "r") as f:
db = json.load(f)
for record in db:
if record["macPrefix"] == p:
print(f"{record["macPrefix"]} | {record["vendorName"]}")
except FileNotFoundError:
print("Database not found! Did you initialize macup?")
def matchVendor(v, saveToFileName = ""):
try:
with open(db_path, "r") as f:
db = json.load(f)
if saveToFileName:
with open(f"{saveToFileName}.txt", "w") as f:
for record in db:
if v.lower() in record["vendorName"].lower():
print(f"{record["macPrefix"]} | {record["vendorName"]}")
f.write(f"{record["macPrefix"]} | {record["vendorName"]}\n")
else:
for record in db:
if v.lower() in record["vendorName"].lower():
print(f"{record["macPrefix"]} | {record["vendorName"]}")
except FileNotFoundError:
print("Database not found! Did you initialize macup?")
def randomPrefix():
try:
with open(db_path, "r") as f:
db = json.load(f)
randRec = db[random.randint(0, len(db) - 1)]
print(f"{randRec['macPrefix']} | {randRec['vendorName']}")
except FileNotFoundError:
print("Database not found! Did you initialize macup?")
if __name__ == "__main__":
main()