# m_getPortInfo.pl: module for SIP911.pl
# This module SNMP queries switches to determine the port/slot information.
#
# Usage: getPortInfo($Switch, $ReadCommunityString, $SourceHex)
#
# Copyright 2002 (c) year by Columbia University. All rights reserved.
#!/bin/env perl

sub getPortInfo
{
    my ($Switch, $ReadCommunityString, $SourceHex) = @_;
    my ($EliminateString) = ".iso.3.6.1.2.1.17.4.3.1.1";
    my ($CurrentLine);
    my ($Index);
    my ($TempIndex);
    my (@VlanArray);
    my (%DescriptionHash);
    my ($BinDir) = 'D:\\Work\\usr\\bin';
    my ($Continue) = 1;
    my ($Found) = 0;
    my (@Result1);
    my (@Result2);

    while ($Continue){
	my (@Result) = `$BinDir\\snmpwalk -Oq -m all $Switch $ReadCommunityString .1.3.6.1.2.1.31.1.1.1.1`;
	while (@Result){
	    $CurrentLine = shift(@Result);
	    if ($CurrentLine =~ /VLAN/){
		my (@TempArray) = split(/\s/, $CurrentLine);
		$TempIndex = $TempArray[1];
		@TempArray = split(/-/, $TempIndex);
		@TempArray = split(/\"/, $TempArray[1]);
		$Index = $TempArray[0];
		if($debug) {print "Found a new vlan:".$Index."\n"};
		push (@VlanArray, $Index);
	    }
	}  # This while gets all the vlans.
	
	# This for-loop goes through vlans and attempts to find the matching HEX.
	my ($i);
	while(!$Found){
	    $i = shift(@VlanArray); #Assigning the next Vlan to query MAC table.
	    if($debug) {print "Current Vlan:".$i."\n";}
	    # SNMP query MAC table
	    @Result1 = `$BinDir\\snmpwalk -Oq -m all $Switch $ReadCommunityString\@$i .1.3.6.1.2.1.17.4.3.1.1`;
	    while (@Result1){
		$CurrentLine = shift(@Result1);
		#print $CurrentLine;
		if ($CurrentLine =~ /$SourceHex/){
		    @TempArray = split(/\"/, $CurrentLine);
		    if ($debug) {print "Script found the MAC!: ".$TempArray[1]."\n"; } # This is the matching HEX
		    $TempArray[0] =~ s/$EliminateString//g;
		    $TempSignficantOID = $TempArray[0];
		    if ($debug) {print "Significant OID".$TempSignficantOID."\n"; } # This is the significant address
		    
		    my ($GetBridgePortNum) = `$BinDir\\snmpwalk -Oq -m all $Switch $ReadCommunityString\@$i .1.3.6.1.2.1.17.4.3.1.2$TempSignficantOID`;
		    @TempArray = split(/\s/, $GetBridgePortNum);
		    $TempSignficantOID = $TempArray[1];
		    if($debug) {print "BridgePortNum:".$TempArray[1]."\n";}
		    my ($GetIfIndex) = `$BinDir\\snmpwalk -Oq -m all $Switch $ReadCommunityString\@$i .1.3.6.1.2.1.17.1.4.1.2.$TempSignficantOID`;
		    @TempArray = split(/\s/, $GetIfIndex);
		    $TempSignficantOID = $GetIfIndex = $TempArray[1];
		    if($debug) {print "Interface Index:".$TempArray[1]."\n";}
		    my ($GetIfName) = `$BinDir\\snmpwalk -Oq -m all $Switch $ReadCommunityString\@$i .1.3.6.1.2.1.31.1.1.1.1.$TempSignficantOID`;
		    @TempArray = split(/\s/, $GetIfName);
		    $TempArray[1] =~ s/\"//g;
		    my ($ReturnPort) = $TempArray[1];
		    if($debug) {print "Interface Name:".$ReturnPort."\n";}
		    
		    my ($LearnedByCDP) = 0;

		    # This SNMP query gets CDP participaing IfIndex
		    if($debug) {print "Testing if ".$ReturnPort." is a CDP port\n";}
		    @Result2 = `$BinDir\\snmpwalk -Oq -m all $Switch $ReadCommunityString .1.3.6.1.4.1.9.9.23.1.2.1.1.3`;
		    while (@Result2) {
			$CurrentLine = shift(@Result2);
			if($debug) {print $CurrentLine;}
			@TempArray = split(/\s/, $CurrentLine);
			$EliminateString = ".iso.3.6.1.4.1.9.9.23.1.2.1.1.3.";
			$TempArray[0] =~ s/$EliminateString//g;
			@TempArray = split(/\./, $TempArray[0]);
			if ($TempArray[0] =~ /$GetIfIndex/){
			    if($debug){print "This MAC is indirectly learned:".$TempArray[0]."\n";}


			    #find out where it is learning from and set switch to that name.
			    #$Found is set to 1 to exit the while loop, preventing it 
			    #from continue searching through other vlans
			    $Found = 1;
			    $Continue = 1;
			    $LearnedByCDP = 1;
			    undef @Result2;
			}
		    }
		    
		    if($LearnedByCDP){}else{
			$Continue = 0; #Stop Continue While loop
			$Found = 1; # Stop Found While loop

			if($debug) {print "Source is connected to ".$ReturnPort." of ".$Switch.".\n";}
			$Switch =~ s/\.net\.columbia\.edu//g;
			@TempArray = split(/\//, $ReturnPort);
			&pullPortInfo($Switch, $TempArray[0], $TempArray[1]);			
			undef @Result;
		    } # end-of-if
		} # end-of if: current is the HEX value ~ "if ($CurrentLine =~ /$SourceHex/){"
	    } # end-of while: there are HEX values in MAC table ~ "while (@Result1){"
	} # end-of while: MAC hasn't been found ~ "while(!$Found){"
    }
} #End of GetPortInfo function

1;
