12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #!/usr/bin/perl
- use strict;
- use DBI;
- use vars qw($PID $DB_USER $DB_PASS);
- require 'dbi.ph';
- sub trim($) {
- my ($str) = shift;
- $str //= '';
- $str =~ s/^\s+//g;
- $str =~ s/\s+$//g;
- return $str;
- }
- $PID = 'WS-C3560CG-8PC-S';
- my $dsn = "DBI:mysql:database=noc";
- my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
- or die "ERROR: Failed to connect to the database.";
- open(IN, "/home/jclarke/inv3560.csv")
- or die "ERROR: Failed to open /home/jclarke/inv3560.csv for reading.";
- my @contents = <IN>;
- close(IN);
- for (my $i = 1 ; $i < scalar(@contents) ; $i++) {
- my $line = $contents[$i];
- chomp $line;
- my ($mac, $serial) = split(/,/, $line);
- $mac = trim($mac);
- $serial = trim($serial);
- if ($mac ne '') {
- my @mac_parts = ($mac =~ /../g);
- $mac = lc(join(':', @mac_parts));
- }
- if ($serial ne '') {
- my $query =
- 'REPLACE INTO DEVICE_MAP (serial_number, pid, mac, max_ports) VALUES (?, ?, ?, ?)';
- my $sth = $dbh->prepare($query);
- $serial = uc $serial;
- if (!$sth) {
- print STDERR "Failed to insert switch record: "
- . $dbh->errstr . "\n";
- next;
- }
- my @params = ($serial, $PID, $mac, '8');
- my $res = $sth->execute(@params);
- if (!$res) {
- print STDERR "Failed to insert switch record: "
- . $dbh->errstr . "\n";
- next;
- }
- }
- }
|