0%

通过python和go调用阿里云SDK获取域名解析记录列表

通过python和go调用阿里云SDK获取域名解析记录列表

Python脚本

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
#!/usr/bin/env python
# coding=utf-8
# pip install aliyun-python-sdk-core aliyun-python-sdk-alidns

import csv
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest

ak = ""
sk = ""
domain = "zhaohongye.com"
credentials = AccessKeyCredential(ak, sk)
client = AcsClient(region_id='cn-hangzhou', credential=credentials)

page_size = 500

request = DescribeDomainRecordsRequest()
request.set_accept_format('json')
request.set_DomainName(domain)
request.set_PageSize(page_size)

response = client.do_action_with_exception(request)
record_data = json.loads(response)['DomainRecords']['Record']
total_count = json.loads(response)['TotalCount']
page_count = int(total_count // page_size) + 1

if page_count > 1:
for page_number in range(2, page_count + 1):
request.set_PageSize(page_size)
request.set_PageNumber(page_number)
response = client.do_action_with_exception(request)
record_data += json.loads(response)['DomainRecords']['Record']

csv_data_list = []
csv_header_list = ['name', 'value', 'ttl', 'status', 'type']
csv_data_list.append(csv_header_list)
for record in record_data:
# if record['Status'] == 'ENABLE' and record['Type'] in ['tmp_data','CNAME']:
# 当解析生效、A记录或CNAME类型
tmp_data = []
tmp_data.append(record['RR'])
tmp_data.append(record['Value'])
tmp_data.append(record['TTL'])
tmp_data.append(record['Status'])
tmp_data.append(record['Type'])

csv_data_list.append(tmp_data)


with open(domain+'.csv', 'w', encoding='gbk')as f:
f_csv = csv.writer(f)
# f_csv.writerows(csv_header_list)
f_csv.writerows(csv_data_list)

Go脚本

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
package main

import (
"flag"
"fmt"
"strconv"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
alidns "github.com/aliyun/alibaba-cloud-sdk-go/services/alidns"

"encoding/csv"
"os"
)

func main() {
domain := flag.String("domain", "", "domain")
flag.Parse()

var ak string = ""
var sk string = ""

config := sdk.NewConfig()

credential := credentials.NewAccessKeyCredential(ak, sk)
client, err := alidns.NewClientWithOptions("cn-hangzhou", config, credential)
if err != nil {
panic(err)
}

request := alidns.CreateDescribeDomainRecordsRequest()

request.Scheme = "https"

request.DomainName = *domain
request.PageNumber = requests.NewInteger(1)
request.PageSize = requests.NewInteger(500)

response, err := client.DescribeDomainRecords(request)
if err != nil {
fmt.Print(err.Error())
}
// fmt.Printf("response is %#v\n", response.DomainRecords.Record)

file, err := os.OpenFile(*domain+".csv", os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println("open file is failed, err: ", err)
}
defer file.Close()
// 写入UTF-8 BOM,防止中文乱码
file.WriteString("\xEF\xBB\xBF")
w := csv.NewWriter(file)
w.Write([]string{"index", "name", "value", "status", "type"})

for index, value := range response.DomainRecords.Record {
// fmt.Println(index, value.RR)
index := strconv.Itoa(index)
w.Write([]string{index, value.RR, value.Value, value.Status, value.Type})

}

if response.TotalCount > 500 {
request.PageNumber = requests.NewInteger(2)
response_new, err := client.DescribeDomainRecords(request)
if err != nil {
fmt.Print(err.Error())
}

for index, value := range response_new.DomainRecords.Record {
// fmt.Println(index, value.RR)
index := strconv.Itoa(index)
w.Write([]string{index, value.RR, value.Value, value.Status, value.Type})

}

}
// 写文件需要flush,不然缓存满了,后面的就写不进去了,只会写一部分
w.Flush()
}

// go run domain.go -domain=zhaohongye.com