Removed /usr/local from CDPATH
[clearscm.git] / clearadm / discovery.pl
index ee4afa8..d765363 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 
 =pod
 
@@ -41,7 +41,7 @@ $Date: 2011/01/07 20:48:22 $
    
    -broadcastA|ddr <ip>:      Broadcast IP (Default: Current subnet)
    -broadcastT|ime <seconds>: Number of sends to wait for responses to broadcast
-                              (Default: 30 seconds)
+                              (Default: 10 seconds)
 
 =head1 DESCRIPTION
 
@@ -62,6 +62,7 @@ use lib "$FindBin::Bin/lib", "$FindBin::Bin/../lib";
 
 use Clearadm;
 use Display;
+use OSDep;
 use Utils;
 
 my $VERSION  = '$Revision: 1.1 $';
@@ -71,7 +72,7 @@ my $clearadm = Clearadm->new;
 
 my $broadcastTime = 10;
 
-sub discover ($) {
+sub discover($) {
   my ($broadcast) = @_;
   
   my $startTime = time;
@@ -81,18 +82,21 @@ sub discover ($) {
   verbose "Performing discovery (for $broadcastTime seconds)...";
 
   while (<$broadcast>) {
-    if (/from (.*):/) {
-      my $ip       = $1;
-      my $hostname = gethostbyaddr (inet_aton ($ip), AF_INET);
-     
-       unless ($hosts{$ip}) {
-         verbose "Received response from ($ip): $hostname";
-         $hosts{$ip} = $hostname;
-       } # unless
+    display "Received line: $_";
+    if (/from (\S+) \((.+)\)/) {
+      my $hostname = $1;
+      my $ip       = $2;
+
+      # Remove domain
+      $hostname =~ s/(\w+)\..*/$1/;
+
+      unless ($hosts{$ip}) {
+        verbose "Received response from ($ip): $hostname";
+        $hosts{$ip} = $hostname;
+      } # unless
     } # if
   
-    last
-      if (time () - $startTime) > $broadcastTime;
+    last if (time() - $startTime) > $broadcastTime;
   } # while
 
   verbose "$broadcastTime seconds has elapsed - discovery complete";
@@ -101,9 +105,9 @@ sub discover ($) {
 } # discover
 
 # Main
-my $broadcastAddress = inet_ntoa (INADDR_BROADCAST);
+my $broadcastAddress = inet_ntoa(INADDR_BROADCAST);
 
-GetOptions (
+GetOptions(
   usage             => sub { Usage },
   verbose           => sub { set_verbose },
   debug             => sub { set_debug },
@@ -111,14 +115,21 @@ GetOptions (
   'broadcastAddr=s' => \$broadcastAddress,  
 ) or Usage "Invalid parameter";
 
-Usage 'Extraneous options: ' . join ' ', @ARGV
-  if @ARGV;
+Usage 'Extraneous options: ' . join ' ', @ARGV if @ARGV;
 
 # Announce ourselves
 verbose "$FindBin::Script V$VERSION";
 
-my $broadcastCmd = "ping -b $broadcastAddress 2>&1";
+my $broadcastCmd = 'ping ';
 
+if ($ARCHITECTURE eq 'solaris') {
+  $broadcastCmd .= '-s ';
+} else {
+  $broadcastCmd .= '-b ';
+} # if
+
+$broadcastCmd .= "$broadcastAddress 2>&1";
+  
 my $pid = open my $broadcast, '-|', $broadcastCmd
   or error "Unable to do $broadcastCmd", 1;
 
@@ -134,17 +145,15 @@ verbose_nolf "Found $nbrHosts host";
 verbose_nolf 's' if $nbrHosts != 1;
 verbose      " on subnet $broadcastAddress";
 
-foreach (sort values %hosts) {
-  my $verbose = get_verbose () ? '-verbose' : '';
+for (sort values %hosts) {
+  my $verbose = get_verbose() ? '-verbose' : '';
   
   my ($status, @output) = Execute "updatesystem.pl -host $_ $verbose";
 
-  error "Unable to update host $_ (Status: $status)\n"
-      . join ("\n", @output), 1
-    if $status;
+  error "Unable to update host $_ (Status: $status)\n" . join ("\n", @output), 1 if $status;
     
   verbose join "\n", @output;
-} # foreach
+} # for
 
 =pod
 
@@ -179,9 +188,9 @@ L<Socket>
 =begin html
 
 <blockquote>
-<a href="http://clearscm.com/php/cvs_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
-<a href="http://clearscm.com/php/cvs_man.php?file=lib/Display.pm">Display</a><br>
-<a href="http://clearscm.com/php/cvs_man.php?file=lib/Utils.pm">Utils</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=clearadm/lib/Clearadm.pm">Clearadm</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
+<a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
 </blockquote>
 
 =end html
@@ -196,4 +205,4 @@ Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
 
 Copyright (c) 2010, ClearSCM, Inc. All rights reserved.
 
-=cut
\ No newline at end of file
+=cut