import_macvendors.pl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin";
  7. use strict;
  8. use Time::Local;
  9. use FileHandle;
  10. use Rstat::config;
  11. use Rstat::main;
  12. use Rstat::mysql;
  13. use Rstat::net_utils;
  14. use Data::Dumper;
  15. use DBI;
  16. use Time::Local;
  17. use Date::Parse;
  18. use JSON;
  19. my @batch_sql=();
  20. open(MACS, "<./updates/20200218/macaddress.io-db.json") || die "./updates/20200218/macaddress.io-db.json not found!";
  21. while (my $line = <MACS>) {
  22. next unless defined $line;
  23. chomp($line);
  24. $line=~s/false/0/;
  25. $line=~s/true/1/;
  26. my $hashRef = decode_json($line);
  27. next if (!$hashRef);
  28. my $oui=$hashRef->{oui};
  29. my $private=$hashRef->{isPrivate};
  30. my $name=$hashRef->{companyName};
  31. my $addr=$hashRef->{companyAddress};
  32. my $country=$hashRef->{countryCode};
  33. my $block=$hashRef->{assignmentBlockSize};
  34. my $created=$hashRef->{dateCreated};
  35. my $updated=$hashRef->{dateUpdated};
  36. my $mac=mac_simplify($oui);
  37. $name=~s/\"//g;
  38. $addr=~s/\"//g;
  39. my $found=get_count_records($dbh,'mac_vendors','oui=".$mac."');
  40. if ($found) {
  41. push(@batch_sql,'UPDATE TABLE mac_vendors set oui="'.$mac.'", isprivate='.$private.', companyName="'.$name.'", companyAddress="'.$addr.'", countryCode="'.$country.'", assignmentBlockSize="'.$block.'", dateCreated="'.$created.' 00-00-00", dateUpdated="'.$updated.' 00-00-00" where oui="'.$mac.'"');
  42. } else {
  43. push(@batch_sql,'INSERT INTO mac_vendors (oui,isprivate,companyName,companyAddress,countryCode,assignmentBlockSize,dateCreated,dateUpdated) VALUES("'.$mac.'",'.$private.',"'.$name.'","'.$addr.'","'.$country.'","'.$block.'","'.$created.' 00-00-00","'.$updated.' 00-00-00")');
  44. }
  45. }
  46. close(MACS);
  47. $dbh->{AutoCommit} = 0;
  48. my $sth;
  49. foreach my $sSQL(@batch_sql) {
  50. $sth = $dbh->prepare($sSQL);
  51. $sth->execute;
  52. }
  53. $sth->finish;
  54. $dbh->{AutoCommit} = 1;
  55. exit;