-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_ya_dns.pl
More file actions
executable file
·121 lines (100 loc) · 2.56 KB
/
dynamic_ya_dns.pl
File metadata and controls
executable file
·121 lines (100 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env perl
use warnings;
use strict;
use YAML qw{LoadFile};
use lib::abs qw{../lib/modules};
use API::YandexDNS;
use Log qw{_print_it};
our $config = '/etc/dynamic_ya_dns.yml';
my $yml_data;
eval {
require YAML;
$yml_data = YAML::LoadFile($config);
};
if ($@) {
_print_it("cannot read YAML config: " . $@);
exit 1;
}
unless (keys %{$yml_data}) {
_print_it("YAML config empty?");
exit 1;
}
for my $domain_name (keys %{$yml_data}) {
unless (exists $yml_data->{$domain_name}->{'token'}) {
_print_it("token id must be defined for domain '$domain_name'");
next;
}
$yml_data->{$domain_name}->{"v4"} //= "auto";
$yml_data->{$domain_name}->{"v6"} //= "auto";
my $yndx = API::YandexDNS->new(
$yml_data->{$domain_name}->{'token'},
undef,
{
'ttl' => 180,
'cache' => 'true',
}
);
unless ($yndx) {
_print_it('YandexDNS->new return false');
exit 1;
}
my $exists_record = $yndx->_check_exists_record($domain_name);
for my $addr_type ( qw{v4 v6} ) {
$yml_data->{$domain_name}->{$addr_type} //= "auto";
next if ($yml_data->{$domain_name}->{$addr_type} eq "none");
my $ip_addr = $yml_data->{$domain_name}->{$addr_type};
if ($yml_data->{$domain_name}->{$addr_type} eq "auto") {
my $sock;
if ($addr_type eq "v4") {
eval {
require IO::Socket::INET;
$sock = IO::Socket::INET->new(
'Proto' => 'udp',
'PeerAddr' => 'a.root-servers.net',
'PeerPort' => '53'
);
};
} elsif ($addr_type eq "v6") {
eval {
require IO::Socket::INET6;
$sock = IO::Socket::INET6->new(
'Proto' => 'udp',
'PeerAddr' => 'a.root-servers.net',
'PeerPort' => '53'
);
};
}
if ($@) {
_print_it("IO::Socket call return error: " . $@);
next;
}
unless ($sock && $sock->sockhost) {
_print_it("cannot get $addr_type addr type");
next;
}
$ip_addr = $sock->sockhost;
}
my $record_type;
if ($addr_type eq "v4") { $record_type = "A"; }
elsif ( $addr_type eq "v6" ) { $record_type = "AAAA"; }
if ($exists_record) {
my $exist_record_flag = 0;
for my $id (keys %{$exists_record}) {
if (
exists $exists_record->{$id}->{'type'} &&
exists $exists_record->{$id}->{'content'} &&
uc($exists_record->{$id}->{'type'}) eq $record_type &&
$exists_record->{$id}->{'content'} eq $ip_addr
) {
$exist_record_flag++;
last;
}
}
if ($exist_record_flag) { next; }
}
unless ($yndx->set_record($domain_name, $record_type, $ip_addr) ) {
_print_it('cannot set record: ' . $domain_name . ' IN ' . $record_type . ' ' . $ip_addr);
}
}
}
exit;