Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP变更信息通过server酱推送到微信 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self):
self.interval = 600
self.access_id = None
self.access_key = None
self.serverchan_sckey = None
self.open_serverchan = None

self.parser = ConfigParser.ConfigParser()
if not self.parser.read(CONF_FILE):
Expand All @@ -51,6 +53,8 @@ def __init__(self):
self.debug = self.parser.getboolean("DEFAULT", "debug")
self.access_id = self.parser.get("DEFAULT", "access_id")
self.access_key = self.parser.get("DEFAULT", "access_key")
self.serverchan_sckey = self.parser.get("DEFAULT", "serverchan_sckey")
self.open_serverchan = self.parser.getboolean("DEFAULT", "open_serverchan")
except ValueError as ex:
DDNSUtils.err_and_exit("Invalid debug in config: {0}".format(ex))
except ConfigParser.NoSectionError as ex:
Expand Down
3 changes: 3 additions & 0 deletions ddns.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ access_key=
interval=600
# Optional: turn on debug mode or not
debug=true
#server chan SCKEY http://sc.ftqq.com/
open_serverchan = false
serverchan_sckey =

[DomainRecord1]
# Required: domain name, like google.com
Expand Down
4 changes: 4 additions & 0 deletions ddns.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def main():
if not remote_record:
DDNSUtils.err("Failed finding remote DomainRecord" \
"[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
DDNSUtils.push_serverchan_msg(config.open_serverchan,config.serverchan_sckey,"更新DNS失败:外网IP[{remote}]".format(remote=remote_record.value))
continue

if current_public_ip == remote_record.value:
Expand All @@ -62,9 +63,12 @@ def main():
if not sync_result:
DDNSUtils.err("Failed updating DomainRecord" \
"[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
DDNSUtils.push_serverchan_msg(config.open_serverchan,config.serverchan_sckey,"更新DNS失败:" \
"[{rec.subdomain}.{rec.domainname}],外网IP[{remote}]".format(rec=local_record,remote=remote_record.value))
else:
DDNSUtils.info("Successfully updated DomainRecord" \
"[{rec.subdomain}.{rec.domainname}]".format(rec=local_record))
DDNSUtils.push_serverchan_msg(config.open_serverchan,config.serverchan_sckey,msg = "外网IP:[{}]".format(remote_record.value),title="IP变动")

if __name__ == "__main__":
main()
12 changes: 12 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ def get_current_time():
:return: timestamp string
"""
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")

@staticmethod
def push_serverchan_msg(open,sckey,msg,title='DDNS'):
"""
Push msg to server chan
:param msg:
:return:
"""
if open:
requests.get('https://sc.ftqq.com/{}.send?text={}&desp={}'.format(sckey,title,msg))
else:
sys.stderr.write("{0}\t[INFO]\tserver chan closed.\n".format(DDNSUtils.get_current_time()))