|
#!/usr/bin/env python |
|
#-*- coding:utf-8 -*- |
|
|
|
import httplib, urllib |
|
import socket |
|
import time |
|
import fcntl |
|
import struct |
|
|
|
params = dict( |
|
login_email="email", # replace with your email |
|
login_password="password", # replace with your password |
|
format="json", |
|
domain_id=100, # replace with your domain_od, can get it by API Domain.List |
|
record_id=100, # replace with your record_id, can get it by API Record.List |
|
sub_domain="www", # replace with your sub_domain |
|
record_line="默认", |
|
) |
|
current_ip = None |
|
|
|
def ddns(ip): |
|
params.update(dict(value=ip)) |
|
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"} |
|
conn = httplib.HTTPSConnection("dnsapi.cn") |
|
conn.request("POST", "/Record.Ddns", urllib.urlencode(params), headers) |
|
|
|
response = conn.getresponse() |
|
print response.status, response.reason |
|
data = response.read() |
|
print data |
|
conn.close() |
|
return response.status == 200 |
|
|
|
def getip(): |
|
sock = socket.create_connection(('ns1.dnspod.net', 6666)) |
|
ip = sock.recv(16) |
|
sock.close() |
|
return ip |
|
|
|
def get_default_iface(): |
|
'''get default route interface from proc''' |
|
f = open('/proc/net/route') |
|
for i in f: |
|
s = i.split('\t') |
|
if s[1] == '00000000': |
|
iface = s[0] |
|
break |
|
return iface |
|
|
|
def get_private_ip(ifname): |
|
'''get ip address from interface''' |
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
return socket.inet_ntoa(fcntl.ioctl( |
|
s.fileno(), |
|
0x8915, # SIOCGIFADDR |
|
struct.pack('256s', ifname[:15]) |
|
)[20:24]) |
|
|
|
if __name__ == '__main__': |
|
while True: |
|
try: |
|
ip = get_private_ip(get_default_iface()) |
|
#ip = getip() |
|
print ip |
|
if current_ip != ip: |
|
if ddns(ip): |
|
current_ip = ip |
|
except Exception, e: |
|
print e |
|
pass |
|
time.sleep(30) |