import3560s.pl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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-C3560CG-8PC-S';
  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/inv3560.csv")
  18. or die "ERROR: Failed to open /home/jclarke/inv3560.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 ($mac, $serial) = split(/,/, $line);
  25. $mac = trim($mac);
  26. $serial = trim($serial);
  27. if ($mac ne '') {
  28. my @mac_parts = ($mac =~ /../g);
  29. $mac = lc(join(':', @mac_parts));
  30. }
  31. if ($serial ne '') {
  32. my $query =
  33. 'REPLACE INTO DEVICE_MAP (serial_number, pid, mac, max_ports) VALUES (?, ?, ?, ?)';
  34. my $sth = $dbh->prepare($query);
  35. $serial = uc $serial;
  36. if (!$sth) {
  37. print STDERR "Failed to insert switch record: "
  38. . $dbh->errstr . "\n";
  39. next;
  40. }
  41. my @params = ($serial, $PID, $mac, '8');
  42. my $res = $sth->execute(@params);
  43. if (!$res) {
  44. print STDERR "Failed to insert switch record: "
  45. . $dbh->errstr . "\n";
  46. next;
  47. }
  48. }
  49. }