Skip to content

Tutorials

Remonsan edited this page Feb 6, 2018 · 2 revisions

目录

准备工作

环境

  1. Python2 >= 2.7.9Python3 >=3.3.0

安装

pip install --upgrade dnsdb-python-sdk

安装到Mac OS X

在Mac OS X使用上面的方法安装, 在使用时可能出现以下错误:

requests.exceptions.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)

这是Mac OS X pyOpenSSL版本太旧导致, 您可以通过homebrew重新安装python解决,具体安装步骤如下:

  1. 通过brew安装python

    brew install python --with-brewed-openssl
  2. 将python的bin路径加入到PATH环境变量中

    export PATH=/usr/local/Cellar/python/2.7.11/bin/:$PATH
  3. 创建符号链接

    mkdir -p /usr/local/opt/python/bin/
    ln -s /usr/local/Cellar/python/2.7.11/bin/python2.7 /usr/local/opt/python/bin/python2.7
  4. 安装

    pip install --upgrade dnsdb-python-sdk

使用

DnsDB Python API主要使用dnsdb_sdk.api.APIClient类。

APIClient常用的几个方法:

  • get_api_user 获取当前API User的信息。
  • search_dns 查询DNS记录,支持分页查询,最多可获取前10000条查询结果。
  • scan_dns 查询DNS记录,不支持分页查询,可以获取所有的查询结果。

创建客户端

APIClient在初始化时需要指定api_idapi_key

from __future__ import print_function
from dnsdb_sdk.api import APIClient

api_id = "your API ID"
api_key = "your API key"
client = APIClient(api_id, api_key)

如果您还没有api_keyapi_id, 你可以查看这里

获取API User

通过APIClient.get_api_user获取当前API User的信息。

api_user = client.get_api_user()
print(api_user.remaining_requests) # 打印该API User的剩余API请求次数
print(api_user.user) # 打印该API User所属的用户名

查询DNS

查询DNS记录可以使用APIClient.search_dns方法, 该方法单次调用最多返回50条查询结果, 每成功调用一次扣除一次API查询次数,同一个查询条件最多获取前10000条结果。

APIClient.search_dns至少需要指定domainhostipvalue_domainvalue_hostvalue_ipemail中的一个参数,否则会抛出异常

查询某个顶级私有域名的DNS记录

result = client.search_dns(domain='github.com')

查询指定host值的DNS记录

result = client.search_dns(host='www.github.com')

查询解析到8.8.8.8的DNS记录

result = client.search_dns(ip='8.8.8.8')

查询某个顶级私有域名的所有A记录

result = client.search_dns(domain='github.com', dns_type='A)

指定page参数, 可以获取指定页码的查询结果, page默认为1

result = client.search_dns(domain='github.com', page=2)  # 获取第二页查询结果

resultdnsdb_sdk.api.SearchResult的实例

print(result.total)  # 查询结果总数
print(result.remaining_request)  # 剩余查询次数
print(len(result))  # 当前返回的结果数量

遍历查询结果

for record in result:
    print(record.host,record.type, record.value)

获取所有DNS查询结果

如果您想获取某个DNS查询的所有结果,您可以使用APIClient.scan_dns方法实现, 调用该方法会扣除一次API请求次数。遍历结果时会根据遍历的数据量扣除相应的API请求次数

查询某个顶级域名的所有DNS记录

result = client.scan_dns(domain='github.com')  # 这里会扣除一次API请求次数

resultdnsdb_sdk.api.ScanResult的实例。获取满足查询条件的结果总数

total = len(result)  # 这里不会扣除API请求次数

遍历查询结果, 这里会根据你遍历的查询结果数扣除相应的API请求次数, 如果您提前结束遍历, 可能会减少扣除的API请求次数

for record in result:
    print(record)

遍历ScanResult时, 它会自动多次调用APIClient.next_dns_scan,每调用一次,扣除一次API请求次数。

配置代理

HTTP/HTTPS代理

from dnsdb_sdk.api import APIClient

api_id = "your API ID"
api_key = "your API key"
proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'http://10.10.1.10:1080',
}
client = APIClient(api_id, api_key, proxies=proxies)

代理服务器使用HTTP Basic Auth时,格式为:http://user:password@host/:

proxies = {'http': 'http://user:pass@10.10.1.10:3128/'}

SOCKS代理

from dnsdb_sdk.api import APIClient

api_id = "your API ID"
api_key = "your API key"
proxies = {
    'http': 'socks5://user:pass@host:port',
    'https': 'socks5://user:pass@host:port'
}
client = APIClient(api_id, api_key, proxies=proxies)
Clone this wiki locally