import3750s.pl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/perl
  2. use strict;
  3. use DBI;
  4. use vars qw($PID $DB_USER $DB_PASS);
  5. require 'dbi.ph';
  6. sub trim($) {
  7. my ($str) = shift;
  8. $str //= '';
  9. $str =~ s/^\s+//g;
  10. $str =~ s/\s+$//g;
  11. return $str;
  12. }
  13. $PID = 'WS-C3750X';
  14. my $dsn = "DBI:mysql:database=noc";
  15. my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
  16. or die "ERROR: Failed to connect to the database.";
  17. open(IN, "/home/jclarke/inv3750.csv")
  18. or die "ERROR: Failed to open /home/jclarke/inv3750.csv for reading.";
  19. my @contents = <IN>;
  20. close(IN);
  21. for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
  22. my $line = $contents[$i];
  23. chomp $line;
  24. my ($mac24, undef, $serial24, $serial48, $mac48, undef) =
  25. split(/,/, $line);
  26. $mac24 = trim($mac24);
  27. $serial24 = trim($serial24);
  28. $serial48 = trim($serial48);
  29. $mac48 = trim($mac48);
  30. if ($mac24 ne '') {
  31. my @mac_parts = ($mac24 =~ /../g);
  32. $mac24 = lc(join(':', @mac_parts));
  33. }
  34. if ($mac48 ne '') {
  35. my @mac_parts = ($mac48 =~ /../g);
  36. $mac48 = lc(join(':', @mac_parts));
  37. }
  38. if ($serial24 ne '') {
  39. my $query =
  40. 'REPLACE INTO DEVICE_MAP (serial_number, pid, mac, max_ports) VALUES (?, ?, ?, ?)';
  41. my $sth = $dbh->prepare($query);
  42. $serial24 = uc $serial24;
  43. if (!$sth) {
  44. print STDERR "Failed to insert switch record: "
  45. . $dbh->errstr . "\n";
  46. next;
  47. }
  48. my @params = ($serial24, $PID, $mac24, 24);
  49. my $res = $sth->execute(@params);
  50. if (!$res) {
  51. print STDERR "Failed to insert switch record: "
  52. . $dbh->errstr . "\n";
  53. next;
  54. }
  55. }
  56. if ($serial48 ne '') {
  57. my $query =
  58. 'REPLACE INTO DEVICE_MAP (serial_number, pid, mac, max_ports) VALUES (?, ?, ?, ?)';
  59. my $sth = $dbh->prepare($query);
  60. $serial48 = uc $serial48;
  61. if (!$sth) {
  62. print STDERR "Failed to insert switch record: "
  63. . $dbh->errstr . "\n";
  64. next;
  65. }
  66. my @params = ($serial48, $PID, $mac48, 48);
  67. my $res = $sth->execute(@params);
  68. if (!$res) {
  69. print STDERR "Failed to insert switch record: "
  70. . $dbh->errstr . "\n";
  71. next;
  72. }
  73. }
  74. }