-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac_address_vendor_lookup.py
179 lines (157 loc) · 6.78 KB
/
mac_address_vendor_lookup.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
"""
This Python script is designed to query the maclookup datebase located at https://api.maclookup.app
for information related to a specific MAC (Media Access Control) address.
"""
__author__ = 'John Bumgarner'
__date__ = 'February 15, 2024'
__status__ = 'Production'
__license__ = 'GPL-3'
__copyright__ = "Copyright (C) 2024 John Bumgarner"
##################################################################################
# Date Completed: February 15, 2024
# Author: John Bumgarner
#
# Date Last Revised: July 22, 2024
# Revised by: John Bumgarner
##################################################################################
##################################################################################
# “AS-IS” Clause
#
# Except as represented in this agreement, all work produced by Developer is
# provided “AS IS”. Other than as provided in this agreement, Developer makes no
# other warranties, express or implied, and hereby disclaims all implied warranties,
# including any warranty of merchantability and warranty of fitness for a particular
# purpose.
##################################################################################
##################################################################################
# Python imports required for basic operations
##################################################################################
# Standard library imports
import sys
from time import sleep
from typing import Union
from random import randint
# Third-party imports
import requests
# Local or project-specific imports
from utilities.colorized_text import colorized_text
class MacAddressLookup:
"""
Purpose
----------
This Python class is used to query the maclookup datebase located at https://api.maclookup.app
for information related to a specific MAC (Media Access Control) address.
Usage Examples
----------
# String Example
data = MacAddressLookup('04:7b:cb:3b:75:94').lookup_address_information()
# do something with the results
# List Example
mac_addresses = ['04:7b:cb:3b:75:94', '04:7b:cb:64:94:a5']
data = MacAddressLookup(mac_addresses).lookup_address_information()
# do something with the results
Parameters
----------
:param lookup_value: Input data containing the MAC addresses to obtain information for
"""
def __init__(self,
lookup_value: str | list = ''):
self.lookup_value = lookup_value
data: dict = {}
@classmethod
def _get_json_data(cls, hardware_id) -> Union[dict, None]:
sleep(randint(a=1, b=3))
response = requests.get(url=f'https://api.maclookup.app/v2/macs/{hardware_id}', timeout=(5, 10))
if response.status_code == 400:
colorized_text(text="An unknown error has occurred.", color='red')
sys.exit(1)
elif response.status_code == 401:
colorized_text(text="An Unauthorized Request has occurred.", color='red')
sys.exit(1)
elif response.status_code == 429:
colorized_text(text="Too Many Requests with the Rate Limit period.", color='red')
sys.exit(1)
elif response.status_code == 200:
cls.data = response.json()
return cls.data
return None
@classmethod
def _get_registered_organization(cls) -> Union[str, None]:
"""
Obtains the name of the registered organization associated with the
MAC address being queried.
:return: registered organization name
:rtype: string
"""
try:
if cls.data['isRand'] is True:
return "randomly assigned"
elif cls.data['isRand'] is False:
return cls.data['company']
except KeyError:
colorized_text(text="Registered organization is not present in JSON data", color='red')
return None
@classmethod
def _get_registered_organization_address(cls) -> Union[str, None]:
"""
Obtains the address of the registered organization associated with the
MAC address being queried.
:return: address of registered organization
:rtype: string
"""
try:
if not cls.data['address']:
return "address unavailable"
elif cls.data['address']:
return cls.data['address']
except KeyError:
colorized_text(text="Registered organization's address is not present in JSON data", color='red')
return None
@classmethod
def _get_registered_organization_country(cls) -> Union[str, None]:
"""
Obtains the country code for the registered organization associated
with the MAC address being queried.
:return: country code of registered organization
:rtype: string
"""
try:
if not cls.data['country']:
return "country unavailable"
elif cls.data['country']:
return cls.data['country']
except KeyError:
colorized_text(text="Registered organization's country is not present in JSON data", color='red')
return None
def lookup_address_information(self) -> Union[dict | list[dict] | None]:
"""
Searches the JSON data for specific data elements associated with the
MAC address being queried.
:return: registered agent information
:rtype: tuple
"""
if isinstance(self.lookup_value, str):
MacAddressLookup._get_json_data(self.lookup_value)
registered_organization = MacAddressLookup._get_registered_organization()
organization_address = MacAddressLookup._get_registered_organization_address()
country = MacAddressLookup._get_registered_organization_country()
data_elements = {'mac_address': self.lookup_value,
'registered_organization': registered_organization,
'organization_address': organization_address,
'country': country}
return data_elements
elif isinstance(self.lookup_value, list):
data_elements = []
for item in self.lookup_value:
MacAddressLookup._get_json_data(item)
registered_organization = MacAddressLookup._get_registered_organization()
organization_address = MacAddressLookup._get_registered_organization_address()
country = MacAddressLookup._get_registered_organization_country()
data = {'mac_address': item,
'registered_organization': registered_organization,
'organization_address': organization_address,
'country': country}
data_elements.append(data)
return data_elements
return None