具体型号我就不打了。某二手交易平台下一大堆没人用,其实原理都是一样的 ,无非是对模块的操作,我们找出指令就好了,此卡托用的e随行软件看看他的日志:
无非就是AT指令操作模块、按着r软件执行的指令进行模拟网络注册,模块模式设定(由于代码渣渣就不献丑了,而且还需要PDU短信的解码,总是有写编码类的问题),后来发现py有直接对GSM操作的库,把成品代码奉上:
[td]
#!encoding:UTF-8
from email.mime.text import MIMEText
from email.utils import formataddr
import smtplib
from gsmmodem.modem import GsmModem
import logging
import re
import serial
import serial.tools.list_ports
import time
def find_port():
port = ''
plist = list(serial.tools.list_ports.comports())
# print(plist)
if len(plist) <= 0:
print u"没有发现端口!"
else:
port_list = []
for i in range(len(plist)):
plist_0 = list(plist)
# print plist_0
serialName = plist_0
if serialName.find('(') > -1 and serialName.find(')') > -1:
a = serialName.find('(')
b = serialName.find(')')
serialName = serialName[a + 1:b]
serialFd = serial.Serial(serialName, 115200, timeout=60)
port = serialFd.name
print u'发现可用端口:', port
port_list.append(port)
return port_list
def handleSms(sms):
message = u'== SMS message received ==\nFrom: {0}\nTime: {1}\nMessage:\n{2}\n'.format(sms.number, sms.time,sms.text)
print message
sms_str = sms.text.encode('UTF-8')
if send_email(sms_str, message) == False:
print u'邮件发送失败,采用短信方式回复!'
sms.reply(u'【短信】:"{0}{1}"'.format(sms.text[:20], '...' if len(sms.text) > 20 else ''))
print u'短信发送成功.\nWaitting SMS...'
def deal_vcode(text):
pp = r'[A-Za-z0-9]{4,}(?![A-Za-z0-9])'
matchObj = re.search(pp, text, re.M | re.I)
if matchObj:
print ("search --> matchObj.group() : ", matchObj.group())
Vcode = matchObj.group()
return Vcode
else:
print ("No match!!")
return False
def send_email(text, context):
mail_host = "smtp.qq.com" # QQ邮箱服务器
from_addr = "[email protected]" # 发件人电子邮箱
from_pwd = "**********" # SMTP授权码
to_addrs = '[email protected]' # 收件人电子邮箱
Vcode = ''
message = MIMEText(context, 'plain', 'utf-8')
message['From'] = formataddr(["【短信】", from_addr])
# 收件人的昵称
message['To'] = formataddr(["收件人", to_addrs])
re_res = deal_vcode(text)
if not re_res:
subject = "【未获取到验证码】:详细查看邮件"
else:
subject = "【短信--验证码】:" + re_res
# subject = "【未获取到验证码】:详细查看邮件"
message["Subject"] = subject
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)# 465 为SMTP端口号
smtpObj.set_debuglevel(1)
smtpObj.login(from_addr, from_pwd)
smtpObj.sendmail(from_addr, to_addrs, message.as_string())
smtpObj.quit()# 关闭连接
print u"邮件发送成功"
return True
except smtplib.SMTPException:
print u"Error:无法发送邮件,采取短信转发!"
return False
def main(PORT):
print 'Initializing modem...'
# Uncomment the following line to see what the modem is doing:
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
modem = GsmModem(PORT, BAUDRATE, smsReceivedCallbackFunc=handleSms)
modem.smsTextMode = False
modem.connect(PIN)
print 'Waiting for SMS message...'
try:
modem.rxThread.join(2 ** 31)
# Specify a (huge) timeout so that it essentially blocks indefinitely, but still receives CTRL+C interrupt signal
finally:
modem.close();
def check_port(port_list):
if len(port_list) > 1:
for i in port_list:
print(port_list.index(i) + 1, i)
print u'请选择使用的端口:'
select = input()
select_port = port_list[select - 1]
print u"已选择端口:", select_port
return select_port
elif len(port_list) == 1:
select_port = port_list[0]
print u"默认选择已识别端口:" + select_port
return select_port
else:
time.sleep(3)
check_port(port_list)
if __name__ == '__main__':
port = check_port(find_port())
# PORT = '/dev/ttyUSB2'
BAUDRATE = 115200
PIN = None # SIM card PIN (if any)
main(port)
插入电脑,识别端口后随便选择一个,就可以自动设置其模式,此代码中设置的模式为 收到短信显示不存储,因为sim卡的存储卡空间就那么大,一直用的话会堆满。
这东西还面临着几个安全性的问题:
1.我的是移动卡托,但是我用的是联通卡,只能2G。也就是面临被劫持的危险,还好我不发短信。3g。4g的网络就不用考虑了
2.邮箱发送,一但邮箱被盗,就坏了,接下来有空改成微信提醒的,
3.做一次改进远程开关机指令。不用的关机
这个模块没有电话功能 打电话什么也不提示,也不说关机也不说无法接通,啥都没有,放心留号码到各个注册平台
linux 平台需要改下端口、windows 直接用。我是放在公司里的电脑上、很可恶,我家里没有2G信号,模块无法注册网络。对比阿里小号来说可操作部分比较多,能够发短信解封之类的,反正各求所需吧。觐见。