Created
November 15, 2012 15:43
-
-
Save mike-zhang/4079260 to your computer and use it in GitHub Desktop.
检测动态域名并上报IP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import smtplib,re,urllib2,time | |
import socket | |
from subprocess import Popen, PIPE | |
smtpServer='smtp.163.com' | |
smtpPort='25' | |
sender = '[email protected]' | |
senderPasswd = "XXXXX" | |
receivers = ['[email protected]'] | |
checkTime = 60 * 10 # seconds | |
def getPublicIp(): | |
return re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http://www.whereismyip.com").read()).group(0) | |
def getMailContent(publicIp): | |
msg2Snd = "From: <" + sender + ">\n" | |
msg2Snd += "To: <" + receivers[0] + ">\n" | |
msg2Snd += "Subject: ipReport\n\n" | |
msg2Snd += "private IP : " + re.search("\d+\.\d+\.\d+\.\d+",Popen("ifconfig", stdout=PIPE).stdout.read()).group(0) + "\n" | |
msg2Snd += "public IP : " + publicIp + "\n" | |
return msg2Snd | |
while True: | |
try: | |
publicIp = getPublicIp() | |
if publicIp==socket.gethostbyname('XXXX.oicp.net'): | |
time.sleep(checkTime) | |
continue | |
smtpObj = smtplib.SMTP(smtpServer,smtpPort) | |
smtpObj.ehlo() | |
smtpObj.login(sender,senderPasswd) | |
smtpObj.sendmail(sender, receivers, getMailContent(publicIp)) | |
print "Successfully sent email" | |
except : | |
print "Error: unable to send email" | |
time.sleep(checkTime) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
检查动态域名失效时(即:获得的公网IP与动态域名的IP不一致),上报主机ip地址,发送到指定邮箱。