Count the Total Number of IPs From CIDR

Posted 805 days ago - Development, Security

After adding a few IPs to a firewall drop list, I wondered, "exactly how many IPs are in this drop list?" Since the list contained 187 entries, all in CIDR notation, adding up the total number of IPs in my head was impossible. So, I put together this little script and figured someone else out there might also benefit.

Requirements

The script requires awk, iptables, and whatmask in order to run successfully. The script also assumes all IPs are in CIDR notation; single IPs throw an error, "whatmask: "97.53.154.29" is not a valid subnet mask or wildcard bit mask!

The Script

[nullamatix ~/ipcount]:# cat /usr/bin/ipcount
#!/bin/sh
echo 'Building droplist.txt'
iptables -L INPUT -n | \
grep DROP | awk '{print $4}' | \
sed 's/^DROP.*$//' | grep -v '0.0.0.0' \
> ~/ipcount/droplist.txt

echo 'Reading droplist.txt'
cat ~/ipcount/droplist.txt |while true
do read LINE || break
whatmask $LINE | grep 'Usable IP Addresses' | \
awk '{print $6}' | sed -e 's/,//g' >> ~/ipcount/cidr-totals.txt
done

echo 'Adding cidr-totals.txt'
awk '{total+=$0} END {print total}' ~/ipcount/cidr-totals.txt
echo 'Done.'
[nullamatix ~/ipcount]:#

Installation

Setup is easy and just like any other script...

[nullamatix ~]:# mkdir ipcount
[nullamatix ~]:# vim /usr/bin/ipcount

(copy & paste the script above)

[nullamatix ~]:# chmod +x /usr/bin/ipcount
[nullamatix ~]:# which ipcount
/usr/bin/ipcount
[nullamatix ~]:#

At the time of writing, nullamatix.com is blocking a grand total of 318,516,468 IPs. That's roughly 7% of the available IPv4 address space. How many IPs are you blocking?

Word Count: 275

Tags: , , ,

Click Here to Submit a Comment

Permalink / Last Modified:

Support Nullamatix.com:

See Also:

  • 01/17/2010 -- New Tool: IP Range to CIDR
    Excerpt: "At least twice a week I find myself visiting ip2cidr.com, the IP to CIDR converter. Since the owner/author of the site hasn't release the source code, and I love a challenge, I developed my own version. The guys at the job find the tool useful, and after a ..."
  • 10/23/2009 -- DIY: Home Surveillance System with VLC
    Excerpt: "The current state of the global economy has shot the U.S. unemployment rate up - waay up. As a result, more people are willing to commit crimes in order to provide for themselves or their family. Just this year, two houses that share the street I live on were ..."
  • 08/31/2009 -- Howto: Insert Bash Command Output Into MySQL
    Excerpt: "A BlogStorm reader emailed me today, Hello, I am replying to your post on http://www.blogstorm.co.uk/how-to-scrape-pages-with-coldfusion/ Wanted to see your experience in page scraping, may need your help on a project. HOw much did you do beyond the ..."
  • 02/11/2008 -- Howto: Setup cron Jobs to Restart Lighttpd & MySQL
    Excerpt: "Over the past couple weeks MySQL crashed when spiked with large amounts of traffic. To remedy this, a cron job has been implemented to simply restart mysql and lighttpd every other day. Here's how it's done. First, if you haven't already, define a cron job ..."

0 Comments

[ RSS feed | Trackback URI | Leave a Comment ]

Leave a Comment

Comments are moderated prior to showing up. If your comment does not show up immediately, please do not attempt to resubmit. If you're redirected to the original post after pressing "Add Comment", your comment was successfully entered into the moderation queue.

Trackback Responses to This Post:

  • No Trackbacks, yet. Help Nullamatix.com by Linking to This Post.