以下脚本适用于python2.7,可以实现每30秒获取一次IP地址,并与上一次获取的地址进行比对,若有变化则发送邮件通知。
另存为test.py文件后,在coreelec的ssh远程终端中使用linux命令:
python /var/media……/test.py & #命令后加&字符,不至于终端关闭后即停止运行,可转入后台一直运行。
test.py下载链接:https://pan.baidu.com/s/1i4IcfkWKkuGdRaq9Ws3Iqg 提取码:ujwg
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import urllib2
import threading
import re
import time
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
def ipvs1():
url = urllib2.urlopen("http://txt.go.sohu.com/ip/soip")
text = url.read()
global ip
ip = re.findall(r'\d+.\d+.\d+.\d+',text)
global ips1
ips1=ip
def ipvs2():
url = urllib2.urlopen("http://txt.go.sohu.com/ip/soip")
text = url.read()
global ip
ip = re.findall(r'\d+.\d+.\d+.\d+',text)
global ips2
ips2=ip
a=1
while a>0:
ipvs1()#给ips1赋值为当前ip
time.sleep(30.0)#等待30秒
ipvs2()#给ips2赋值为30秒后的Ip
if ips1!=ips2:#比对两个时间下IP地址的值,如不相同则执行以下发送邮件的脚本。
ipn=str(ips2)#将最新Ip地址转化为字符串,以便后面做为邮件正文
sender_mail = 'zxxxxxxxxx@126.com'
sender_pass = 'JULVNFGMTLUKAKBF'#126邮箱客户端授权码,在网页版邮箱设置-更多设置-POP3/SMTP……中获取
to = 'zxxxxxxxxx@126.com'
# 设置总的邮件体对象,对象类型为mixed
msg_root = MIMEMultipart('mixed')
# 邮件添加的头尾信息等
msg_root['From'] = 'zxxxxxxxxx@126.com<zxxxxxxxxx@126.com>'
msg_root['To'] = to
# 邮件的主题,显示在接收邮件的预览页面
subject = '最新COREELEC公网IP地址'
msg_root['subject'] = Header(subject, 'utf-8')
# 构造文本内容
text_info = ipn
text_sub = MIMEText(text_info, 'plain', 'utf-8')
msg_root.attach(text_sub)
sftp_obj =smtplib.SMTP('smtp.126.com', 25)
sftp_obj.login(sender_mail, sender_pass)
sftp_obj.sendmail(sender_mail, to, msg_root.as_string())
sftp_obj.quit()
print('sendemail successful!')
else:
#print("next while loop")
continue
下一步研究一下如何做成系统服务,开机自启动。
另存为test.py文件后,在coreelec的ssh远程终端中使用linux命令:
python /var/media……/test.py & #命令后加&字符,不至于终端关闭后即停止运行,可转入后台一直运行。
test.py下载链接:https://pan.baidu.com/s/1i4IcfkWKkuGdRaq9Ws3Iqg 提取码:ujwg
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import urllib2
import threading
import re
import time
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
def ipvs1():
url = urllib2.urlopen("http://txt.go.sohu.com/ip/soip")
text = url.read()
global ip
ip = re.findall(r'\d+.\d+.\d+.\d+',text)
global ips1
ips1=ip
def ipvs2():
url = urllib2.urlopen("http://txt.go.sohu.com/ip/soip")
text = url.read()
global ip
ip = re.findall(r'\d+.\d+.\d+.\d+',text)
global ips2
ips2=ip
a=1
while a>0:
ipvs1()#给ips1赋值为当前ip
time.sleep(30.0)#等待30秒
ipvs2()#给ips2赋值为30秒后的Ip
if ips1!=ips2:#比对两个时间下IP地址的值,如不相同则执行以下发送邮件的脚本。
ipn=str(ips2)#将最新Ip地址转化为字符串,以便后面做为邮件正文
sender_mail = 'zxxxxxxxxx@126.com'
sender_pass = 'JULVNFGMTLUKAKBF'#126邮箱客户端授权码,在网页版邮箱设置-更多设置-POP3/SMTP……中获取
to = 'zxxxxxxxxx@126.com'
# 设置总的邮件体对象,对象类型为mixed
msg_root = MIMEMultipart('mixed')
# 邮件添加的头尾信息等
msg_root['From'] = 'zxxxxxxxxx@126.com<zxxxxxxxxx@126.com>'
msg_root['To'] = to
# 邮件的主题,显示在接收邮件的预览页面
subject = '最新COREELEC公网IP地址'
msg_root['subject'] = Header(subject, 'utf-8')
# 构造文本内容
text_info = ipn
text_sub = MIMEText(text_info, 'plain', 'utf-8')
msg_root.attach(text_sub)
sftp_obj =smtplib.SMTP('smtp.126.com', 25)
sftp_obj.login(sender_mail, sender_pass)
sftp_obj.sendmail(sender_mail, to, msg_root.as_string())
sftp_obj.quit()
print('sendemail successful!')
else:
#print("next while loop")
continue
下一步研究一下如何做成系统服务,开机自启动。

