I'm writing a script to extract information from BusyBox v1.25.1/Linux 2.6.36/router where the user can simply run the script and copy/paste the output into a submission form to request support. The script it's essentially a list of commands like route
, ifconfig
, etc. Because of the busybox implication I'm restricted to /bin/sh.
I'm trying to find a smart way to automatically mask all the public IP addresses only from the output. Replace the full IP would be already good but if it's possible I would be looking for a command to pipe at the end of each command (even an internal function) to simply replace let's say the first two octects e.g.
80.80.80.80
with
XX.XX.80.80
A nice to have feature would be to replace the same number of digits to retain formatting where possible. e.g.
8.8.8.8=X.X.X.X
80.80.8.8=XX.XX.X.X
180.180.80.8=XXX.XXX.XX.X
practical example:
root@router:/proc# route | filtering-goes-here
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
XX.XX.168.1 * 255.255.255.255 UH 0 0 0 vlan2
172.16.9.0 * 255.255.255.0 U 0 0 0 br1
10.10.9.0 * 255.255.255.0 U 0 0 0 br0
10.114.126.0 * 255.255.254.0 U 0 0 0 tun11
XX.XX.168.0 * 255.255.252.0 U 0 0 0 vlan2
10.10.0.0 * 255.255.0.0 U 0 0 0 tinc
127.0.0.0 * 255.0.0.0 U 0 0 0 lo
default cpc86269 0.0.0.0 UG 0 0 0 vlan2
Thanks!!
Does a
sed
script like this work for you?Here is an example run,
This is going to convert all IP addresses of the form
a.b.c.d
into equivalent widthx.x.x.x
pattern. If you need to skip some IP addresses those would need exclusion rules in the script.If the
___
pattern is part of your possible output in the context used here, you would need to change to a different 'context' that is not possible in the input to the script.Update: for public IP masking
Try this
sed
script replacement to the above basic one.Note: This will also
x
-out the netmasks with one exception. I've not added240.0.0.0
to the list.Update2: If you want to just mask the first IP-address on each line
This is for a
route
output case where the first column is IP addresses.Replace the first line in the updated
sed
script with,On OPs suggestion, leaving also the mask-for-private-IPs script here