Issue
I am starting with a string of MAC addresses and a list of MAC addresses:
s3_mac_string = "[['50:9a:4c:17:b4:f5', '54:13:79:70:6b:97', '54:13:79:70:6b:98', '56:13:79:70:6b:97', '66:13:79:70:6b:97'], ['c8:34:8e:72:3f:89', 'c8:34:8e:72:3f:8a', 'ca:34:8e:72:3f:89'], ['54:ee:75:b7:9f:a4', 'f0:d5:bf:bf:65:ee', 'f0:d5:bf:bf:65:ef', 'f2:d5:bf:bf:65:ee']]"
huntress_unseen_mac = [['80:86:f2:da:dd:ce', '80:86:f2:da:dd:cf', '80:86:f2:da:dd:d2', '82:86:f2:da:dd:ce', 'ec:f4:bb:40:25:be'], ['10:60:4b:73:39:db'], ['a4:bb:6d:3f:ed:10'], ['a4:bb:6d:3e:7b:e1'], ['d0:3c:1f:77:84:b3', 'd0:3c:1f:77:84:b4', 'f0:d5:bf:bf:65:ee', 'd0:3c:1f:77:84:b7']]
I am then converting string1
to a list with this function:
def Convert(string):
string = string.replace("[", "").replace("]", "").replace('"', "").replace("'", "")
li = list(string.split(","))
return li
My main():
def main():
s3_mac_string = get_mac_addresses_from_s3()
huntress_unseen_mac = get_unseen_mac_huntress()
s3_mac_list = Convert(s3_mac_string)
huntress_unseen_mac_lower = [
str(huntress_unseen_mac).lower().strip()
for huntress_unseen_mac in huntress_unseen_mac
]
s3_mac_list_lower = [str(s3_mac).lower().strip() for s3_mac in s3_mac_list]
common_macs = []
for mac in huntress_unseen_mac_lower:
if mac in s3_mac_list_lower:
common_macs.append(mac)
print(f"Common MACs: {common_macs}")
The printing of the common macs is just printing an empty list eventhough there are commonalities between the lists. As a troubleshooting step I appended the huntress_unseen_mac_lower with huntress_unseen_mac_lower.appent("<MAC THAT IS IN S3_MAC_LIST")
I also tried using set intersection initially and that didn't work either.
huntress_unseen_mac_lower = [
str(huntress_unseen_mac).lower() for huntress_unseen_mac in huntress_unseen_mac
]
s3_mac_list_lower = [str(s3_mac).lower() for s3_mac in s3_mac_list]
common_macs = set(huntress_unseen_mac_lower).intersection(s3_mac_list_lower)
print(f"Common Macs: {list(common_macs)}")
Is there something I am missing?
Solution
You can build a list of lists from s3_mac_string using ast.literal_eval.
From that, build a set from all the sub-elements.
Build another set from huntress_unseen_mac (which is already a list of lists).
Print the intersection of the two sets.
from ast import literal_eval
def as_set(_list: list[list[str]]) -> set:
s = set()
s.update(*_list)
return s
s3_mac_string = "[['50:9a:4c:17:b4:f5', '54:13:79:70:6b:97', '54:13:79:70:6b:98', '56:13:79:70:6b:97', '66:13:79:70:6b:97'], ['c8:34:8e:72:3f:89', 'c8:34:8e:72:3f:8a', 'ca:34:8e:72:3f:89'], ['54:ee:75:b7:9f:a4', 'f0:d5:bf:bf:65:ee', 'f0:d5:bf:bf:65:ef', 'f2:d5:bf:bf:65:ee']]"
huntress_unseen_mac = [['80:86:f2:da:dd:ce', '80:86:f2:da:dd:cf', '80:86:f2:da:dd:d2', '82:86:f2:da:dd:ce', 'ec:f4:bb:40:25:be'], ['10:60:4b:73:39:db'], ['a4:bb:6d:3f:ed:10'], ['a4:bb:6d:3e:7b:e1'], ['d0:3c:1f:77:84:b3', 'd0:3c:1f:77:84:b4', 'f0:d5:bf:bf:65:ee', 'd0:3c:1f:77:84:b7']]
s3_mac_set = as_set(literal_eval(s3_mac_string))
h_mac_set = as_set(huntress_unseen_mac)
print(s3_mac_set & h_mac_set)
Output:
{'f0:d5:bf:bf:65:ee'}
Answered By - CtrlZ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.