update_dns.pl 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/perl
  2. use strict;
  3. use DBI;
  4. use vars qw($DB_USER $DB_PASS);
  5. require 'dbi.ph';
  6. my $dsn = "DBI:mysql:database=noc";
  7. my $dbh = DBI->connect($dsn, $DB_USER, $DB_PASS, {PrintError => 0})
  8. or die "ERROR: Failed to connect to the database.";
  9. my $sql = 'SELECT name, ip_address FROM SWITCHES WHERE ip_address IS NOT NULL';
  10. my $sth = $dbh->prepare($sql);
  11. if (!$sth) {
  12. print STDERR "Failed to prepare query: " . $dbh->errstr . "\n";
  13. exit(1);
  14. }
  15. my $res = $sth->execute();
  16. if (!$res) {
  17. print STDERR "Failed to execute query: " . $dbh->errstr . "\n";
  18. exit(1);
  19. }
  20. while (my $r = $sth->fetchrow_hashref) {
  21. 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'}`;
  22. if ($out =~ /302 Not Found/) {
  23. $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'}`;
  24. if ($out !~ /100 Ok/) {
  25. print STDERR "Error adding entry for $r->{'name'}: '$out'\n";
  26. }
  27. } else {
  28. my ($h, $a) = ($out =~ /([^:]+): addr=([^;]+)/);
  29. if ($a ne $r->{'ip_address'}) {
  30. $out = `ssh -2 root\@63.231.220.23 /opt/nwreg2/local/usrbin/nrcmd -N jclarke -P jclarke zone noc.ciscolive.com removeHost $r->{'name'}`;
  31. if ($out !~ /100 Ok/) {
  32. print STDERR "Failed to remove host $r->{'name'}: '$out'\n";
  33. next;
  34. }
  35. $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'}`;
  36. if ($out !~ /100 Ok/) {
  37. print STDERR "Error adding entry for $r->{'name'}: '$out'\n";
  38. }
  39. }
  40. }
  41. }