perl 切换 dnspod 域名记录

提供域名,dnspod 账户密码(毕竟dns密码比较重要 不能谁 cat一下都可以看到 需要base64加密),原IP,切换目标IP,

#!/bin/perl
use warnings;
use MIME::Base64;
use LWP;
use Encode;
use JSON;
use Data::Dumper;
my $ChangeDomain=‘linuxjs.com‘;
my $BeforeIP=‘XXX.XXX.XXX.XXX‘;
my $ChangeIP=‘XXX.XXX.XXX.XXX‘;
my $user= decode_base64("加密后的用户名字符串");
my $passwd= decode_base64("加密后的密码字符串");
chomp($user,$passwd);
sub GetDomainID{
    my $url=https://dnsapi.cn/Domain.Info;
    my $browser = LWP::UserAgent->new(agent => ‘checkdns.pl/1.0.0(hydezhao@gmail.com)‘);
    $response = $browser->post( $url,
     [
        login_email => $user,
        login_password => $passwd,
        format => ‘json‘,
        domain => $_[0]
    ]
    );
    if ($response->is_success != 1)
    {
    print "链接失败!\n";
    exit;
    }
    else
    {
    my $rs=$response->content;
    my $json_text = JSON->new->utf8->decode($rs);
        if ($json_text->{‘status‘}->{‘code‘} != 1 )
        {
            print $json_text->{‘status‘}->{‘message‘}."\n";
            exit;
        }
        else
        {
            return $json_text->{‘domain‘}->{‘id‘};
        }
    }
}
sub GetRecordId{
    my $url=https://dnsapi.cn/Record.List;
        my $browser = LWP::UserAgent->new(agent => ‘checkdns.pl/1.0.0(hydezhao@gmail.com)‘);
        $response = $browser->post( $url,
        [
                login_email => $user,
                login_password => $passwd,
                format => ‘json‘,
                domain_id => $_[0]
        ]
        );
        if ($response->is_success != 1)
        {
        print "链接失败!\n";
        exit;
        }
        else
        {
        my $rs=$response->content;
        my $json_text = JSON->new->utf8->decode($rs);
        if ($json_text->{‘status‘}->{‘code‘} != 1 )
        {
                print $json_text->{‘status‘}->{‘message‘}."\n";
                exit;
        }
        else
        {
        for my $record(@{$json_text->{‘records‘}})
        {
            $id_name{$record->{‘id‘}} = $record->{‘name‘}."||".$record->{‘value‘};
        }
        return %id_name;
        }
        }
}
sub UpdateRecord{
        my $url=https://dnsapi.cn/Record.Modify;
        my $browser = LWP::UserAgent->new(agent => ‘checkdns.pl/1.0.0(hydezhao@gmail.com)‘);
        $response = $browser->post( $url,
         [
                login_email => $user,
                login_password => $passwd,
                format => ‘json‘,
                domain_id => $_[0],
        record_id => $_[1],
        sub_domain => $_[2],
                value => $ChangeIP,
        record_type => $_[3],
        record_line => ‘默认‘,
        ttl => ‘600‘
    ]
        );
        if ($response->is_success != 1)
        {
        print "链接失败!\n";
        exit;
        }
        else
        {
        my $rs=$response->content;
        my $json_text = JSON->new->utf8->decode($rs);
    if ($json_text->{‘status‘}->{‘code‘} != 1)
        {
        print "code:".$json_text->{‘status‘}->{‘code‘}."  error:".$json_text->{‘status‘}->{‘message‘}."\n";
        exit;
        }
    else
    {
        my $message=$json_text->{‘record‘}->{‘name‘}."--->".$json_text->{‘record‘}->{‘value‘};
        return $message;
    }
    }
}
my $id=GetDomainID($ChangeDomain);
chomp $id;
#print "$id \n";
my %records= GetRecordId($id);
while (my ($k,$v)= each %records )
{
    my ($name,$ip)= split(/\|\|/,$v);
    if ($ip eq $BeforeIP)
    {
        chomp($name,$ip);
        print "$name--->$ip\n";
        print "changing.....\n";
        print UpdateRecord($id,$k,$name,‘A‘)."\n";
    }
}

 这个脚本 配合宕机检测机制 触发执行 可以在无人值守的情况下 短时间内 切换故障机器 把所有域名 切换到 指定备份机器

perl 切换 dnspod 域名记录

上一篇:SQL VIEW(视图)


下一篇:你们真的不明白