From 80059702938f043420366c1ed41228e2eb25ab6c Mon Sep 17 00:00:00 2001 From: NewFuture Date: Mon, 7 Nov 2016 11:56:46 +0800 Subject: [PATCH] =?UTF-8?q?linux=20run.sh=20=E5=92=8C=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dnspod.py | 16 +++++++++++----- run.py | 6 +++--- run.sh | 3 +++ task.sh | 4 ++++ 4 files changed, 21 insertions(+), 8 deletions(-) create mode 100755 run.sh create mode 100755 task.sh diff --git a/dnspod.py b/dnspod.py index b15bfd8..b1123b0 100644 --- a/dnspod.py +++ b/dnspod.py @@ -25,7 +25,13 @@ def request(action,method='POST',**params): # 发送请求 conn.close() if response.status == 200: - return json.loads(data) + data = json.loads(data) + if not data: + raise Exception("empty response") + elif data.get("status",{}).get("code") == "1": + return data + else: + raise Exception(data.get('status',{})) else: return None @@ -84,7 +90,7 @@ def get_records(id, **conditions): if not id in get_records.records: get_records.records[id]={} data = request('Record.List', domain_id=id) - if data and data.get("status",{}).get("code") == "1": + if data: for r in data.get('records'): get_records.records[id][r["id"]]={k: v for (k, v) in r.items() if k in get_records.keys} @@ -100,7 +106,7 @@ def get_records(id, **conditions): def update_record(domain, value, record_type="A"): # 更改记录 domainid,sub = get_domain_info(domain) if not domainid: - raise "invalid domain"+domain + raise Exception("invalid domain: [ %s ] "%domain) records = get_records(domainid,name=sub,type=record_type) result={} @@ -109,7 +115,7 @@ def update_record(domain, value, record_type="A"): # 更改记录 for (id,record) in records.items(): if record["value"] != value: r=request('Record.Modify',record_id=id,record_line=record["line"].encode("utf-8"),value=value, sub_domain=sub,domain_id=domainid,record_type=record_type,ttl=600) - if r and r.get("status",{}).get("code") == "1": + if r : get_records.records[domainid][id]["value"]=value result[id]=r.get("record") else: @@ -119,7 +125,7 @@ def update_record(domain, value, record_type="A"): # 更改记录 else: # create #http://www.dnspod.cn/docs/records.html#record-create r = request("Record.Create", domain_id=domainid, value=value, sub_domain=sub, record_type=record_type, record_line="默认", ttl=600) - if r and r.get("status",{}).get("code") == "1": + if r : id = r.get("record")["id"] get_records.records[domainid][id]=r.get("record") result=r.get("record") diff --git a/run.py b/run.py index 0656a90..69f37ad 100755 --- a/run.py +++ b/run.py @@ -21,8 +21,8 @@ def get_config(key=None, file="config.json"): def update(): - print time.ctime() - index4=get_config('index4') or 0 + print "="*25+" "+time.ctime()+" "+"="*25 + index4=get_config('index4') or "default" if str(index4).isdigit(): ipv4 = ip.local_v4(index4) else: @@ -32,7 +32,7 @@ def update(): for domain in get_config('ipv4'): print dnspod.update_record(domain, ipv4, 'A') - v6_domains=get_config("ipv6") + v6_domains=get_config("ipv6") or "default" if len(v6_domains) > 0: index6=get_config('index6') if str(index6).isdigit(): diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..980beb6 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +RUN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; +"$RUN_DIR/run.py" >> "$RUN_DIR/run.log" \ No newline at end of file diff --git a/task.sh b/task.sh new file mode 100755 index 0000000..cef02af --- /dev/null +++ b/task.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +RUN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; +echo "*/5 * * * * root ${RUN_DIR}/run.sh" > /etc/cron.d/ddns; +/etc/init.d/cron reload;