需要下载阿里云对应的包

#!/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])
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐