Skip navigation.
Home

Perl

Resolve IP Addresses to DNS Names

Sometimes, you have textual data, like log files, with IP addresses. You sometimes want this data to show hostnames instead.

This script converts IP addresses in the standard input to hostnames. (Script is based on one I found in perlmonks.org.)


#!/usr/bin/perl -w
#
# Resolve IP addresses in web logs.
# Diego Zamboni, Feb 7, 2000
# John Kawakami, May 12, 2008

use Socket;

# Local domain mame
$localdomain = 'slaptech.net';

while (my $l = <>) {
  if ($l =~ /^(.*?)(\d+\.\d+\.\d+\.\d+)(.*?)$/) {
    $pre = $1;
    $address = $2;
    $post = $3;
    if ($cache{$address}) {
                $addr = $cache{$address};
    }
    else {
      $addr=inet_aton($address);
      if ($addr) {
        $name=gethostbyaddr($addr, AF_INET);
        if ($name) {

Perl Rocks (even yet)

Perl still rocks. People still say it's hard to read (true), but it's because the language is terse.

sub getAccountIdsOfSitesToSuspend
{
    $sql = <<EOQ;
        SELECT account_id
        FROM account
        WHERE
            account.account_balance < (SELECT triggerAmount FROM fk_suspension_rules)
            AND (
                    SELECT IF(SUM(transaction_ammount), SUM(transaction_ammount), 0)
                    FROM transaction
                    WHERE transaction_date > DATE_SUB( CURDATE(),
                        INTERVAL (SELECT paymentWindow FROM fk_suspension_rules) DAY)
                    AND transaction.account_id = account.account_id

Email Obfuscation and Shielding Script

Here's a perl script that takes email addresses as arguments, and returns javascript code that hides your email address from web spiders. The email address is also linked so it's clickable.

#! /usr/bin/perl

foreach my $email (@ARGV) {

        $email =~ s/@/ @ /;
        $email =~ s/\./ . /;

        @parts = split( ' ', $email );

        print "\n";
        print "document.write('');";
        foreach my $word (@parts) {
                print "document.write('".$word."');\n";
        }
        print "document.write('');\n";
        print "\n\n";
}

Vim is Great for Reading Code

Vim has a couple features to make it easy to read C (and PHP and Perl) code.

First is the "go to file" feature. If you move the cursor onto a file name, and type gf, you'll be taken to that file. To return to the previous file, press Control-o. The file opens are kept on a stack, so you can drill down into file, and climb back out. This is useful for reading code with "includes".

FileMirror in Perl

This is a Perl version of the VB FileMirror class shown earlier on this site. The class helps you write scripts to (semi) safely mirror or move a file system from one directory to another. It does this by building the correct destination paths, making destination directories, and deleting files only after they appear to have been copied over.

Docs are in FileMirror.txt, attached.

DriveGMover.pl is a script that uses the class.

Syndicate content