OSDN Git Service

Fixed link error
[opengatem/opengatem.git] / phpsrc / updatevendortable.php
1 <?php
2
3 /************************************************************/
4 // Create or Update the vendor table in Management DB.
5 // The table is used in opengatemchk
6 //
7 // At installation, run to create vendor table
8 // After installation, run periodically to update vendor table.
9
10 // the database connection parameters might be modified.
11 // As this script should be used only by the administrators, 
12 // it should be protected by some access control method.
13 /************************************************************/
14
15 //connect to Management DB (* set proper parameters *)
16 $link = mysqli_connect("localhost", "root", "");
17 if (!$link) die('Cannot connect to DB.'.mysqli_error());
18
19 // select opengatem database
20 $db_selected = mysqli_select_db($link, 'opengatem');
21 if (!$db_selected) die('Cannot select DB.'.mysqli_error());
22
23 // create table if not exists
24 $sql = "CREATE TABLE IF NOT EXISTS nicvendors(oui CHAR(8), org TINYTEXT)";
25 mysqli_query($link, $sql);
26
27 // open oui list in IEEE site
28 // $fp = fopen("http://standards.ieee.org/develop/regauth/oui/oui.txt", "r");
29 $fp = fopen("./oui.txt", "r");
30 if(!$fp) die('Cannot connect to IEEE data');
31
32 // scan the download list
33 while (!feof($fp)) {
34
35   // get one line
36   $line = fgets($fp);
37
38   // get oui(MAC-left-3bytes) and org(company name) from header line
39   // such as "AB-CD-EF   (hex)   Foo-bar company"
40   $pattern='/([0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2})\s*\(hex\)\s*(.*)$/';
41   if(preg_match($pattern, $line, $matches)!=1) continue;
42   $oui=$matches[1];
43   $org=$matches[2];
44
45   // replace AB-CD-EF to ab:cd:ef
46   $oui = str_replace("-",":",$oui);
47   $oui = strtolower($oui);
48
49   // remove quotation and tab code
50   $org = str_replace("\"", "", $org);
51   $org = str_replace("\t", "", $org);
52
53   // insert to db
54   $sql = sprintf(
55     'REPLACE INTO nicvendors (oui,org) VALUES( "%s", "%s")',
56     $oui, $org);
57   mysqli_query($link, $sql);
58
59   // log to console
60   print $oui." ".$org."<br>\n";
61 }
62 fclose($fp);
63 ?>
64