Unifix knowledge database - The original one

Dashboard - News - Search - List - About - Admin


Program til at spore MAC Adresser på SKNET v0.2

Author:mike - Wed Jul 18 02:22:35 2007

Programmet er nu udvidet med hjælpeskærm, samt ban/unban funktioner.


#!/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.201';
$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]='data';

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

$switch[X2][name]='Kastanievaenget';
$switch[X2][address]='10.0.0.203';
$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.204';
$switch[X3][links][J4]='Link to X2  (Kastanievaenget)';
$switch[X3][community]='public';

$switch[X4][name]='Heimdal';
$switch[X4][address]='10.0.0.205';
$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.206';
$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.207';
$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";
$mib[ifStatus] = "1.3.6.1.2.1.2.2.1.7";

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";
        }
}

function PortToIfNo($sw,$port)
{
        global $switch,$mib;

        //IF-MIB::ifOperStatus
        //return HarvestValue(@snmpget($ip, $community, $mib[ifName].".".$if));
        for ($i=1; $i<=128; $i++)
        {
        //      print "Fetch $i";
                $list[HarvestValue(@snmpget($switch[$sw][address], $switch[$sw][community], $mib[ifName].".".$i))]=$i;
        }
        return $list[$port];
}

function Ban($port,$ban)
{
        global $switch,$mib;
        $v=explode(":",$port);
//      print "IfNo:".$ifno=PortToIfNo($v[0],$v[1])."\n";
        $oid=$mib[ifStatus].".".PortToIfNo($v[0],$v[1]);
        $address=$switch[$v[0]][address];
        $community=$switch[$v[0]][community];
//      print "Oid: '$oid'\n";
//      print "Address: $address\n";
//      print "Community: $community\n";
//      print "snmpset($address,$community,$oid,\"i\",1);\n";

        if ($ban == "ban")
        {
                print "Switch $v[0] Port $v[1].... ";
                snmpset($address,$community,$oid,"i",2);
                print "Banned\n";
        } elseif ($ban == "unban") {
                print "Switch $v[0] Port $v[1].... ";
                snmpset($address,$community,$oid,"i",1);
                print "UnBanned\n";
        } else
        {
                print "Current value:".snmpget($address, $community, $oid)."\n";
        }
}

//print MacToDottedDec("00:A0:D1:3F:1A:BF");

switch ($argv[1])
{
        case "ban":
                Ban($argv[2],"ban");
                Ban($argv[2],"status");
                exit;
        case "unban":
                Ban($argv[2],"unban");
                Ban($argv[2],"status");
                exit;
        case "status":
                Ban($argv[2],"status");
                exit;
        exit;
        case "verbose":
                print "Tracing mac $argv[2]....\n";
                print LocateMac($argv[2]);
        exit;
        case "lookup":
                print LocateMac($argv[2],silent);
        exit;
}
print "LocateMac v0.2 Copyright (c) 2005-2007 Uni-fix Computer & Mc Solutions\n\n";
print "/locatemac.php [command] [parm]\n\n";

print "  lookup [mac]   - Gives you the portname from where the mac are being used.\n";
print "  verbose [mac]  - Gives you the portname from where the mac are being used.\n";
print "                   but shows you a lot extra information\n";
print "  ban [port]     - Takes the given port down for mainenance.\n";
print "  unban [port]   - Undo the previous command.\n";
?>















Copyright(c) Unifix.org 2002-2011


Dashboard - News - Search - List - About - Admin
 
Powered by Mikjaer Consulting Solutions - Hosted by Specialhosting.dk