فهرست منبع

- add reverse dns update script

Dmitriev Roman 4 سال پیش
والد
کامیت
8492140ff1
3فایلهای تغییر یافته به همراه132 افزوده شده و 1 حذف شده
  1. 31 0
      docs/addons/print-dns-commands.pl
  2. 63 0
      docs/addons/set_dns_record.pl
  3. 38 1
      scripts/Rstat/mysql.pm

+ 31 - 0
docs/addons/print-dns-commands.pl

@@ -0,0 +1,31 @@
+#!/usr/bin/perl
+
+#
+# Copyright (C) Roman Dmitiriev, rnd@rajven.ru
+#
+
+use utf8;
+use English;
+use base;
+use FindBin '$Bin';
+use lib "$Bin/";
+use strict;
+use DBI;
+use Time::Local;
+use Rstat::config;
+use Rstat::main;
+use Rstat::mysql;
+use Rstat::net_utils;
+
+my @devices=get_records_sql($dbh,'SELECT * FROM devices WHERE deleted=0 and (vendor_id=3 or vendor_id=8 or vendor_id=9) and device_type<=2 ORDER BY device_name');
+foreach my $device (@devices) {
+print "./set_dns_record.pl '$device->{device_name}' '$device->{ip}' 1\n";
+}
+
+
+my @devices=get_records_sql($dbh,'SELECT * FROM User_auth WHERE deleted=0 and dns_name IS NOT NULL and dns_name >"" ORDER BY dns_name');
+foreach my $device (@devices) {
+print "./set_dns_record.pl '$device->{dns_name}' '$device->{ip}' 1\n";
+}
+
+exit 0;

+ 63 - 0
docs/addons/set_dns_record.pl

@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+
+#
+# Copyright (C) Roman Dmitiriev, rnd@rajven.ru
+#
+
+use utf8;
+use English;
+use base;
+use FindBin '$Bin';
+no if $] >= 5.018, warnings =>  "experimental::smartmatch";
+use lib "$Bin/";
+use strict;
+use DBI;
+use Time::Local;
+use Net::Patricia;
+use NetAddr::IP;
+use Data::Dumper;
+use Rstat::config;
+use Rstat::main;
+use Rstat::mysql;
+use Rstat::net_utils;
+use File::Basename;
+use File::Path;
+
+my $h_name = $ARGV[0];
+my $ip = $ARGV[1];
+
+my $reverse = $ARGV[2] || '0';
+
+exit if (!$h_name and !$ip);
+
+my $ad_zone = get_option($dbh,33);
+my $ad_dns = get_option($dbh,3);
+
+my $subnets_dhcp = get_subnets_ref($dbh);
+my $enable_ad_dns_update = ($ad_zone and $ad_dns);
+
+my $subnet = GetSubNet($ip);
+
+log_debug("Subnet: $subnet");
+log_debug("DNS update flags - zone: $ad_zone dns: $ad_dns config: $update_hostname_from_dhcp subnet: $subnets_dhcp->{$subnet}->{dhcp_update_hostname}");
+
+#update dns block
+my $fqdn;
+if ($h_name) {
+    $fqdn=lc($h_name);
+    $fqdn=~s/_/-/g;
+    if ($fqdn!~/$ad_zone$/i) {
+            $fqdn=~s/\.$//;
+            $fqdn=lc($fqdn.'.'.$ad_zone);
+            }
+    }
+
+db_log_info($dbh,"Manual create dns record $fqdn");
+update_ad_hostname($fqdn,$ip,$ad_zone,$ad_dns,$dbh);
+
+if ($reverse) {
+    db_log_info($dbh,"Manual create dns ptr-record $fqdn => $ip");
+    update_ad_ptr($fqdn,$ip,$ad_dns,$dbh);
+    }
+
+exit;

+ 38 - 1
scripts/Rstat/mysql.pm

@@ -58,6 +58,7 @@ StrToIp
 get_first_line
 update_dns_record
 update_ad_hostname
+update_ad_ptr
 update_record
 write_db_log
 set_changed
@@ -710,6 +711,7 @@ if (!$db) {
     } else {
     db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server A: $fqdn IP: $ip");
     }
+my $ad_zone = get_option($db,33);
 my @add_dns=();
 push(@add_dns,"gsstsig");
 push(@add_dns,"server $server");
@@ -719,7 +721,42 @@ push(@add_dns,"update add $fqdn 3600 A $ip");
 push(@add_dns,"send");
 my $nsupdate_file = "/tmp/".$fqdn.".nsupdate";
 write_to_file($nsupdate_file,\@add_dns);
-do_exec('kinit -k -t /usr/local/scripts/cfg/dns_updater.keytab dns_updater@'.uc($zone).' && nsupdate "'.$nsupdate_file.'"');
+do_exec('kinit -k -t /usr/local/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && nsupdate "'.$nsupdate_file.'"');
+if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
+}
+
+#---------------------------------------------------------------------------------------------------------------
+
+sub update_ad_ptr {
+my $fqdn = shift;
+my $ip = shift;
+my $server = shift;
+my $db = shift;
+my $radr;
+my $zone;
+if ($ip =~ /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})(\/[0-9]{1,2}){0,1}/) {
+    return 0 if($1 > 255 || $2 > 255 || $3 > 255 || $4 > 255);
+    $radr = "$4.$3.$2.$1.in-addr.arpa";
+    $zone = "$3.$2.$1.in-addr.arpa";
+    }
+if (!$radr or !$zone) { return 0; }
+if (!$db) { 
+    log_info("DNS-UPDATE: Zone $zone Server: $server A: $fqdn PTR: $ip"); 
+    } else {
+    db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server A: $fqdn PTR: $ip");
+    }
+my $ad_zone = get_option($db,33);
+my @add_dns=();
+push(@add_dns,"gsstsig");
+push(@add_dns,"server $server");
+push(@add_dns,"zone $zone");
+push(@add_dns,"update delete $radr PTR");
+push(@add_dns,"update add $radr 3600 PTR $fqdn.");
+push(@add_dns,"send");
+my $nsupdate_file = "/tmp/".$radr.".nsupdate";
+write_to_file($nsupdate_file,\@add_dns);
+my $run_cmd = 'kinit -k -t /usr/local/scripts/cfg/dns_updater.keytab dns_updater@'.uc($ad_zone).' && nsupdate "'.$nsupdate_file.'"';
+do_exec($run_cmd);
 if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
 }