Using snmptable to get MAC -> IPaddress

Some of the data gets mangled but you have a db of MAC-IPS
USAGE: getips.sh router community


Create a MySQL table:

DROP TABLE IF EXISTS ipmac;
CREATE TABLE ipmac (
  ipaddr varchar(25),
  macaddr varchar(25)
);


#!/bin/sh

insert() {
 mysql test -uuserid -ppassword -e "INSERT INTO ipmac VALUES('$2','$1')"
}

snmptable $1 $2 at.atTable| grep -v at | awk '{print $2 $3 $4 $5 $6 $7 "\t" $9}' | \
 sed 's/"//g' | grep -v at |  while read ipmacs; do

   insert $ipmacs

done


PHP front end to data



<?php

$f=mysql_connect("localhost","userid","password") or die("Database is DOWN");
if($submit) {
 if($mode == "all") {
$query = "SELECT * from ipmac";
} else {
$query = "SELECT * from ipmac WHERE macaddr = '$macaddr'";
}
$res=mysql_db_query("test",$query);
$totalips = mysql_numrows($res);

if(!$res) {
 print "ERROR: " . mysql_error();
} else {
 print "<TABLE>";
 print "<TR><TH>Ipaddress</TH><TH>MAC Address</TH></TR>";
 while($row=mysql_fetch_array($res)) {
   print "<tr><td>" . $row[0] . "</td><td>" . $row[1] . "</td></tr>";
 }
 print "</TABLE>";
}
} else {
  print "Search for a MAC address";
  print "<FORM ACTION=$PHP_SELF METHOD=post>";
  print "MAC ADDR: <INPUT TYPE=text NAME=macaddr>";
  print "Display ALL: <INPUT TYPE=checkbox NAME=mode VALUE=all>";
  print "<INPUT TYPE=submit NAME=submit VALUE=\"GET IPADDR\">";
}

?>


PCCS-Linux.COM ::ource Advocate Articles catalogue
2000 2002