How Do I Get Perl Counter Script to Collect IP Addresses?

Perl is a powerful scripting language that can be used to count occurrences in files, such as IP addresses. If you want your counter script to tally up the number of IP addresses in a file for example, you can use regular expressions to do the task; they make sorting out specific text and numbers a lot easier. If you want to store those IP addresses, you can then append them to a text file.

Instructions

    • 1

      Open your Perl counter script.

    • 2

      Create a regular expression that will match IP addresses from the lines you're reading in:

      ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)

      This will match any IPv4 IP address.

    • 3

      Increase the counter every time an IP address is detected:

      if ($line =~ m/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/)
      {
      $counter++;
      }

    • 4

      Add the IP address to your list:

      open (YOURFILE, '>>IPaddresses.txt');
      print YOURFILE $1;
      close (YOURFILE);

      This will append the new IP address to your external file. Your final code segment should look like this:

      if ($line =~ m/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/)
      {
      open (YOURFILE, '>>IPaddresses.txt');
      print YOURFILE $1;
      close (YOURFILE);
      $counter++;
      }

Related Searches:

References

Comments

Related Ads

Featured