perl利用DNSPOD API獲取域名的各個地區的解析

#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use Encode;

my $mail='user';
my $pass='pass';
my $domain_info_url='https://dnsapi.cn/Domain.Info';
my $record_list_url='https://dnsapi.cn/Record.List';

sub get_domain_id {
	###獲取數據
	my ($domain)=@_;
	my $UA = LWP::UserAgent->new;
	$UA->default_header( 'User-Agent' => 'TEST DNS Client/1.0.0 ([email protected])' );
	my $response  = $UA->post( $domain_info_url, [ 'login_email' => "$mail",'login_password' => "$pass",'format' => 'json',"domain" => "$domain"] );
	my @string = $response->content;
	###利用JSON 處理數據
	my $json_obj;
	my $json = new JSON;
	foreach (@string){
		$json_obj = $json->decode("$_");
	}
	return $json_obj->{'domain'}->{'id'};
}

sub get_record_list {
	my ($domain_id,$sub_domain)=@_;
	my $UA = LWP::UserAgent->new;
	$UA->default_header( 'User-Agent' => 'TEST DNS Client/1.0.0 ([email protected])' );
	my $response  = $UA->post( $record_list_url, [ 'login_email' => "$mail",'login_password' => "$pass",'format' => 'json',"domain_id" => "$domain_id","sub_domain" => "$sub_domain"] );
	return $response->content;
}

my $json_obj;
my $json = JSON->new->utf8;
foreach (get_record_list(get_domain_id("main_domain.com"),"test")){
    $json_obj = $json->decode("$_");
}
###main_domain.com爲主域名,test二級域名
my ($cast,@t);
for my $item(@{$json_obj->{'records'}}){
  $cast = $item->{'ttl'} . "_" . $item->{'line'}  . "_" . $item->{'value'};
  push (@t,$cast);
}
#####此處可以獲取的值:           
#            "id": "744",
#            "name": "hot",
#            "line": "默認",
#            "type": "A",
#            "ttl": "60",
#            "value": "1.1.1.1",
#            "mx": "0",
#            "enabled": "1",
#            "status": "enabled",
#            "monitor_status": "",
#            "remark": "",
#            "updated_on": "2014-07-30 12:38:44",
#            "use_aqb": "no"
          
foreach (@t){
	my $re = encode("gb2312",$_);
    print "$re\n";
}

以上是自己寫的一個perl , 剛入門 寫的有點亂。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章