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
  • Select an option

  • Save nibocn/06ab864530693bdc5222 to your computer and use it in GitHub Desktop.

Select an option

Save nibocn/06ab864530693bdc5222 to your computer and use it in GitHub Desktop.

Revisions

  1. MikeZhang revised this gist Nov 15, 2012. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions reportMyIp.py
    Original file line number Diff line number Diff line change
    @@ -14,23 +14,25 @@
    def getPublicIp():
    return re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http://www.whereismyip.com").read()).group(0)

    def getMailContent():
    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 : " + getPublicIp() + "\n"
    msg2Snd += "public IP : " + publicIp + "\n"
    return msg2Snd

    while True:
    try:
    if getPublicIp() == socket.gethostbyname('XXXXX.oicp.net'):
    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())
    smtpObj.sendmail(sender, receivers, getMailContent(publicIp))
    print "Successfully sent email"
    except :
    print "Error: unable to send email"
    print "Error: unable to send email"
    time.sleep(checkTime)
  2. MikeZhang revised this gist Nov 15, 2012. No changes.
  3. MikeZhang created this gist Nov 15, 2012.
    36 changes: 36 additions & 0 deletions reportMyIp.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/python

    import smtplib,re,urllib2,time
    import socket
    from subprocess import Popen, PIPE

    smtpServer='smtp.163.com'
    smtpPort='25'
    sender = 'XXXXX@163.com'
    senderPasswd = "XXXXX"
    receivers = ['XXXXX@gmail.com']
    checkTime = 60 * 10 # seconds

    def getPublicIp():
    return re.search('\d+\.\d+\.\d+\.\d+',urllib2.urlopen("http://www.whereismyip.com").read()).group(0)

    def getMailContent():
    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 : " + getPublicIp() + "\n"
    return msg2Snd

    while True:
    try:
    if getPublicIp() == socket.gethostbyname('XXXXX.oicp.net'):
    time.sleep(checkTime)
    continue
    smtpObj = smtplib.SMTP(smtpServer,smtpPort)
    smtpObj.ehlo()
    smtpObj.login(sender,senderPasswd)
    smtpObj.sendmail(sender, receivers, getMailContent())
    print "Successfully sent email"
    except :
    print "Error: unable to send email"