ipv6-ddns脚本(python,dnspod-api)

存在的问题:

运营商分配的IPV6地址经常改变,而改变后原有的地址却不会被释放,导致设备存在多个IPv6地址。本打算重启网卡释放,但有时网卡down后无法up。后来发现shell命令获取的首个ipv6地址通常是活跃地址(没遇到过过期地址),就不管过期地址了。

import requests
import json
import os

# ----------remote_ipv6----------
record_query = 'https://dnsapi.cn/Record.Info'
recode_update = 'https://dnsapi.cn/Record.Modify'
login_token = '1****2,d******************************7'
domain_id = '8******8'  # dx3.top
record_id = '6*******4' # box.tl.dx3.top
sub_domain = 'box.ipv6'

head = {"Content-Type": "application/x-www-form-urlencoded"}
data = 'login_token=%s&format=json&domain_id=%s&record_id=%s' % (
    login_token, domain_id, record_id)

req = requests.post(record_query, headers=head, data=data)
result = json.loads(req.text)

remote_ipv6 = result['record']['value']
print('remote_ipv6:',remote_ipv6)
# -------------------------------

# ----------local_ipv6----------
ipv6 = os.popen(
    "ip addr | grep -A1 'inet6 [^f:]' | grep 'ff:fe' | awk '{print $2}' | cut -d'/' -f1")
# os.popen返回是一个os._wrap_close对象,使用readlines()方法读取内容
ipv6 = ipv6.readlines()
# readlines()读取的内容为list,每行占一个长度,判断list长度即可得出当前ipv6地址个数
ipv6_num = len(ipv6)
local_ipv6 = ipv6[0].strip('\n')
print('local_ipv6:',local_ipv6)
# ------------------------------

#if ipv6_num == 1:
if remote_ipv6 != local_ipv6:
    # 参数:login_token,domain_id,record_id,sub_domain,record_type,record_line_id,value
    data = 'login_token=%s&format=json&domain_id=%s&record_id=%s&sub_domain=%s&record_type=%s&record_line_id=%s&value=%s' % (
        login_token, domain_id, record_id,  sub_domain, 'AAAA', '0', local_ipv6)
    req = requests.post(recode_update, headers=head, data=data)
#else :
    # 重启网卡
    # os.popen('ifconfig wlan0 down;ifconfig wlan0 up;')
    #print('more than 1 ipv6,wlan0 down&up')

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇