Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Discover Ping-only Devices

This script performs a ping scan of the specified ranges and creates the appropriate configuration for ping-only devices.

Edit the @ranges array definition at the top of the function to define your own IP ranges.

use Socket;

sub custom_scan_ping_devices
{
   my @ranges = (
      "10.1.1.0/24",
   );
   my @domains = config_load_domains ();
   my $IN;
   my $OUT;
   my $PING_SCAN_OUT = "$HOME_TMP/ping-scan.out";
   my %result;

   open ($OUT, "|-", "$PING_SCAN -o $PING_SCAN_OUT") or EXIT_FATAL ("Can't run $PING_SCAN: $!");
   for my $r (@ranges) {
      printf $OUT "%s\n", $r;
   }
   close $OUT;  # Block on the close until nm-ping-scan completes

   open ($IN, "<", $PING_SCAN_OUT) or EXIT_FATAL ("Can't open $PING_SCAN_OUT: $!");
   while (my $ipaddr = <$IN>) {
      chomp $ipaddr;

      my $name = gethostbyaddr (inet_aton ($ipaddr), AF_INET);

      if (defined $name) {
         $name = config_strip_sysname ($name, @domains);
      }
      else {
         $name = $ipaddr;
      }

      if ($ipaddr =~ /$REGEX_IPV4_ADDR/m) {
         $result{$name}{ip4addr} = $ipaddr;
         $result{$name}{device}  = $name;
      }
      elsif ($ipaddr =~ /$REGEX_IPV6_ADDR/m) {
         $result{$name}{ip6addr} = $ipaddr;
         $result{$name}{device}  = $name;
      }
   }
   close $IN;

   for my $name (keys %result) {
      config_add_ping_device ($result{$name});
   }
}