"pipe" output of some program to Perl script for processing

Sometimes you need to "pipe" output of some program to Perl script for processing.

bash $ nslookup | filterDNS.pl

In this example we will show you how to process output or detect that there is no calling application.

bash $ nslookup www.google.com
Server: condor
Address: 192.168.1.1
Non-authoritative answer:
Name: www.l.google.com
Addresses: 72.14.204.147, 72.14.204.104, 72.14.204.103, 72.14.204.99
Aliases: www.google.com
bash $

We are planning to process "nslookup" output, and retrieve IP addresses. We will call our script "filterDNS.pl".

Our script will process input redirected from nslookup command, and if input will not be detected for 10 seconds, then it will thow error.

bash $ cat filterDNS.pl
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
#----------------------
# Name: filterDNS.pl
# Date: 05/12/2009
# By: Paul Greenberg
# Description: This script is used for "nslookup" command processing
#----------------------
#----------------------
#-- Global variables
#----------------------
my $help = "";
my $hostname = "dummy";
my @ips = ();
GetOptions ( 'help' => \$help );
#----------------------
#-- Pre-routine
#----------------------
if ($help eq "1") {
usage();
exit 1;
}
#----------------------
#-- Global functions
#----------------------
sub usage {
print <<ENDUSAGE;
Usage:
nslookup www.google.com | filterDNS.pl
ENDUSAGE
return;
}
sub stdin_ready() {
my $rin = "";
my $timeout = 10;
vec($rin,fileno(STDIN),1) = 1;
if (select(my $rout=$rin, undef, undef, $timeout) < 1) {
print "NO INPUT\n";
exit 1;
}
}
#----------------------
#-- Main routine
#----------------------
stdin_ready();
while (<STDIN>) {
my $line = $_;
chomp($line);
# Parsing Hostname
# Name: www.l.google.com
if ($line =~ m/Name:\s+(.*)/) {
$hostname = $1;
}
# Parsing IP addresses
# Addresses: 72.14.204.147, 72.14.204.104, 72.14.204.103, 72.14.204.99
elsif ($line =~ m/Addresses:\s(.*)/) {
my $ipstring = $1;
$ipstring =~ s/\s+//;
@ips = split (/,/, $ipstring);
}
else {
# do nothing
}
}
if ($hostname eq "dummy") {
print "unable to find hostname!";
usage();
exit 1;
}
print "$hostname:".join(',',@ips)."\n";
#----------------------
#-- Completed
#----------------------
exit 0;

Let's see our script in action:

bash $ nslookup www.google.com | filterDNS.pl
www.l.google.com:72.14.204.103, 72.14.204.104, 72.14.204.147, 72.14.204.99
bash $ nslookup www.google23455.com | filterDNS.pl
*** sasyp2.ms.com can't find www.google23455.com: Non-existent host/domain
unable to find hostname!
Usage:
nslookup www.google.com | filterDNS.pl

Moving your apps to Amazon or Miscrosoft Clouds?

We can help you analyze your existing infrastructure, identify the cost savings we can achieve by migrating to a cloud provider. We can then execute end-to-end migration plan of your infrastructure and bringing down your TCO.

Cloud Computing

Ready for IPv6 Migration?

The Internet is running out of the equivalent of phone numbers - familiar problem, non-trivial solution.

The world has to move to IPv6, with its 128-bit addresses. But that's easier said than done.

IPv6 Migration

Are you fluent in "Linux"?

Learn Linux from a leading expert and quickly master you Linux skills.

Learn how to simplify your workflow and increase your productivity using tips and techniques of the pros.

Ideal training for Corporate IT Beginners and Advanced IT Admins alike.

Corporate Linux Training

Who's Online

We have 3 guests and no members online