Created
January 29, 2021 12:24
-
-
Save michelep/8fa4e6f629022d2874019c3de13b281a to your computer and use it in GitHub Desktop.
Get all IP and MACs addresses from a Cisco router via SNMP and save into a MySQL table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// Questa procedura preleva dai routers, via SNMP, l'elenco degli indirizzi IP e relativi MAC connessi, salvandoli su una tabella MySQL: | |
// | |
// -- | |
// -- Struttura della tabella `ipMACs` | |
// -- | |
// | |
// CREATE TABLE IF NOT EXISTS `ipMACs` ( | |
// `IP` char(16) NOT NULL, | |
// `MAC` char(18) NOT NULL, | |
// `firstSeen` datetime DEFAULT NULL, | |
// `lastSeen` datetime DEFAULT NULL, | |
// PRIMARY KEY (`IP`,`MAC`) | |
// ) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
// | |
include_once __DIR__."/../common.inc.php"; | |
use PhpIP\IPBlock; | |
function getMACs($routerIP) { | |
echo "Walking on $routerIP...\n"; | |
// Get all MACs... | |
$macResult = snmprealwalk($routerIP, "public", "IP-MIB::ipNetToMediaPhysAddress"); | |
if(count($macResult) > 0) { | |
foreach ($macResult as $key => $value) { | |
// echo "$key=>$value\n"; | |
// [IP-MIB::ipNetToMediaPhysAddress.4.172.16.68.43] => STRING: 0:8:9b:d8:a3:8c | |
if(preg_match('/^IP\-MIB::ipNetToMediaPhysAddress\.(\d+)\.(\d+\.\d+\.\d+\.\d+)/m', $key, $matches)) { | |
$ip = $matches[2]; | |
// Extract MAC | |
if(preg_match('/STRING: ([0-9A-Fa-f]{1,2}:[0-9A-Fa-f]{1,2}:[0-9A-Fa-f]{1,2}:[0-9A-Fa-f]{1,2}:[0-9A-Fa-f]{1,2}:[0-9A-Fa-f]{1,2})/m', $value, $matches)) { | |
$mac = $matches[1]; | |
doQuery("INSERT IGNORE INTO ipMACs(IP,MAC,firstSeen) VALUES ('$ip','$mac',NOW()) ON DUPLICATE KEY UPDATE lastSeen=NOW();"); | |
} | |
} | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment