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