#!/bin/env perl # # call: # script.pl name.tld # use warnings; use strict; use IPC::ShareLite; use Storable qw(thaw freeze); use File::Temp qw(tempfile); use Fcntl ':flock'; use POSIX ":sys_wait_h"; # allow the child-processes to store their results # so that the parent can access them my $store = new IPC::ShareLite( -key => "__dnssearch__",                 -create  => 'yes',               -destroy => 'no' ) or die "Store: " . $!; my $results = {}; $store->store(freeze($results)); # fake addresses, tested with two real servers my @dnss = qw(111.111.111.111        222.222.222.222        111.222.111.222        222.111.222.111); my $host = $ARGV[0]; my @children = (); # used to "flock" access to the shared storage in the children my $lock = tempfile(); foreach (@dnss) { my $dns = $_; my $parent = fork(); if (!$parent) {         my $command = "nslookup -type=A $host $dns 2>/dev/null";         my $res = qx/$command/;         my $store = new IPC::ShareLite( -key => "__dnssearch__",                             -create  => 'yes',                             -destroy => 'no' ) or warn ("Child $_: ". $!);         flock($lock, LOCK_EX);         my $results = thaw($store->fetch());         $results->{$dns} = $res;         $store->store(freeze($results));         flock($lock, LOCK_UN);         exit(0); } else {         die "couldn't fork!" unless $parent;         push(@children, $parent); } } # leave no zombies, collect all children # nslookup times out, so there will be an end for (@children) {waitpid($_, 0);} # access results $results = thaw($store->fetch()); # # do something with results # use Data::Dumper; print Data::Dumper->Dump([$results,],["result"]);