Skip to content

Instantly share code, notes, and snippets.

@mailinglists35
Created March 21, 2024 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mailinglists35/5ee63690622c17000df1d0f5760f54ae to your computer and use it in GitHub Desktop.
Save mailinglists35/5ee63690622c17000df1d0f5760f54ae to your computer and use it in GitHub Desktop.
anonymize ethernet mac address
#!/bin/bash
# thanks microsoft copilot. worked from first try. prompt:
# can you write a small script in a language of your choice that takes input as defined by the name of the input file, searches for patterns that resemble an ethernet mac address including those that omit the zero, such as like when netstat on macos does, then replace the last three octets of the mac address with xx:xx:xx
# it wrote python but python requires xcode so I asked for a rewrite in bash
filename="$1"
# MAC address pattern (with optional leading zeros)
pattern="([0-9A-Fa-f]{1,2}[:-]){5}([0-9A-Fa-f]{1,2})"
# Read file line by line
while IFS= read -r line
do
if [[ $line =~ $pattern ]]; then
# Extract MAC address
mac="${BASH_REMATCH[0]}"
# Split MAC address into octets
IFS=':' read -ra octets <<< "$mac"
# Replace last three octets
new_mac="${octets[0]}:${octets[1]}:${octets[2]}:xx:xx:xx"
# Replace MAC address in line
line="${line//$mac/$new_mac}"
fi
echo "$line"
done < "$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment