Working with many docker containers on one machine I needed to get list of free IP addresses in specific docker network. This is the way:
comm -3 <(nmap -sL 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}' | sort) <(nmap -sn 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}' | sort) | sort -V
172.30.0.9
172.30.0.10
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15
Let me explain.
This generates the list of IPs in subnetwork:
nmap -sL 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}'
172.30.0.8
172.30.0.9
172.30.0.10
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15
This generates list of used IPs in subnetwork:
nmap -sn 172.30.0.10/29 | awk '/Nmap scan report/{print $NF}'
172.30.0.8
The comm compares two lists and suppress common lines
comm - compare two sorted files line by line
-3 suppress column 3 (lines that appear in both files)
One more example:
cat ip_pool.txt
172.30.0.8
172.30.0.9
172.30.0.10
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15
cat ip_used.txt
172.30.0.8
172.30.0.10
comm -3 <(cat ip_pool.txt | sort) <(cat ip_used.txt | sort) | sort -V
172.30.0.9
172.30.0.11
172.30.0.12
172.30.0.13
172.30.0.14
172.30.0.15