How to mask mac, ipv4 and ipv6 addresses in text file before posting

I am trying to use networkd-dispatcher to do DDNS updates.

While working with the author to debug, I need to attach outputs like networkctl to GitLab.

How can I easily mask the captioned address before posting?

For example, in my system, the output of
# networkctl status --no-pager --no-legend – br1

contains 21 address in total.
Unique counts are:
mac x 1
ipv4 x 2
ipv6 x 7

I can only doing it manually at the moment.

You could pipe the output of your commands through sed:

yourcommand | sed -r 's/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/<ipv4>/'

replaces any instance of an ipv4 address with <ipv4> (technically it replaces anything that matches <1-3 digits>.<1-3 digits>.<1-3 digits>.<1-3 digits>, so not ONLY valid ipv4 addresses).

You can construct similar expressions for mac addresses and ipv6 ( [0-9a-fA-F]{n,m} matches a hexadecimal digit n to m times).

3 Likes

Thanks!

The ipv4 is working.

ipv6 is so difficult as it is not a fixed format.
The number of : in an ipv6 address not are fixed.

Yeah, ipv6 is a bit tricky. Something like this could work:

sed -r 's/(([0-9a-fA-F]{,4}:)*)[0-9a-fA-F]{,4}:[0-9a-fA-F]{,4}/<ipv6>/'

This is not a very clean solution, as it doesn’t care about the total number of colons or hex blocks, so will match much more than just valid ipv6 addresses. Depending on how your output looks like, this might be enough. I’m sure there are more elegant solutions out there, try searching for ‘ipv6 regular expression’ or similar.

If you’re also going to mask MAC addresses with their own ‘keyword’, you likely will need to replace those first - the above WILL match a MAC address if it uses colons as separators.

2 Likes