Open-AudIT
https://www.open-audit.org/phpBB3/

Systems connected to each switch port (Code)
https://www.open-audit.org/phpBB3/viewtopic.php?f=5&t=1445
Page 1 of 2

Author:  d.l.dave [ Sat Sep 30, 2006 1:51 am ]
Post subject:  Systems connected to each switch port (Code)

Just in case anyone else finds this useful, I quickly hacked this together.

If you save this as say switchport.php
Then access it with
http://openaudit/swithport.php?ip=192.1 ... ity=public

It connects to the switch/router/whatever with the specified ip address and community string using snmp and gets the mac address database. It then uses open-audit to get the names of machines for the mac addresses.

I've tried it with a few 3com and cisco switches. If you have closed vlan's then you will need to specify the vlan number in the uri. ( e.g. http://openaudit/swithport.php?ip=192.1 ... ity=public
&vlan=200 )

[code]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<title>Switch</title>
</head>
<body>

<?php
include "include_config.php";

$dot1dTpFdbAddress = ".1.3.6.1.2.1.17.4.3.1.1";
$dot1dTpFdbPort = ".1.3.6.1.2.1.17.4.3.1.2";
$dot1dBasePortIfIndex = ".1.3.6.1.2.1.17.1.4.1.2";
$community = "public";
$ip = "";
if (isset($_REQUEST["ip"])) {
$ip = $_REQUEST["ip"];
}
if (isset($_REQUEST["community"])) {
$community = $_REQUEST["community"];
}
if (isset($_REQUEST["vlan"])) {
$community = $community . "@" . $_REQUEST["vlan"];
}

$db = mysql_connect($mysql_server, $mysql_user, $mysql_password) or die("Could not connect");
mysql_select_db($mysql_database) or die("Could not select database");

echo "<h1>" . gethostbyaddr($ip) . "</h1>\n";
snmp_set_oid_numeric_print(TRUE);
snmp_set_quick_print(TRUE);
snmp_set_enum_print(TRUE);

$addresses = snmprealwalk($ip, $community, $dot1dTpFdbAddress) or die("error dbAddress");
$ports = snmprealwalk($ip, $community, $dot1dTpFdbPort) or die("error dbPort");
$interfaces = snmprealwalk($ip, $community, $dot1dBasePortIfIndex) or die("error ifIndex");
asort($ports,SORT_NUMERIC);

echo "<table border=\"1\">\n";
echo "<tr><th>Port</th><th>Interface</th><th>MAC</th><th>Name</th><th>Description</th><th>IP</th></tr>\n";

foreach (array_keys($ports) as $oid) {
$macoid = $dot1dTpFdbAddress . substr($oid,23);
$mac = str_replace(" ",":",substr($addresses[$macoid],1,17));
$intoid = $dot1dBasePortIfIndex . "." . $ports[$oid];
echo "<tr>\n";
print "<td>$ports[$oid]</td><td>$interfaces[$intoid]</td><td>$mac</td>";

$sql = "select system_name, system_man_description, system.net_ip_address from system join network_card on net_uuid = system_uuid and net_timestamp = system_timestamp join system_man on system_uuid = system_man_uuid where net_mac_address = '$mac'";
$result = mysql_query($sql);
if (!$myrow = mysql_fetch_array($result)) {
$sql = "select other_network_name, other_description, other_ip_address from other where other_mac_address = '$mac'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
}
print "<td>" . $myrow[0] . "</td>\n";
print "<td>" . $myrow[1] . "</td>\n";
print "<td>" . $myrow[2] . "</td>\n";

echo "</tr>\n";
}

echo "</table>\n";
?>

</body>
</html>
[/code]

Author:  A_Hull [ Sat Sep 30, 2006 2:26 am ]
Post subject: 

Looks cool, but I get

[code]
Fatal error: Call to undefined function snmp_set_oid_numeric_print() in C:\Program Files\xampp\htdocs\openaudit\switchport.php on line 30
[/code]

Do I need any extra libs? I have extension=php_snmp.dll in php.ini

Also a neat idea would be to add this as a link on the networked other items page for any item defines as a suitable switch.. Might require a little thought though.

Author:  d.l.dave [ Sat Sep 30, 2006 2:42 am ]
Post subject: 

yeah sorry, it was put together v quickly with no error checking.

You need snmp support enabled in php.

Just check you have php_snmp.dll in the php extensions directory and then you probably just need to uncomment [code];extension=php_snmp.dll[/code] in the php.ini.

Restart apache and you might be away!

Author:  A_Hull [ Sat Sep 30, 2006 2:56 am ]
Post subject: 

[quote="d.l.dave"]yeah sorry, it was put together v quickly with no error checking.

You need snmp support enabled in php.

Just check you have php_snmp.dll in the php extensions directory and then you probably just need to uncomment [code];extension=php_snmp.dll[/code] in the php.ini.

Restart apache and you might be away!

Tried all that, but I'll give it another whirl Monday afternoon. I might update PHP at the same time, after all what could possibly go wrong? :roll:

Author:  Mark [ Tue Oct 03, 2006 12:54 pm ]
Post subject: 

I gave it a go. Seems to work well.
I use Ubuntu, so I has to apt-get install php5-snmp, but after that, all good.
Have put it in Trunk, but there are no links to it from the menus (for now). We can add it in "officially" later.

Mark.

Author:  matze [ Tue Oct 03, 2006 10:25 pm ]
Post subject: 

SNMP:

It would be create, if we can gather data by snmp from all systems or only for the nmap-devices. I create a new thread for this.

Author:  mikeyrb [ Wed Oct 04, 2006 2:40 am ]
Post subject: 

Mark, it doesn't look like you remembered to add it (as in, I don't see it in the trunk). It won't commit unless you tell svn to add the file.

Author:  mikeyrb [ Wed Oct 04, 2006 3:18 am ]
Post subject: 

This is actually quite useful. I know the ports on the switches have been given names, but of course, they are quite old and basically unusable. This is much faster than querying the mac addresses and hunting down what machine it is! I'd be interested in knowing what other kinds of info we can get (maybe even from servers if you install snmp?).

Author:  BobDole [ Wed Oct 11, 2006 12:43 am ]
Post subject: 

[quote]Looks cool, but I get

Code:

Fatal error: Call to undefined function snmp_set_oid_numeric_print() in C:\Program Files\xampp\htdocs\openaudit\switchport.php on line 30


Do I need any extra libs? I have extension=php_snmp.dll in php.ini

I'm getting the same error, and I have the extension loaded. I'm running OA from the same system I have CACTI loaded on. Is there a specific version of PHP that this code is written for? I'm using Windows that that makes a difference.

Author:  mikeyrb [ Wed Oct 11, 2006 1:07 am ]
Post subject: 

Did you restart the apache server after loading the extension in php.ini? If not, that may be the cause. PHP should be running in isapi mode, so it loads with apache. Therefore, it only reads php.ini when apache is restarted.

Author:  BobDole [ Wed Oct 11, 2006 1:33 am ]
Post subject: 

[quote="mikeyrb"]Did you restart the apache server after loading the extension in php.ini? If not, that may be the cause. PHP should be running in isapi mode, so it loads with apache. Therefore, it only reads php.ini when apache is restarted.

The extension has been enabled for months. It's a required component to use CACTI. I'm not using Apache, I'm using IIS and PHP works fine for CACTI. Restarting IIS has not helped.

Author:  d.l.dave [ Wed Oct 11, 2006 2:18 am ]
Post subject: 

You also need PHP >= 4.3.0

Author:  BobDole [ Wed Oct 11, 2006 2:37 am ]
Post subject: 

I've got:
PHP Version 4.4.2

The SNMP section says:
UCD-SNMP Support enabled
UCD-SNMP Version ucd-snmp-4.2.3

Author:  d.l.dave [ Wed Oct 11, 2006 2:43 am ]
Post subject: 

Should probably add something like:

[code] if (function_exists("version_compare") and version_compare(phpversion(), "4.3.0", "<")) {
die ("Sorry you need PHP version 4.3.0 or greater");
}
if (!extension_loaded('snmp')) {
die ("Sorry you need to load the extension snmp")
}[/code]

to the start

Author:  Kris M [ Thu Oct 12, 2006 4:42 am ]
Post subject: 

[quote="BobDole"][quote="mikeyrb"]Did you restart the apache server after loading the extension in php.ini? If not, that may be the cause. PHP should be running in isapi mode, so it loads with apache. Therefore, it only reads php.ini when apache is restarted.

The extension has been enabled for months. It's a required component to use CACTI. I'm not using Apache, I'm using IIS and PHP works fine for CACTI. Restarting IIS has not helped.

I'm having the exactly same problem using IIS, and PHP 4.4.0.0. I've installed the php_snmp.dl extension and I believe I've installed Net-SNMP corrently (set the environment variables and the path statement etc). It just says: "Call to undefined function: snmp_set_oid_numeric_print()" so obviously php is not finding this function.

Page 1 of 2 All times are UTC + 10 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/