Use Perl to Match IP to Subnets

In the case that you have many IP addresses in your logs and don’t know what networks they belong to, you can easily map them using Perl.

  • You must have a database of networks to match against
  • You must have a list of IP to search in the database of networks
  • use NetAddr::IP module to help you do the heavy lifting
  • Term::ANSIcolor and Win32::Console::ANSI colorize stuff
  • Create a subroutine to convert between dotted dash format and decimal (for sorting)
  • Then loop over all of your ips and see where they fit!

Custom Perl Programming can solve tedious work like this in seconds without the risk of errors. This particular snippet was written to parse millions of IP addresses against a few thousand known networks. What will you use our Perl script that matches IP to subnets script for? Please comment if it’s helped you.

Here is the script that uses Perl to match IP addresses to subnets:

4 thoughts on “Use Perl to Match IP to Subnets

    • December 31, 2018 at 5:17 pm
      Permalink

      Thank you Johnathan.

      Reply
  • December 28, 2020 at 10:37 am
    Permalink

    Thanks for the code. One note, though, you are reusing the $found variable for two things, and not actually reporting the number of found IPs in the results. Still, it was an easy fix and got me moving on finding subnets for 6000 IPs in a list of results, which would take me days to match!

    Reply
    • January 18, 2021 at 7:32 am
      Permalink

      Thank you for the comment and pointing that out. I’ll fix and update (and add some color to it).

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.