Program til at spore MAC Adresser på SKNET

Outputtet ser ud som flg.

garfield / # ./locatemac.php verbose 00:c0:9f:ea:67:5f
Tracing mac 00:c0:9f:ea:67:5f....
  Probing X1a    = Dalgas serverrum     : J2 = Link to X2  (Kastanievaenget)
  Probing X1b    = Dalgas blok 8        : J8 = Link to X1a (Dalgas serverrum)
  Probing X2     = Kastanievaenget      : J4 = Link to X3  (Pilevaenget)
  Probing X3     = Pilevaenget          : J7 =
  Probing X4     = Heimdal              : J3 = Link to X1a (Dalgas serverrum)
  Probing X5     = Gefionsvej           : J3 = Link to X4  (Heimdal)
  Probing X6     = Glatrupparken        : J4 = Link to X5  (Gefionsvej)

Found 00:c0:9f:ea:67:5f at X3:J7


Kildekoden saadan:

#!/usr/bin/php
<?
# Copyright (c) 2005-2007 Unifix Computer

// SwitchID May be no longer thant 4 chars

$switch[X1a][name]='Dalgas serverrum';
$switch[X1a][address]='10.0.0.x';
$switch[X1a][links][J1]='Link to X1b (Dalgas blok8)';
$switch[X1a][links][J2]='Link to X2  (Kastanievaenget)';
$switch[X1a][links][J3]='Link to X4  (Heimdal)';
$switch[X1a][community]='public';

$switch[X1b][name]='Dalgas blok 8';
$switch[X1b][address]='10.0.0.x';
$switch[X1b][links][J8]='Link to X1a (Dalgas serverrum)';
$switch[X1b][community]='public';

$switch[X2][name]='Kastanievaenget';
$switch[X2][address]='10.0.0.x';
$switch[X2][links][J3]='Link to X1a (Dalgas serverrum)';
$switch[X2][links][J4]='Link to X3  (Pilevaenget)';
$switch[X2][community]='public';

$switch[X3][name]='Pilevaenget';
$switch[X3][address]='10.0.0.x';
$switch[X3][links][J4]='Link to X2  (Kastanievaenget)';
$switch[X3][community]='public';

$switch[X4][name]='Heimdal';
$switch[X4][address]='10.0.0.x';
$switch[X4][links][J3]='Link to X1a (Dalgas serverrum)';
$switch[X4][links][J4]='Link to X5  (Gefionsvej)';
$switch[X4][community]='public';

$switch[X5][name]='Gefionsvej';
$switch[X5][address]='10.0.0.x';
$switch[X5][links][J3]='Link to X4  (Heimdal)';
$switch[X5][links][J4]='Link to X6  (Glatrupparken)';
$switch[X5][community]='public';

$switch[X6][name]='Glatrupparken';
$switch[X6][address]='10.0.0.x';
$switch[X6][links][J4]='Link to X5  (Gefionsvej)';
$switch[X6][community]='public';

$hosts[X1a][J4]='Beastie';
$hosts[X1a][J5]='Garfield';

# Dont change anything below this

$mib[dot1dTpFdbAddress] = "1.3.6.1.2.1.17.4.3.1.1";
$mib[dot1dTpFdbPort] = "1.3.6.1.2.1.17.4.3.1.2";
$mib[ifName] = "1.3.6.1.2.1.31.1.1.1.1";

function MacToDottedDec($mac)
{
        $hexnumbers=preg_split("/ |:|-/",$mac);
        $decnumbers=array_map("hexdec",$hexnumbers);
        return implode(".",$decnumbers);
}

function HarvestValue($value)
{
        $dat=preg_split('/ /',$value,-1,PREG_SPLIT_NO_EMPTY);
        return $dat[1];
}

function GetIfName($ip,$community,$if)
{
        global $mib;
        return HarvestValue(@snmpget($ip, $community, $mib[ifName].".".$if));
}

function MacToPort($ip,$community,$mac)
{
        global $mib;
        $ifID = HarvestValue(@snmpget($ip, $community, $mib[dot1dTpFdbPort].".".MacToDottedDec($mac)));
        return GetIfName($ip,$community,$ifID);
}

function LocateMac($mac,$silent = "no")
{
        global $switch,$mib;

        foreach ($switch as $sw=>$value)
        {
                $port=MacToPort($switch[$sw][address],$switch[$sw][community],$mac);

//              $ret.= "Probing $sw(".$switch[$sw][name]."):".$port;
                $switchtext=$switch[$sw][name];

                if ($text=$switch[$sw][links][$port])
                {
                        $text=$text;
                } else if ($port != '') {
                        $result[]=$sw.":".$port;
                }

                $ret.=sprintf("  Probing %-6s = %-20s : %-2s = %s\n",$sw,$switchtext,$port,$text);

        //      $ret.="\n";
        }
        if (count($result)!=0)
        {
                $ret.= "\nFound $mac at ".implode(',',$result)."\n";
        } else {
                $ret.= "\nNot found, sorry\n";
        }
        if ($silent == "no")
        {
                return $ret;
        } else {
                return implode(',',$result)."\n";
        }
}

//print MacToDottedDec("00:A0:D1:3F:1A:BF");
if ($argv[1] == 'verbose')
{
        print "Tracing mac $argv[2]....\n";
        print LocateMac($argv[2]);
} else {
        print LocateMac($argv[1],silent);
}
?>
Dette indlæg blev udgivet i Knowledge Base, Old Base. Bogmærk permalinket.

Skriv et svar