Skip to content

Instantly share code, notes, and snippets.

@nibocn
Forked from mike-zhang/reportMyIp.py
Created March 29, 2016 09:30
Show Gist options
  • Save nibocn/06ab864530693bdc5222 to your computer and use it in GitHub Desktop.
Save nibocn/06ab864530693bdc5222 to your computer and use it in GitHub Desktop.
检测动态域名并上报IP
#!/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