﻿{"id":1119,"date":"2019-06-28T16:55:18","date_gmt":"2019-06-28T08:55:18","guid":{"rendered":"http:\/\/www.tansoo.cn\/?p=1119"},"modified":"2019-10-11T20:21:38","modified_gmt":"2019-10-11T12:21:38","slug":"%e6%9b%b4%e6%96%b0%e9%98%bf%e9%87%8c%e4%ba%91dns%e8%a7%a3%e6%9e%90%e5%ae%bd%e5%b8%a6%e5%8a%a8%e6%80%81ip%ef%bc%8c%e4%bd%9c%e4%b8%baddns%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.tansoo.cn\/?p=1119","title":{"rendered":"\u5229\u7528\u963f\u91cc\u4e91API\u81ea\u52a8\u66f4\u65b0\u5bbd\u5e26\u52a8\u6001IP\u89e3\u6790\uff0c\u4f5c\u4e3aDDNS\u4f7f\u7528"},"content":{"rendered":"<p>\u539f\u6587\u5f15\u7528\uff1a<a href=\"http:\/\/bbs.atoo.top:8081\/viewtopic.php?f=1&amp;t=4\">http:\/\/bbs.atoo.top:8081\/viewtopic.php?f=1&amp;t=4<\/a><\/p>\n<p>\u82b19\u5143\u94b1\u5728\u963f\u91cc\u4e91\u4e0a\u4e91\u6ce8\u518c\u4e00\u4e2a\u201catoo.top\u201d\u7684\u9876\u7ea7\u57df\u540d\uff0c\u518d\u5229\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u5b9a\u65f6\u66f4\u65b0\u4f60\u5bb6\u8def\u7531\u5668\u7684\u52a8\u6001IP\uff0c\u4e0d\u7ba1\u5bbd\u5e26IP\u600e\u4e48\u53d8\uff0c\u90fd\u53ef\u4ee5\u6b63\u786e\u8bbf\u95ee\u4e86\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86DDNS\u7684\u6548\u679c\u4e86\u3002<br \/>\n\u4e0b\u9762Python\u4ee3\u7801\u4e2d\u7684AcsClient\uff08\uff09\u8fd9\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u8bf7\u5728https:\/\/help.aliyun.com\/document_detail\/53090.html \u4e0a\u67e5\u770b\u3002<\/p>\n<p><!--more--><\/p>\n<pre class=\"lang:python decode:true \"># \u963f\u91cc\u652fDNS\u81ea\u52a8\u66f4\u65b0IP\u5730\u5740\u7a0b\u5e8f\uff08Python\uff09\r\n# \u6587\u4ef6\u540d  \uff1a aliyundns.py\r\n# \u8c03\u7528\u65b9\u6cd5\uff1a python aliyundns.py www.atoo.top\r\n\r\n#!\/usr\/bin\/env python\r\n#coding=utf-8\r\n\r\nimport sys\r\nimport json \r\nimport urllib\r\nimport datetime\r\nfrom aliyunsdkcore.client import AcsClient\r\nfrom aliyunsdkcore.acs_exception.exceptions import ClientException\r\nfrom aliyunsdkcore.acs_exception.exceptions import ServerException\r\nfrom aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest\r\nfrom aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest\r\n\r\nif len(sys.argv)&lt;=1:\r\n    print 'Usage: python aliyundna.py www.atoo.top'\r\n    sys.exit(0)\r\n    \r\naccessKeyId = 'LTAIjO*****BfI'\r\naccessSecret = 'JlvkmaCPek3SqqYaF*******8Zp'\r\n\r\nDomainName = sys.argv[1]   \r\n\r\n# print sys.argv[0], sys.argv[1], sys.argv[2]\r\n\r\nclient = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')\r\n\r\nrequest = DescribeSubDomainRecordsRequest()\r\nrequest.set_accept_format('json')\r\nrequest.set_SubDomain(DomainName)\r\nresponse = client.do_action_with_exception(request)\r\n# print response\r\nresponse = json.loads(response.decode())\r\nRecordId = response['DomainRecords']['Record'][0]['RecordId']\r\nRR       = response['DomainRecords']['Record'][0]['RR']\r\nold_ip   = response['DomainRecords']['Record'][0]['Value']\r\nnew_ip = urllib.urlopen(\"http:\/\/ip.42.pl\/raw\").read()\r\n\r\nif old_ip != new_ip:\r\n    request = UpdateDomainRecordRequest()\r\n    request.set_accept_format('json')\r\n\r\n    request.set_Value(new_ip)\r\n    request.set_Type(\"A\")\r\n    request.set_RR(RR)\r\n    request.set_RecordId(RecordId)\r\n    response = client.do_action_with_exception(request)\r\n\r\n    print datetime.datetime.now(), ' ', DomainName,': ', old_ip, ' =&gt; ', new_ip \r\nelse :\r\n    print datetime.datetime.now(), ' ', DomainName, ': IP has not changed.', new_ip\r\n<\/pre>\n<p>\u8c03\u7528\u65b9\u6cd5\uff1a<span style=\"color: #ff0000;\"><strong> python? aliyundns.py? <a class=\"postlink\" style=\"color: #ff0000;\" href=\"http:\/\/www.atoo.top\/\">www.atoo.top<\/a><\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5f15\u7528\uff1ahttp:\/\/bbs.atoo.top:8081\/viewtopic.php?f=1&amp;t=4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[68,70,69],"class_list":["post-1119","post","type-post","status-publish","format-standard","hentry","category-program","tag-ddns","tag-python","tag-ip"],"_links":{"self":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1119"}],"version-history":[{"count":7,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1119\/revisions"}],"predecessor-version":[{"id":1166,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1119\/revisions\/1166"}],"wp:attachment":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}