#!/perl/bin
# ------------------------------------------------------------------------ 
# GETMAC v1.2 for NT4                  1998 Nils Reichen <reichen@eicn.ch>
# ------------------------------------------------------------------------ 
# This program does a scan of all the connecting computer.
# It return the MAC and IP address and if possible the DNS and NetBIOS 
# name.
#
# Made by Nils Reichen <reichen@eicn.ch>
# EICN, NEUCHATEL SCHOOL OF ENGINEERING
# Le Locle, Switzerland
#
# under Perl 5.004_02 for WinNT4.0
# Copyri... ah hell, just take it.
# Modify and use as you see fit, but please leave my name and the EICN 
# name on it as long as it still resembles the original code.
#
# For runing this under a Unix like: change [ping -n 1] by [ping -c 1]
# and change the ipconfig cmd to the ifconfig cmd with a little change
#
#$Header: /proj/arp/getmac.pl,v 1.2 1998/4/21
# ------------------------------------------------------------------------ 
# v1.0 Created: 03.Feb.98 - Created by Nils Reichen <reichen@eicn.ch>
# v1.1 Revised  05.Mar.98 - Released Code
# v1.2 Revised  21.Apr.98 - netdata.log: style changed -> netdata.dat
$ver      = "v1.2";
$ver_date = "21.Apr.98";
# ------------------------------------------------------------------------

print "Start IP address: ";
chomp($startIP=<STDIN>);
print "End IP address: ";
chomp($endIP=<STDIN>);
print "\n";
$ipaddress=$startIP;

# ipaddress : $ip4.$ip3.$ip2.$ip1
($ip1)=($ipaddress=~ /\d+\D\d+\D\d+\D(\d+)/);
($ip2)=($ipaddress=~ /\d+\D\d+\D(\d+)/);
($ip3)=($ipaddress=~ /\d+\D(\d+)/);
($ip4)=($ipaddress=~ /(\d+)/);

# endIP : $ipe4.$ipe3.$ipe2.$ipe1
($ipe1)=($endIP=~ /\d+\D\d+\D\d+\D(\d+)/);
($ipe2)=($endIP=~ /\d+\D\d+\D(\d+)/);
($ipe3)=($endIP=~ /\d+\D(\d+)/);
($ipe4)=($endIP=~ /(\d+)/);

# check for invalid IP address 
if (($ip1<=254)&&($ip2<=254)&&($ip3<=254)&&($ip4<=254)&&($ipe1<=254)&&($ipe2<=254)&&($ipe3<=254)&&($ipe4<=254)&&($ip1!=0)&&($ipe1!=0))
{
    # open database file
    open(FILE,">>netdata.dat") or die "Cannot open netdata.log: $!";  

    # Write in database: Hostname MAC and IP address, DNS name and Netbios name
    $local= `ipconfig /all`;
    ($localMAC)=($local=~ /(..-..-..-..-..-..)/);
    $localMAC=~ tr/A-Z/a-z/;  # min to MAJ
    ($localIP)=($local=~ /IP Address.+: (\d+\D\d+\D\d+\D\d+)/);
    ($localDNS)=($local=~ /Host Name.+: (\S+)/);
    $local= `tracert $localIP`;
    ($localNB)=($local=~ /ms\s\s(\S+)\s.\d+\D\d+\D\d+\D\d+./);
    print FILE "MAC:$localMAC IP:$localIP DNS:$localDNS WINS:$localNB\n";
    print "Local name:          ",$localDNS,"\n";
    print "NetBIOS name:        ",$localNB,"\n";
    print "Local IP address:    ",$localIP,"\n";
    print "Local MAC address:   ",$localMAC,"\n\n";

    # 'Search network info while(ipaddress < endIP)' loop
    while (($ip4<$ipe4)or(($ip4==$ipe4)and(($ip3<$ipe3)or(($ip3==$ipe3)and(($ip2<$ipe2)or(($ip2==$ipe2)and($ip1<=$ipe1)))))))
    {
	$ping = `ping -n 1 $ipaddress`;
	if ($ping =~ /bytes=/) {
	    $arp = `arp -a $ipaddress`;
	    # MAC address filter
	    $arp =~ /\s\s...............\s+(..-..-..-..-..-..)/;    
	    $macaddr = $1;
	    if($macaddr){ # if ipaddress is the same as the localhost => skip
		# DNS request for $ipaddress
		$dns = `nslookup -view $ipaddress`;
		if ($dns=~ /Name/){
		    ($dns)=($dns=~ /.+\n.+\n.*\nName.\s\s\s\s(\S+)/);
		}
		else{$dns="";}  # if no DNS name, $dns="";
		# NetBIOS name search
		$nb= `tracert $ipaddress`;
		($nb)=($nb=~ /ms\s\s(\S+)\s.\d+\D\d+\D\d+\D\d+./);
                # Write in database file: MAC IP DNS NetBIOS
		print FILE "MAC:$macaddr IP:$ipaddress DNS:$dns WINS:$nb\n";
		print "DNS name:     ",$dns,"\n";
		print "NetBIOS name: ",$nb,"\n";
		print "IP address:   ",$ipaddress,"\n";
		print "MAC address:  ",$macaddr,"\n\n";		
	    }
	}
	# Incr. IP address
	if ($ip1 < 254){
	    $ip1=$ip1+1;
	}
	else{
	    $ip1=1;
	    if ($ip2 < 254){
		$ip2=$ip2+1;
	    }
	    else{
		$ip2=0;
		if ($ip3 < 254){
		    $ip3=$ip3+1;
		}
		else{
		    $ip3=0;
		    if ($ip4 < 254){
			$ip4=$ip4+1;
		    }
		    else{
			print "\nError, IP>254.254.254.254\n";
			$ip3=254;
			$ip2=254;
			$ip1=254;
		    }
		}
	    }
	}
	# End of incr. IP address
	# IP address recreated
	$ipaddress="$ip4.$ip3.$ip2.$ip1";
    }
    close(FILE);  # close netdata.dat file
}else {
    print "Error, invalid IP address !\n"; # 255 or 0
}
# End of getmac.pl


                                                                                                                   