Sort IP Addresses Using Perl

When dealing with lists of IP addresses, it was common for the numbers to merge together and lose track of duplicates, bad IP addresses etc. This Perl script helps you extract valid IPs from a block of text.

Sort IP Addresses Using Perl

This program gives you a Tk Box to paste some IPv4 looking text. After you have a block of text with IP addresses in it, you click “sort” and here is what happens:

  • Duplicates IP Are Removed (Deduplication)
  • Invalid IP/Text is skipped
  • IP Addresses are sorted
  • Valid IP printed out
  • Count/statistics of lines, ip addresses, etc

What to Learn from this Perl Code:

We try to follow the Perl style guide by Damon Conway, but we have our preferences too. This is more of a lesson in how to solve simple problems. This would not be a great way to sort through records that have millions of entries – for that you’d want to optimize certain aspects and verify you are not soaking up too much memory. For the purpose of a few hundred IP addresses though, this script works great.

  • Tk (message boxes, entry boxes, buttons, basic form setup)
  • IPv4 Extraction
  • Operating System Detection
  • Win32 Console Management
  • Sorting and Working with Perl Hashes

Typical Use

When working for a client, we processed many firewall requests (sometimes up to 50 changes per day). They often provided us with spreadsheets that were manually typed up and contained duplicates, errors, and sometimes missing ip addresses. It was maddening to try and review a list and not make mistakes. Perl is great for automation tasks like this, even if Tk is an outdated interface – it works!

Known Issues

Valid IP addresses come in many forms. Perhaps you don’t want anything except the x.x.x.x format? We are only looking at the x.x.x.x format! So if you need a different type of input, you will have to rewrite your error checking!

Source Code for Sorting IP Addresses:

(Don’t forget that this removes duplicates and matches valid IP only too!)

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.