阿里云香港主机自动换IP

为什么要写这个脚本原因你懂的,现在都是直接封IP

pip3 install aliyun-python-sdk-alidns aliyun-python-sdk-domain aliyun-python-sdk-ecs time jsonpath requests

用计划任务跑不能小于10分钟

*/10 * * * * python3 /root/update_ip.py >>/var/log/update_ip.log 2>&1

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/26 0026 12:01
# @Author  : sanmuyan
# @FileName: aliyun_ip.py
import json, time, jsonpath,requests
from aliyunsdkcore.client import AcsClient
from aliyunsdkecs.request.v20140526 import ModifyInstanceNetworkSpecRequest, DescribeInstancesRequest
from aliyunsdkalidns.request.v20150109 import DescribeDomainRecordsRequest
from aliyunsdkalidns.request.v20150109 import AddDomainRecordRequest
from aliyunsdkalidns.request.v20150109 import DeleteDomainRecordRequest
from aliyunsdkalidns.request.v20150109 import DescribeDomainRecordInfoRequest


AccessKey = ""
Secret = ""
Regions = "cn-hongkong"


# 创建 AcsClient 实例
client = AcsClient(
    AccessKey,
    Secret,
    Regions
);

def http_test():
    for i in range(6):
        try:
            data = str(requests.head('http://mirrors.ustc.edu.cn',timeout=5))
        except:
            print("Firewall deny connect timeout=5")
        else:
            print("No proble",data)
            exit()

def ecs_Downgrade():
    request = ModifyInstanceNetworkSpecRequest.ModifyInstanceNetworkSpecRequest()
    request.set_protocol_type("https")
    request.set_InstanceId(InstanceId)
    request.set_InternetMaxBandwidthOut('0')
    response = client.do_action_with_exception(request)
    return response


def ecs_update():
    request = ModifyInstanceNetworkSpecRequest.ModifyInstanceNetworkSpecRequest()
    request.set_protocol_type("https")
    request.set_InstanceId(InstanceId)
    request.set_InternetMaxBandwidthOut('20')
    response = client.do_action_with_exception(request)
    return response


def ecs_des(InstanceId):
    request = DescribeInstancesRequest.DescribeInstancesRequest()
    request.set_protocol_type("https")
    request.set_InstanceIds(InstanceId)
    response = client.do_action_with_exception(request)
    data = json.loads(response)
    data = jsonpath.jsonpath(data, "$.Instances.Instance.[*].PublicIpAddress.IpAddress.[*]")
    global PublicIpAddress
    PublicIpAddress = ','.join(data)


def dns_des(RecordId):
    request = DescribeDomainRecordInfoRequest.DescribeDomainRecordInfoRequest()
    request.set_protocol_type("https")
    request.set_RecordId(RecordId)
    response = client.do_action_with_exception(request)
    data = json.loads(response)
    data = json.dumps(data, sort_keys=True, indent=2)
    print(data)


def dns_del():
    request = DescribeDomainRecordsRequest.DescribeDomainRecordsRequest()
    request.set_protocol_type("https")
    request.set_DomainName(DomainName)
    request.set_RRKeyWord(RR)
    response = client.do_action_with_exception(request)
    data = json.loads(response)
    TotalCount = jsonpath.jsonpath(data, "$..TotalCount")
    if TotalCount[0] != 0:
        data1 = jsonpath.jsonpath(data, "$..RR")
        data2 = jsonpath.jsonpath(data, "$..RecordId")
        if RR in data1:
            index = data1.index(RR)
            RecordId = data2[index]
            print("WARNING: Delete the existing records")
            dns_des(RecordId)
            request = DeleteDomainRecordRequest.DeleteDomainRecordRequest()
            request.set_RecordId(RecordId)
            response = client.do_action_with_exception(request)
            return response


def dns_add():
    dns_del()
    request = AddDomainRecordRequest.AddDomainRecordRequest()
    request.set_protocol_type("https")
    request.set_DomainName(DomainName)
    request.set_RR(RR)
    request.set_Type(Type)
    request.set_Value(Value)
    response = client.do_action_with_exception(request)
    data = json.loads(response)
    print("INFO: Record adding success")
    RecordId = (data['RecordId'])
    dns_des(RecordId)


if __name__ == '__main__':
    print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
    InstanceId = "实例id"
    DomainName = 'you.net'
    RR = 'hk'
    Type = 'A'
    http_test()
    ecs_Downgrade()
    time.sleep(360)
    ecs_update()
    time.sleep(10)
    ecs_des(InstanceId=[InstanceId])
    Value = PublicIpAddress
    dns_add()

 

转载于:https://www.cnblogs.com/37yan/p/8611437.html

你可能感兴趣的:(阿里云香港主机自动换IP)