IPv6動態位址DNS解析

2021-10-17 03:53:33 字數 4191 閱讀 2677

python指令碼利用阿里雲api,動態同步本地ipv6位址到dns伺服器。

最近想在家裡搭建一台小伺服器,用來當nas和遠端寫一點**。正好接了ipv6網路,就不用了內網穿透那麼麻煩了,利用阿里的給的api,把伺服器的ipv6位址放在dns伺服器上就好了。

用命令列可以查詢本機的網路資訊

# windows

ipconfig

# linux

ifconfig

正則匹配到本機的ipv6位址

re.

compile

(r"(([a-f0-9]:)[a-f0-9])"

)

def

localipv6address()

:# 判斷當前作業系統,使用命令

if(platform.system()==

'windows'):

cmd =

'ipconfig'

elif

(platform.system()==

'linux'):

cmd =

'ifconfig'

else

:print

("作業系統不相容"

) exit(

) v6_pattern = re.

compile

(r"(([a-f0-9]:)[a-f0-9])"

) r = os.popen(cmd)

text = r.read(

) r.close(

) v6_address = v6_pattern.findall(text)

return v6_address[0]

[0]

阿里雲解析dns文件

為了利用阿里的api首先要安裝兩個庫,詳情見阿里文件

pip install aliyun-python-sdk-core

pip install aliyun-python-sdk-alidns

獲取accesskeyid和accesssecret

def

alidnsupdate

(v6_address)

: client = acsclient(accesskeyid, accesssecret,

'cn-hangzhou'

) request = adddomainrecordrequest(

) request.set_accept_format(

'json'

) request.set_value(v6_address)

request.set_type(

"aaaa"

) request.set_rr(rr)

request.set_domainname(domainname)

try:

response = client.do_action_with_exception(request)

response =

str(response, encoding=

'utf-8'

)except

: response =

''return response

import os

import re

import time

import json

import platform

import requests

from settings import

*# 所有需要修改的資料

from aliyunsdkcore.client import acsclient

from aliyunsdkalidns.request.v20150109.adddomainrecordrequest import adddomainrecordrequest

deflocalipv6address()

:# 判斷當前作業系統,使用命令

if(platform.system()==

'windows'):

cmd =

'ipconfig'

elif

(platform.system()==

'linux'):

cmd =

'ifconfig'

else

:print

("作業系統不相容"

) exit(

) v6_pattern = re.

compile

(r"(([a-f0-9]:)[a-f0-9])"

) r = os.popen(cmd)

text = r.read(

) r.close(

) v6_address = v6_pattern.findall(text)

return v6_address[0]

[0]def

alidnsupdate

(v6_address)

: client = acsclient(accesskeyid, accesssecret,

'cn-hangzhou'

) request = adddomainrecordrequest(

) request.set_accept_format(

'json'

) request.set_value(v6_address)

request.set_type(

"aaaa"

) request.set_rr(rr)

request.set_domainname(domainname)

try:

response = client.do_action_with_exception(request)

response =

str(response, encoding=

'utf-8'

)except

: response =

''return response

# 如果更新失敗,返回false,否則返回true

defjudgeresponse

(r):

data = json.loads(r)

if"error"

in data:

return

false

return

true

defwritelog

(msg, address)

: data = json.loads(msg)

now_time = time.strftime(

"%y-%m-%d %h:%m:%s"

, time.localtime())

with

open

(logfile,

'w+'

, encoding=

'utf-8'

)as f:

f.write(now_time+

'\t'

+address+

'\n'

)for key in data:

value = data[key]

line =

'\t'

+str

(key)

+'\t:\t'

+str

(value)

+'\n'

f.write(line)

f.close(

)if __name__ ==

'__main__'

: pre_address =

''while

true

: v6_address = localipv6address(

)if v6_address != pre_address:

r = alidnsupdate(v6_address)

writelog(r, v6_address)

ifnot judgeresponse(r)

:break

pre_address = v6_address

time.sleep(

60)

ubuntu關閉ipv6,提高dns解析速度

大多數的 linux distro 也將 ipv6 internet protocol version 6 預設地開啟,但有時實際上不需要 ipv6 支援,如要關閉 ipv6 可用以下方法 debian ubuntu 1.開啟 etc modprobe.d aliases 2.裡面有一行是 alia...

IPv6知識概述 IPv6位址

首選格式 x x x x x x x x x表示乙個4位十六進製制數 典型的例子如下所示 2000 0000 0000 0000 0001 2345 6789 abcd ipv6位址每段中的前導0是可以去掉的,但是至少要保證每一段有乙個數字。將不必要的前導0去掉後,上述位址可以表示為 2000 0 ...

申請IPV6位址配置IPV6網域名稱

0.前言 最近弄了一下ipv6,雖然不知道什麼時候會用到,但是伺服器支援ipv6,還是有必要的。1.申請ipv6位址 到這個 去註冊乙個帳號,然後建立乙個隧道。第一次開啟這個頁面,一股80 90年代的 風格。註冊時郵箱要寫合法郵箱,要收驗證碼的。然後檢視tunnel資訊 按照提示配置好ecs伺服器網...