浏览代码

linux run.sh 和定时任务脚本

NewFuture 9 年之前
父节点
当前提交
8005970293
共有 4 个文件被更改,包括 21 次插入8 次删除
  1. 11 5
      dnspod.py
  2. 3 3
      run.py
  3. 3 0
      run.sh
  4. 4 0
      task.sh

+ 11 - 5
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")

+ 3 - 3
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():

+ 3 - 0
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"

+ 4 - 0
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;