Created
March 28, 2023 19:40
-
-
Save stboch/9adfbf28e86b7b284d2cb810b09a7e06 to your computer and use it in GitHub Desktop.
format_macaddress
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
{ | |
"create_time": "2023-03-28T19:36:11.883314+00:00", | |
"custom_function_id": "ba821c29dd95204206d5240ea66d1ecf4c4ef9eb", | |
"description": "Ensure mac address is in format of chosen input \ndefaults to - if not provided", | |
"draft_mode": false, | |
"inputs": [ | |
{ | |
"contains_type": [ | |
"*" | |
], | |
"description": "Enter MAC Address", | |
"input_type": "item", | |
"name": "macaddress", | |
"placeholder": "MAC Address" | |
}, | |
{ | |
"contains_type": [ | |
"*" | |
], | |
"description": "Choose your mac address seperator - or : ", | |
"input_type": "item", | |
"name": "separator", | |
"placeholder": "-" | |
} | |
], | |
"outputs": [ | |
{ | |
"contains_type": [ | |
"mac address" | |
], | |
"data_path": "macaddress", | |
"description": "Formatted mac address" | |
} | |
], | |
"outputs_type": "item", | |
"platform_version": "6.0.0.114895", | |
"python_version": "3" | |
} |
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
def format_macaddress(macaddress=None, separator=None, **kwargs): | |
""" | |
Ensure mac address is in format of chosen input | |
defaults to - if not provided | |
Args: | |
macaddress (CEF type: *): Enter MAC Address | |
separator (CEF type: *): Choose your mac address seperator - or : | |
Returns a JSON-serializable object that implements the configured data paths: | |
macaddress (CEF type: mac address): Formatted mac address | |
""" | |
############################ Custom Code Goes Below This Line ################################# | |
import json | |
import phantom.rules as phantom | |
import re | |
outputs = {} | |
mac = macaddress | |
separator = "-" if separator is None else separator | |
assert separator == ":" or separator == "-", "Invalid Separator" | |
# Write your custom code here... | |
mac = re.sub('[.:-]', '', mac).lower() # remove delimiters and convert to lower case | |
mac = ''.join(mac.split()) # remove whitespaces | |
assert len(mac) == 12, "Invalid Length" # length should be now exactly 12 (eg. 008041aefd7e) | |
assert mac.isalnum(), "Invalild MAC values" # should only contain letters and numbers | |
# convert mac in hypen form (eg. 00-80-41-ae-fd-7e) | |
mac = separator.join(["%s" % (mac[i:i+2]) for i in range(0, 12, 2)]) | |
outputs = { "macaddress": mac } | |
# Return a JSON-serializable object | |
assert json.dumps(outputs) # Will raise an exception if the :outputs: object is not JSON-serializable | |
return outputs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment