通过阿里云域名解析接口实时更新A记录

需要下载阿里云对应的包

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordInfoRequest import DescribeDomainRecordInfoRequest
import subprocess,json,os,sys,time

date = time.strftime("%Y-%m-%d-%H:%M:%S")

class DNS_Recahnge(object):
    def __init__(self,key,value,where):
        self.key = key
        self.value = value
        self.where = where
        self.client = AcsClient(key,value,where)

    def get_input_ip(self):
        p = subprocess.Popen("curl cip.cc | awk NR==1{'print $3'}",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
        p = p.stdout.read()[:-1]
        p = bytes.decode(p)
        self.p = p
        self.p = p
        return p
    def get_dns_ip(self):
        request = DescribeDomainRecordInfoRequest()
        request.set_accept_format('json')
        request.set_RecordId('18913465021700096')
        response = self.client.do_action_with_exception(request)
        data = str(response,encoding='utf-8')
        datas = json.loads(data)
        return datas['Value'],datas['RR']

    def check_ip(self,value,rr):
        if value == self.p and rr == 'vpn':
            print(date,value,rr)
        else:
            request = UpdateDomainRecordRequest()
            request.set_accept_format('json')
            request.set_RecordId('18913465021700096')
            request.set_Type('A')
            request.set_RR('vpn')
            request.set_Value('%s'%self.p)
            response = self.client.do_action_with_exception(request)
            print(str(response,encoding='utf-8'),date)

if __name__=="__main__":
    key = ' '
    value = ' '
    where = 'cn-hangzhou'
    DNS = DNS_Recahnge(key,value,where)
    ip = DNS.get_dns_ip()
    DNS.get_input_ip()
    DNS.check_ip(ip[0],ip[1])

发现出口ip有缓存问题导致vpn连接失败
所以优化从防火墙获取wan口出口ip地址

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordInfoRequest import DescribeDomainRecordInfoRequest
import subprocess,json,os,sys,time
import requests



date = time.strftime("%Y-%m-%d-%H:%M:%S")

class DNS_Recahnge(object):
    def __init__(self,key,value,where):
        self.key = key
        self.value = value
        self.where = where
        self.client = AcsClient(key,value,where)

    def get_input_ip(self):
        p = subprocess.Popen("curl cip.cc | awk NR==1{'print $3'}",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
        p = p.stdout.read()[:-1]
        p = bytes.decode(p)
        self.p = p
        return p

    def get_web_ip(self):
        ip = get_info(self.p)
        self.ip = ip
        return ip

    def get_dns_ip(self):
        request = DescribeDomainRecordInfoRequest()
        request.set_accept_format('json')
        request.set_RecordId('18913465021700096')
        response = self.client.do_action_with_exception(request)
        data = str(response,encoding='utf-8')
        datas = json.loads(data)
        return datas['Value'],datas['RR']

    def check_ip(self,value,rr):
        if value == self.ip and rr == 'vpn': 
            if value == self.p and rr == 'vpn':
                print(date,value,rr)
        else:
            request = UpdateDomainRecordRequest()
            request.set_accept_format('json')
            request.set_RecordId('18913465021700096')
            request.set_Type('A')
            request.set_RR('vpn')
            request.set_Value('%s'%self.ip)
            response = self.client.do_action_with_exception(request)
            print(str(response,encoding='utf-8'),date)

def get_cookie():
    LOGIN_URL = 'https://192.168.255.1/logincheck'  #请求的URL地址
    DATA = {"ajax":'1',"username":'',"secretkey":''}   #登录系统的账号密码,也是我们请求数据

    HEADERS = {
    "Content-Type" : "application/json",
    "Referer": "https://192.168.255.1/login",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
                  }
    requests.packages.urllib3.disable_warnings()
    RES = requests.post(LOGIN_URL,data=DATA,headers=HEADERS,verify = False)  #模拟登陆操作
    if RES.status_code == 200:
        return RES.cookies
    else:
        return 0

def get_info(ip):
    cookie = get_cookie()
    if cookie == 0:
        return ip
    else:
        LOGIN_URL = 'https://192.168.255.1/p/system/interface/status/wan2/'  # 请求的URL地址

        HEADERS = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "Content-Type": "application/json",
            "Referer": "https://192.168.255.1/ng/page/p/system/interface/status/wan2/",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
        }
        requests.packages.urllib3.disable_warnings()
        RES = requests.get(LOGIN_URL, cookies=cookie, verify=False)  # 模拟登陆操作
        if RES.status_code == 200:
            return (RES.text.split("span")[3].split(" ")[0][1:])
        else:
            return ip
if __name__=="__main__":
    key = ''
    value = ''
    where = ''
    DNS = DNS_Recahnge(key,value,where)
    ip = DNS.get_dns_ip()
    print(DNS.get_input_ip())
    print(DNS.get_web_ip())
    DNS.check_ip(ip[0],ip[1])
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值