Friday, September 15, 2023

Using Bash to Sort IPs by Subnet Uniqueness

This is a helpful Bash script to parse IP addresses by the uniqueness of their subnets. This can be quite helpful in various scenarios.

We ingest a list of IP addresses and use sort to pedantically order them on a per digit basis. Then, we take that list and use it to extract the first three octets of each IP address — the 24 bits that are used for network identification — and iterate over them, only adding IP addresses to print if the subnet for it is unique and we've not yet seen it.

#!/bin/bash

# sortBySubnet.sh

if [ "$#" -ne 1 ]; then
    echo "Usage: $0 <ip_list>"
    exit 1
fi

# get arg, make tmp outs
ip_list="$1"
sorted_ips=$(mktemp)
unique_subnets=$(mktemp)

# subnet fn 
get_subnet() {
  echo "$1" | cut -d. -f1-3
}

# sort ips by octet
sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n -u "$ip_list" > "$sorted_ips"

# if we haven't seen it, add it
while read -r ip; do
  subnet=$(get_subnet "$ip")
  if ! grep -q "^$subnet$" "$unique_subnets"; then
    echo "$subnet" >> "$unique_subnets"
    echo "$ip"
  fi
done < "$sorted_ips"

rm "$sorted_ips" "$unique_subnets"

Gist here.

No comments:

Post a Comment