Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Net::Whois::Raw silently fails

by neilwatson (Curate)
on Oct 31, 2006 at 14:29 UTC ( #581498=perlquestion: print w/ replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

I wrote this script to report domain expiries. Net::Whois::Raw seems to fail silently (returns 'Whois failed') more often than it succeeds. A standard UNIX whois always works. What could the problem be?
#!/usr/bin/perl use strict; use warnings; # Fix Redhat issues with UTF8 $ENV{LC_ALL} = 'en_US'; use Net::Whois::Raw; use Date::Manip qw(ParseDate UnixDate DateCalc Delta_Format); # Report domain only if days left is less than # a given argument. Default is 60. my $left; if (defined $ARGV[0]){ $left = $ARGV[0]; }else{ $left = 60; } # Add or subtract domains to check from here. my @domains = qw( example.ca example.net example.org ); my ($d, $who, $domain, $expire, %expires, $days, $edate); format STDOUT_TOP= Domains that expire in less than @<<< days $left Domain Expires Days Left ------------------------------------------------------ . format STDOUT= @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<< @### $domain, $expire, $days . # Calculate days to expiration. foreach $d (@domains){ # Lookup domain with whois $who = whois($d) or die "Cannot reach whois server"; # Get expiration date if ($who =~ m/expir.*?(\d{2}-\w{3}-\d{4})/i) { $edate = $1; }elsif ($who =~ m|renewal-date.*?(\d{4}/\d{2}/\d{2})|i) { $edate = $1; }else{ $expires{$d} = "Whois failed"; next; } # Parse expiration date to yyyy/mm/dd. $expires{$d} = dateparse($edate); } # Generate report foreach $d (sort keys %expires){ $domain = $d; $expire = $expires{$d}; # Do not calculate expired dates if whois failed if ($expire eq 'Whois failed') { $days = 0; }else{ $days = daystoexpire($expires{$d}); } # Only report if expiration date is earlier than stated. if ($days <= $left){ write STDOUT; } } # Parse date to yyyy/mm/dd sub dateparse { my $datestr = shift; my ($y, $m, $d) = UnixDate($datestr, "%Y", "%m", "%d"); my $date = "$y/$m/$d"; return $date; } # Calculate days until domain expires sub daystoexpire { my $date = shift; my $days = DateCalc("today", $date); # Format delta date to days. $days = Delta_Format($days, 2, '%dh'); return $days; }

Neil Watson
watson-wilson.ca

Comment on Net::Whois::Raw silently fails
Download Code
Re: Net::Whois::Raw silently fails
by Corion (Pope) on Oct 31, 2006 at 14:35 UTC

    I blame your uninformative error message:

    # Lookup domain with whois $who = whois($d) or die "Cannot reach whois server"; # Get expiration date if ($who =~ m/expir.*?(\d{2}-\w{3}-\d{4})/i) { $edate = $1; }elsif ($who =~ m|renewal-date.*?(\d{4}/\d{2}/\d{2})|i) { $edate = $1; }else{ $expires{$d} = "Whois failed"; next; }

    Once you replace your "Whois failed" with something more informative, it should become much clearer where the error lies:

    $expires{$d} = "Whois failed for $d: Couldn't decipher '$who'" +;
      The error message did help. Thank you. I tried something similar but, the format for the printed report truncated the error message (my bad). Also, whois servers sometimes get upset by to many requests in a short period of time. I placed a sleep (10 seconds) in the domain loop and things are working now.

      Neil Watson
      watson-wilson.ca

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://581498]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2015-07-03 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (56 votes), past polls