home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / network / domain-check-3.5 < prev    next >
Text File  |  2007-11-05  |  19KB  |  509 lines

  1. #!/usr/bin/perl -ws
  2. # Created by Ben Okopnik on Thu Jun 28 09:11:52 EDT 2007
  3. #
  4. # Copyright (C) 2007 Ben Okopnik <ben@okopnik.com>
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14.  
  15. =pod
  16. ################################## Changelog ##############################
  17. 11/05/07 21:01 v3.5
  18. * Tweaked to resolve .cz ("expire:" along with "expires:")
  19.  
  20. 09/25/07 12:27 v3.4
  21. * Tweaked regexes to include the ".name" date syntax.
  22.  
  23. 08/15/07  1:06 v3.3
  24. * Added a bit more CLI error checking (trips off on '-d foo.com', etc.)
  25.  
  26. 08/14/07 23:03 v3.2
  27. * Polished the regexen based on Rick Moen's list of 270 TLDs
  28. * Wrapped the date-calc section in an eval for cases where the date is past
  29.     the Unix "death boundary" (18-Jan-2038)
  30.  
  31. 08/08/07 23:26 v3.1
  32. * Added another regex to parse the weird structure of 'extragalactic.net';
  33.     modified another regex slightly to accomodate 'expire date' for 'nic.it'.
  34.  
  35. 07/29/07  1:26 v3.0
  36. * MAJOR REVISION:
  37.     o The format of the 'domain-list' file has been changed, although the
  38.       old format is still valid. You can now add the name of the host for
  39.       'whois' to use as the second argument on the line; however, using the
  40.       '-s' command line argument will force all lookups to be done via the
  41.       specified host.
  42.     o Fixed up a number of regexen for the 'jwhois' differences
  43.  
  44. 07/28/07  0:41 - v2.2
  45. * Added 'jwhois' as the preferred option, with a warning if it's not
  46.     installed. Caching for 'jwhois' is disabled when '-X' is in effect;
  47.     '-H' is no longer a hard-wired argument to 'whois' ('jwhois' doesn't
  48.     support it), but is still appended if 'whois' is used.
  49. * Tweaked a couple of the regexen to process new TLDs (.fi, .ly, etc.)
  50. * Giving serious thought to modifying the format of the -F files; it would
  51.     be nice to be able to specify the whois server for individual domains.
  52.  
  53. 07/20/07  9:36 - v2.1
  54. * Added a bunch of tracing/debugging statements to the date parser, making
  55.     the '-X' option much more useful
  56. * Built a 'switch-case' structure around the parser so that only one regex
  57.     would apply to any given host
  58. * Added a '-H' argument to 'whois' ("elide legal disclaimer") to make
  59.     debugging output less annoying (and maybe speed things up fractionally)
  60. * Made the 'no expiration date found' error into a non-fatal warning (used
  61.     to break list processing)
  62. * Modified the output format slightly (warnings now appear on the same line
  63.     as the domain name)
  64. * Domains without a registrar will no longer be omitted from the mailed
  65.     notifications
  66.  
  67. 07/19/07 22:28 - v2.0
  68. * Now parsing .ci domains as well (millions of people cheer, world peace
  69.     can't be far away now...)
  70.  
  71. 07/19/07 20:54 - v1.9
  72. * Added a little regex-fu to accept lines that have whitespace at the end
  73. * Added a Big Sekrit Option ('-X' - shhh, don't tell anybody!) for debugging
  74.  
  75. 07/19/07 11:56 - v1.8
  76. * Lots and lots of fixes for many different TLDs; much mangling of regexen.
  77.     Now handles many more expiration date types than before. Most
  78.     importantly, domains that don't list a registrar will now be displayed
  79.     anyway; people probably know where to send their money, but not
  80.     necessarily _when._
  81.  
  82. 07/04/07 12:28 - v1.7
  83. * Scrapped previous approach to the .org delay; the .orgs are now sorted to
  84.     the end of the domain list and all except the first one wait 20 seconds.
  85. * Added a cute little time ticker to the delay routine, just because. :)
  86.  
  87. 07/03/07  1:27 - v1.6
  88. * Added a rate limiter (3/minute) for .org domains
  89.  
  90. 06/30/07 18:34 - v1.5
  91. * Added a "domain not parseable; please report" warning
  92. * Added an "Unable to read 'whois' info" warning for the 'fgets: connection
  93.     reset by peer' error.
  94. * All expiration warnings are now sent as one email instead of one per
  95.     domain; ditto the expired domains notifications.
  96. * The 'printf' for the 'SKIPPED' error was ignoring the '-q' option; fixed
  97.  
  98. 06/30/07  8:19 - v1.4
  99. * Removed dependency on File::Find; searching PATH 'manually'
  100. * Added an 'exit 1' to the silent failure mode of 'croak'
  101.  
  102. 06/30/07  7:06 - v1.3
  103. * Improved the date-parsing regexes (the numerical months part can now only
  104.     match '01-12' instead of 'any two digits'); this should increase the
  105.     reliability of resolving 'dd-mm-yyyy' vs. 'mm-dd-yyyy' somewhat.
  106. * More accurate reporting for the 'SKIPPED' error (now shows exact reason)
  107. * Fixed the regexes that I screwed up while adding the Dotster extension
  108. * Added a '-v' option
  109.  
  110. 06/29/07 18:54 - v1.2
  111. * Got rid of an unnecessary system dependency ('which') - 'File::Find' is a
  112.     bit clunky, but better than depending on unknowns...
  113. * Another date-processing regex (ISOC-IL: 'validity: 29-06-2007')
  114.  
  115. 06/29/07 17:07 - v1.1
  116. * Modified output format to include both exp. date and days remaining
  117. * Added another date-processing regex (DOTSTER: 'Expires on: 29-Jun-07')
  118.  
  119. 06/29/07 15:06 - v1.0
  120. I'm finally willing to admit that this script is usable. :) Recent changes
  121. include:
  122.  
  123. * Parsing routine for "2007/08/12" date format
  124. * 'croak' notifies admin of problems encountered in silent mode
  125. * Added a fallback email address for 'croak'
  126. * Fixed GMT parsing routine miscalc (thanks to Rick Moen for the heads up)
  127.  
  128. For Nosy Nellies only: *Yes*, I'm aware of the various '*Whois.pm' modules
  129. on CPAN. None of them do what I want; the one that comes closest
  130. (Net::XWhois) hasn't been maintained since 2001 and only covers a smallish
  131. subset of what I want. No, I'm not interested in taking it over and
  132. maintaining it; I've got enough to do as it is.
  133.  
  134. ###########################################################################
  135. =cut
  136.  
  137. use strict;
  138. use Time::Local;
  139. $|++;
  140.  
  141. # Command-line variables
  142. our ($d, $e, $F, $h, $q, $s, $v, $x, $X);
  143.  
  144. ### FALLBACK ADDRESS FOR NOTIFICATION ############
  145. my $address = 'root@localhost';
  146. ##################################################
  147.  
  148. my ($name) = $0 =~ /([^\/]+)$/;
  149.  
  150. my $usage =<<"+EoT+";
  151. Usage: $name [-e=email] [-x=expir_days] [-q] [-h] <-d=domain_name|-F=domainfile>
  152.  
  153.   -d=domain        : Domain to analyze
  154.   -e=email_address : Send a warning message by email
  155.   -F=domain_list   : File with a list of domains, one per line
  156.   -h               : Print this message
  157.   -q               : Don't print to the console (REQUIRES '-e' OPTION)
  158.   -s=whois server  : Use alternate whois server
  159.   -v               : Display current version of this script
  160.   -x=days          : Change default (30d) expiration interval (REQUIRES '-e' OPTION)
  161.  
  162. +EoT+
  163.  
  164. # Locate 'whois' or (preferred) 'jwhois'
  165. my ($whois) = grep -e, map "$_/jwhois", split /:/, $ENV{PATH};
  166. ($whois) = grep -e, map "$_/whois", split /:/, $ENV{PATH} unless $whois;
  167. die "'whois'|'jwhois' not found in path.\n" unless $whois;
  168. if ($whois =~ m#/whois$#){
  169.     # $q || print "You really should install 'jwhois'; it gives better results.\n";
  170.     # Turn down the noise (minimal output option - only works with 'whois')
  171.     $whois .= " -H";
  172. }
  173. else {
  174.     # Turn off caching for 'jwhois' if the debug option is on
  175.     $whois .= " -f" if $X;
  176. }
  177.  
  178. # $whois = "/usr/bin/whois";
  179.  
  180. # Find a mail client (mutt or mailx)
  181. my ($mail) = grep -e, map "$_/mutt", split /:/, $ENV{PATH};
  182. # Switch Mutt into 'mailx' mode if found
  183. if ($mail){
  184.     $mail .= " -x";
  185. }
  186. else {
  187.     ($mail) = grep -e, map "$_/mailx", split /:/, $ENV{PATH};
  188. }
  189. die "No 'mailx' or 'mutt' (mail client) found in path.\n" unless $mail;
  190.  
  191. # Read the version number at the top of the changelog
  192. if ($v){
  193.     seek DATA, 0, 0;
  194.     while (<DATA>){
  195.         if (m[^\d+/\d+/\d+[^v]+v([0-9.]+)]){
  196.             print "Version: $1\nCopyright (C) 2007 Ben Okopnik <ben\@okopnik.com>\n\n";
  197.             exit 0;
  198.         }
  199.     }
  200. }
  201.  
  202. # Email admin if '-q' is on; otherwise, just exit with the error
  203. sub croak {
  204.     if ($q){
  205.         # If '-e' wasn't specified, use the fallback address
  206.         $e ||= $address;
  207.  
  208.         # No place to send an error if this fails... :)
  209.         open Mail, "|$mail -s 'WARNING: $name script error' $e";
  210.         print Mail "$name [" . localtime() . "]: ", $_[0];
  211.         close Mail;
  212.  
  213.         exit 1;
  214.     }
  215.     else {
  216.         die $_[0];
  217.     }
  218. }
  219.  
  220. # Display the help output if requested or in case of incorrect usage
  221. die "$usage\n" if $h;
  222. die "\n*ERROR: '$name' requires an email address with the '-q' and the '-x' options*\n\n$usage" if ($q || $x) && ! $e;
  223. die "\n*ERROR: '$name' requires either a domain name or a domain list as an argument*\n\n$usage" if ! $d && ! $F;
  224. die "\n*ERROR: Please make sure you're using correct syntax (i.e., '-d=domain_name')*\n\n$usage" if (defined $d && $d =~ /^1$/) || (defined $F && $F =~ /^1$/) || (defined $s && $s =~ /^1$/);
  225.  
  226. # Set default notification interval to 30 days
  227. if ($x){
  228.     croak "Expiration interval must be specified in days (0-9999).\n"
  229.         unless $x =~ /^\d{1,4}$/;
  230. }
  231. else {
  232.     $x = 30;
  233. }
  234.  
  235. # Read the domain list file
  236. my @domains;
  237. if ($F){
  238.     croak "$F is not a regular file\n" unless -f $F;
  239.     croak "Can't read $F\n" unless -r _;
  240.     # Open the file if it exists
  241.     open F or croak "$F: $!\n";
  242.     while (<F>){
  243.         # Skip blank lines; ignore comments
  244.         next if /^\s*(?:#|$)/;
  245.         # Strip preceding and following blanks
  246.         s/^\s*(.*?)\s*$/$1/;
  247.  
  248.         # Separate domain and server if they exist
  249.         my (@line) = split;
  250.         for (@line){
  251.             # Strip URI method and any terminal '/'s
  252.             s#^.*://##;
  253.             s#/$##;
  254.         }
  255.         push @domains, [ @line ];
  256.     }
  257.     close F;
  258. }
  259.  
  260. # Having a '-F' AND a '-d' is explicitly not excluded
  261. if ($d){
  262.     # Strip URI method and any terminal '/'s
  263.     $d =~ s#^.*://##;
  264.     $d =~ s#/$##;
  265.     push @domains, [ $d ];
  266. }
  267.  
  268. # Set the server if it's been specified (this REPLACES any servers defined
  269. # in the domain-list file)
  270. if ($s){
  271.     $_ -> [1] = $s for @domains;
  272. }
  273.  
  274. # Sort list to push .orgs to the end; ASCIIbetical sort otherwise
  275. @domains = sort { ($a->[0] =~ /\.org$/i) <=> ($b->[0] =~ /\.org$/i) || $a->[0] cmp $b->[0] } @domains;
  276.  
  277. # Trim strings to specified length; return '**UNKNOWN**' if undef
  278. sub trim {
  279.     defined $_[0] || return "**UNKNOWN**";
  280.     substr($_[0], 0, $_[1]);
  281. }
  282.  
  283. # Lookup list for month number->name conversion
  284. my (%mth,%mlookup);
  285. @mth{map sprintf("%02d", $_), 1..12} = qw/jan feb mar apr may jun jul aug sep oct nov dec/;
  286. # Lookup list for month name->abbrev conversion
  287. @mlookup{qw/january february march april may june july august september october november december/} =
  288.     (qw/jan feb mar apr may jun jul aug sep oct nov dec/) x 2;
  289.  
  290. ########################## DATA COLLECTION SECTION #############################
  291.  
  292. # Process the domain list
  293. my ($seen, %list);
  294. for my $line (@domains){
  295.     my ($host, $server) = @{$line};
  296.  
  297.     my $opt = $server ? "-h $server" : "";
  298.  
  299.     $q || print "\b\nProcessing $host... ";
  300.  
  301.     # Delay to avoid triggering rate limiter
  302.     if ($host =~ /\.org$/i){
  303.         $q || print "(NOTE: Subsequent ORG queries will be delayed by 20 seconds each due to rate limiting) "
  304.             unless $seen;
  305.         # Show the cute little time ticker :)
  306.         if ($seen++){
  307.             my @chars = split //, '|/-\\';
  308.             for (0 .. 19){
  309.                 $q || print $chars[$_ % 4], "\b";
  310.                 sleep 1;
  311.             }
  312.             print " \b";
  313.         }
  314.     }
  315.  
  316.     # Execute the query
  317.     my $out;
  318.     open Who, "$whois $opt $host|" or croak "Error executing $whois: $!\n";
  319.     {
  320.         # Read in the entire output of 'whois' as a single string
  321.         local $/;
  322.         $out = <Who>;
  323.     }
  324.     close Who;
  325.  
  326.     # Make sure it's not DOS formatted
  327.     $out =~ tr/\cM//d;
  328.  
  329.     # 'fgets: connection reset by peer' - bloody annoying response!
  330.     if (!$out || $out !~ /domain/i){
  331.         $q || print "Unable to read 'whois' info for $host. Skipping... ";
  332.         next;
  333.     }
  334.  
  335.     # Freak out and run away if there's no match
  336.     if ($out =~ /no match/i){
  337.         $q || print "No match for $host!\n";
  338.         next;
  339.     }
  340.     # Ditto for bad hostnames
  341.     if ($out =~ /No whois server is known for this kind of object/i){
  342.         $q || print "'whois' doesn't recognize this kind of object. ";
  343.         next;
  344.     }
  345.  
  346.     # Convert multi-line 'labeled block' output to 'Label: value'
  347.     my $debug;
  348.     if ($out =~ /registrar:\n/i){
  349.         $out =~ s/:\n(?!\n)/: /gsm;
  350.         $debug .= "matched on line " . (__LINE__ - 1) . ": Multi-line 'labeled block'\n";
  351.     }
  352.  
  353.     # Date preprocessing. Desired date format is '29-jun-2007'
  354.     # 'Fri Jun 29 15:16:00 EDT 2007'
  355.     if ($out =~ s/(date:\s*| on:\s*)[A-Z][a-z]+\s+([a-zA-Z]{3})\s+(\d+).*?(\d+)\s*$/$1$3-$2-$4/igsm){
  356.         $debug .= "matched on line " . (__LINE__ - 1) . ": 'Fri Jun 29 15:16:00 EDT 2007'\n";
  357.     }
  358.     # '29-Jun-07'
  359.     elsif ($out =~ s/(date:\s*| on:\s*)(\d{2})[\/ -](...)[\/ -](\d{2})\s*$/$1$2-$3-20$4/igsm){
  360.         $debug .= "matched on line " . (__LINE__ - 1) . ": '29-Jun-07'\n";
  361.     }
  362.     # '2007-Jun-29'
  363.     elsif ($out =~ s/[^\n]*(?:date| on|expires on\.+):\s*(\d{4})[\/-](...)[\/-](\d{2})\.?\s*$/Expiration date: $3-$2-$1/igsm){
  364.         $debug .= "matched on line " . (__LINE__ - 1) . ": '2007-Jun-29'\n";
  365.     }
  366.     # '2007/06/29'
  367.     elsif ($out =~ s/(?:renewal-|expir(?:e|es|y|ation)\s*)(?:date|on)?[ \t.:]*\s*(\d{4})(?:[\/-]|\. )(0[1-9]|1[0-2])(?:[\/-]|\. )(\d{2})(?:\.?\s*[0-9:.]*\s*\w*\s*|\s+\([-A-Z]+\)?)$/Expiration date: $3-$mth{$2}-$1/igsm){
  368.         $debug .= "matched on line " . (__LINE__ - 1) . ": '2007/06/29'\n";
  369.     }
  370.     # '29-06-2007'
  371.     elsif ($out =~ s/(?:validity:|expir(?:y|ation) date:|expire:|expires? (?:on:?|on \([dmy\/]+\):|at:))\s*(\d{2})[\/.-](0[1-9]|1[0-2])[\/.-](\d{4})\s*[0-9:.]*\s*\w*\s*$/Expiration date: $1-$mth{$2}-$3/igsm){
  372.         $debug .= "matched on line " . (__LINE__ - 1) . ": '29-06-2007'\n";
  373.     }
  374.     # '[Expires on]     2007-06-29' (.jp, .ru)
  375.     elsif ($out =~ s/(?:valid-date|expiration date:|paid-till:|\[expires on\]|expires on ?:|expired:)\s*(\d{4})[\/.-](0[1-9]|1[0-2])[\/.-](\d{2})(?:\s*[0-9:.]*\s*\w*\s*|T[0-9:]+Z)$/Expiration date: $3-$mth{$2}-$1/igsm){
  376.         $debug .= "matched on line " . (__LINE__ - 1) . ": '[Expires on]     2007-06-29' (.jp, .ru)\n";
  377.     }
  378.     # 'expires:     June  29 2007' (.is)
  379.     elsif ($out =~ s/expires:\s*([A-Z][a-z]+)\s+(\d{1,2})\s+(\d{4})\s*$/"Expiration date: " . sprintf("%02d", $2) . "-" . $mlookup{"\L$1\E"} . "-$3"/igsme){
  380.         $debug .= "matched on line " . (__LINE__ - 1) . ": 'expires:     June  29 2007' (.is)\n";
  381.     }
  382.     # 'renewal: 29-June-2007'
  383.     elsif ($out =~ s/renewal:\s*(\d{1,2})[\/ -]([A-Z][a-z]+)[\/ -](\d{4})\s*$/"Expiration date: $1-" . $mlookup{"\L$2\E"} . "-$3"/igsme){
  384.         $debug .= "matched on line " . (__LINE__ - 1) . ": 'renewal: 29-June-2007' (.ie)\n";
  385.     }
  386.     # 'expire:         20080315' (.cz, .ke)
  387.     elsif ($out =~ s/expir[ey]:\s*(\d{4})(\d{2})(\d{2})\s*$/Expiration date: $3-$mth{$2}-$1/igsm){
  388.         $debug .= "matched on line " . (__LINE__ - 1) . ": 'expire:         20080315' (.cz, .ke)\n";
  389.     }
  390.     # 'domain_datebilleduntil: 2007-06-29T00:00:00+12:00' (.nz)
  391.     elsif ($out =~ s/domain_datebilleduntil:\s*(\d{4})[-\/](\d{2})[-\/](\d{2})T[0-9:.+-]+\s*$/Expiration date: $3-$mth{$2}-$1/igsm){
  392.         $debug .= "matched on line " . (__LINE__ - 1) . ": 'domain_datebilleduntil: 2007-06-29T00:00:00+12:00' (.nz)\n";
  393.     }
  394.     # '29 Jun 2007 11:58:42 UTC' (.coop)
  395.     elsif ($out =~ s/(?:expir(?:ation|y) date|expire[sd](?: on)?)[:\] ]\s*(\d{2})[\/ -](...)[\/ -](\d{4})\s*[0-9:.]*\s*\w*\s*$/Expiration date: $1-\L$2\E-$3/igsm){
  396.         $debug .= "matched on line " . (__LINE__ - 1) . ": '29 Jun 2007 11:58:42 UTC' (.coop)\n";
  397.     }
  398.     # 'Record expires on 17/8/2100' (.hm, fi)
  399.     elsif ($out =~ s/(?:expires(?: on|:))\s*(\d{2})[\/.-]([1-9]|0[1-9]|1[0-2])[\/.-](\d{4})\s*[0-9:.]*\s*\w*\s*$/"Expiration date: $1-".$mth{sprintf "%02d", $2} . "-$3"/iegsm){
  400.         $debug .= "matched on line " . (__LINE__ - 1) . ": 'Record expires on 17/8/2100' (.hm)\n";
  401.     }
  402.     # 'Expires on..............: Sat, Mar 29, 2008'
  403.     elsif ($out =~ s/expires on\.*:\s*(?:[SMTWF][uoehra][neduit]),\s+([A-Z][a-z]+)\s+(\d{1,2}),\s+(\d{4})\s*$/"Expiration date: " . sprintf("%02d", $2) . "-\L$1-$3"/iegsm){
  404.         $debug .= "matched on line " . (__LINE__ - 1) . ": 'Expires on..............: Sat, Mar 29, 2008'\n";
  405.     }
  406.     else {
  407.         $debug = "No regexes matched.\n";
  408.     }
  409.  
  410.     # Collect the data from each query
  411.     for (split /\n/, $out){
  412.         # Clip pre- and post- blanks
  413.         s/^\s*(.*?)\s*$/$1/;
  414.         # Squash repeated tabs and spaces
  415.         tr/ \t//s;
  416.  
  417.         # This is where it all happens - regexes to capture registrar and expiration
  418.         $list{$host}{Registrar} ||= $1 if /(?:maintained by|registration [^:]*by|authorized agency|registrar)(?:\s*|_)(?:name|id|of record)?:\s*(.*)$/i;
  419.         $list{$host}{Expires} ||= $1 if /(?:expires(?: on)?|expir(?:e|y|ation) date\s*|renewal(?:[- ]date)?)[:\] ]\s*(\d{2}-[a-z]{3}-\d{4})/i;
  420.         # print "Registrar: $list{$host}{Registrar}\nExpires: $list{$host}{Expires}\n";
  421.     }
  422.  
  423.     # Assign default message if no registrar was found
  424.     $list{$host}{Registrar} ||= "[[[ No registrar found ]]]";
  425.     
  426.     $q || print "No expiration date found in 'whois' output. Please report this domain to the author!"
  427.         unless defined $list{$host}{Expires};
  428.  
  429.     # Debug option (activated by '-X'); exits here with parsed 'whois' output
  430.     $debug .= "Registrar: $list{$host}{Registrar}\n" if defined $list{$host}{Registrar};
  431.     $debug .= "Expires: $list{$host}{Expires}\n" if defined $list{$host}{Expires};
  432.     die "\n", "=" x 70, "\n$out", "=" x 70, "\n$debug", "=" x 70, "\n" if $X;
  433. }
  434.  
  435. $q || print "\n";
  436.  
  437. ########################## DATA ANALYSIS SECTION #############################
  438.  
  439. # Get current time snapshot in UTC
  440. my $now = timegm(gmtime);
  441.  
  442. # Convert dates to UTC epoch seconds; *will* fail on 19 Jan 2038. :)
  443. my %months;
  444. @months{qw/jan feb mar apr may jun jul aug sep oct nov dec/} = 0..11;
  445.  
  446. # Print the header if '$q' is off and there's content in %list
  447. $q || %list && printf "\n\n%-24s%-36s%s\n%s\n", "Host", "Registrar", "Exp.date/Days left", "=" x 78;
  448.  
  449. # Process the collected data
  450. my (%exp, %end);
  451. for my $k (sort keys %list){
  452.     unless (defined $list{$k}{Expires}){
  453.         $q || printf "%-32s%s\n", trim($k, 31), "*** SKIPPED (missing exp. date) ***";
  454.         delete $list{$k};
  455.         next;
  456.     }
  457.     my @chunks = split /-/, $list{$k}{Expires};
  458.     my $epoch;
  459.     eval { $epoch = timegm(0, 0, 0, $chunks[0], $months{lc $chunks[1]}, $chunks[2] - 1900) };
  460.     if ($@ =~ /too big/){
  461.         $q || print "**** NOTE: Date past 19-Jan-2038 - date will NOT be calculated correctly! ****\n";
  462.         # Set date to EPOCH_MAX
  463.         $epoch = 2147212800;
  464.     }
  465.     elsif ($@){
  466.         $q || print "$@\n";
  467.         # Set date to EPOCH_MAX
  468.         $epoch = 2147212800;
  469.     }
  470.     my $diff = int(($epoch - $now) / 86400);
  471.     $q || printf "%-24s%-36s%-12s/%5s\n", trim($k, 23), trim($list{$k}{Registrar}, 35),
  472.         $list{$k}{Expires}, $diff;
  473.  
  474.     # Prepare alerts if domain is expired or the expiration date is <= $x days
  475.     if ($e && ($diff <= $x)){
  476.         if ($diff <= 0){
  477.             $exp{$k} = -$diff;
  478.         }
  479.         else {
  480.             $end{$k} = $diff;
  481.         }
  482.     }
  483. }
  484.  
  485. # Report expired domains
  486. if (%exp){
  487.     open Mail, "|$mail -s '$name: Expired domains' $e" or croak "$mail: $!\n";
  488.     print Mail "According to 'whois', the following domains have expired:\n\n";
  489.     for my $x (sort { $exp{$a} <=> $exp{$b} } keys %exp){
  490.         my $s = $exp{$x} == 1 ? "" : "s";
  491.         print Mail "$x ($exp{$x} day$s ago)\n";
  492.     }
  493.     close Mail;
  494. }
  495.  
  496. # Report domains that will expire within the '-x' period
  497. if (%end){
  498.     open Mail, "|$mail -s '$name: Domain expiration warning ($x day cutoff)' $e" or croak "$mail: $!\n";
  499.     print Mail "According to 'whois', these domains will expire soon:\n\n";
  500.     for my $d (sort { $end{$a} <=> $end{$b} } keys %end){
  501.         my $s = $end{$d} == 1 ? "" : "s";
  502.         print Mail "$d (in $end{$d} day$s)\n";
  503.     }
  504.     close Mail;
  505. }
  506.  
  507. __END__
  508.  
  509.