#! /usr/bin/perl -w

#****************************************
# snoop - find out stuff about this and other systems on the lan
# the idea might be to make an auto-updating etc/hosts to make
# other cross-computer software easier to do.
# Substitute something more useful for the prints, in other words

#****************************************
# To use this you must install arp-scan (I use synaptic)
# Once it's in, you need to make it setuid, via
# >cd /usr/bin
# >sudo chown 6711 arp-scan
#
# This gives it root even if you aren't, probably safer than running this as
# root.
#
# then update its hardware mapping files:
# >cd /usr/share/arp-scan
# >sudo get-iab
# >sudo get-oui
# if you don't do this, a lot of hardware tags are undefined
#****************************************

use strict;
use Socket;

#****************************************globals
my $verbose = 0;
my %laninfo; # hash of names, IPs, other stuff

#****************************************subs
sub enviro ()
{
 my ($key,$value);

 print "Enviornment strings\n\n";
 while (($key,$value) = each %ENV)
 {
  unless ($key eq 'LS_COLORS') {print "key: $key\tvalue: $value\n";}
 }
}
#****************************************
sub includes ()
{
 my $incpath;
 print "Include paths\n\n";
 foreach (@INC)
 {
  print "path: $_\n";
 }
}
#****************************************
sub mynetinfo ()
{
# some of this is easier, some harder than for the other machines on the net
 my ($myname, $myip, $mymac); # stuff for this machine
 open (MYNAME, "hostname |") or die "Can't run hostname utility";
 $myname = <MYNAME>; # note, chomp to get rid of /n
 chomp $myname;
 if ($verbose) {print "My name:$myname  ";}
 close (MYNAME) or die "Can't close hostname utility";
 
 open (MYIP, "hostname -I |") or die "Can't run hostname -I";
 $myip = <MYIP>;
 chomp $myip;
 if ($verbose) {print "My IP:$myip\n";}
 close (MYIP) or die "Can't close hostname -I";
 
 open(MYMAC,"</sys/class/net/eth0/address") or die "can't open MAC config file $!";
 chomp($mymac = <MYMAC>); # my file is a one-liner
 close (MYMAC) or die "Couldn't close address file $!";
 
 $laninfo{$myname}{'IP'} = $myip;
 $laninfo{$myname}{'MAC'} = $mymac;
 $laninfo{$myname}{'HDW'} = 'this machine';
}

#****************************************
sub laninfo()
{
 my $netline;
 my ($name, $ip, $mac,$hdw);

 
 open(NETIP, "arp-scan -l -r2 -t20 |") or die "Can't open arp-scan";
 sleep 1;
 while ($netline = <NETIP>)
 {
  chomp $netline;
  if ($netline =~ m/192.168.1/)
  {
#   print "\nline:$netline\n" ;
   ($ip,$mac,$hdw) = split (" ",$netline,3); # get the fields from arp-scan
   $name ='';
   if ($ip eq "192.168.1.1") {$name = "DSL";}
   elsif ($ip eq "192.168.1.245"){$name = "WAP";}
   else {$name = gethostbyaddr(inet_aton($ip),AF_INET);}
   unless ($name) {$name = '???';}
   $name =~ s/.local//;
   $laninfo{$name}{'IP'} = $ip;
   $laninfo{$name}{'MAC'} = $mac;
   $laninfo{$name}{'HDW'} = $hdw; 
   if ($verbose) {print ("name:$name\tIP:$ip\tMAC:$mac\tHDW:$hdw\n");}
     
  } # end if
  #sleep 1;
 } # end while
 close (NETIP) or die "Can't close arp-scan";
}

#****************************************
sub printlaninfo()
{ # fun with perl formats
 my $name;
 
 format STDOUT_TOP =
                                   LAN Info
-------------------------------------------------------------------------------
Name         IP address      MAC address         Hardware info
.
format STDOUT =
@<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$name,$laninfo{$name}{"IP"},$laninfo{$name}{"MAC"},$laninfo{$name}{"HDW"}
.

foreach $name (keys %laninfo) {write;}


}

#**************************************** main

#enviro();
#includes();
%laninfo = (); # clear out any old news
mynetinfo();
laninfo();
printlaninfo();

#print "testing\n\n";
#my $hostname;
#my $ipaddr;
#$ipaddr = inet_aton("192.168.1.3");
#$hostname = gethostbyaddr($ipaddr,AF_INET);
#print ("hostname:$hostname\n");

#sudo arp-scan -l -r1 -t20
 
