#!/usr/bin/perl use strict; use DBI; use vars qw($DB_USER $DB_PASS); require 'dbi.ph'; 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."; my $sql = 'SELECT name, ip_address FROM SWITCHES WHERE ip_address IS NOT NULL'; my $sth = $dbh->prepare($sql); if (!$sth) { print STDERR "Failed to prepare query: " . $dbh->errstr . "\n"; exit(1); } my $res = $sth->execute(); if (!$res) { print STDERR "Failed to execute query: " . $dbh->errstr . "\n"; exit(1); } while (my $r = $sth->fetchrow_hashref) { my $out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com showHost $r->{'name'}`; if ($out =~ /302 Not Found/) { $out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com addHost $r->{'name'} $r->{'ip_address'}`; if ($out !~ /100 Ok/) { print STDERR "Error adding entry for $r->{'name'}: '$out'\n"; } } else { my ($h, $a) = ($out =~ /([^:]+): addr=([^;]+)/); if ($a ne $r->{'ip_address'}) { $out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com removeHost $r->{'name'}`; if ($out !~ /100 Ok/) { print STDERR "Failed to remove host $r->{'name'}: '$out'\n"; next; } $out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com addHost $r->{'name'} $r->{'ip_address'}`; if ($out !~ /100 Ok/) { print STDERR "Error adding entry for $r->{'name'}: '$out'\n"; } } } }