前段時(shí)間貌似是因?yàn)殡娦啪W(wǎng)絡(luò)維護(hù)停止了 CDMA 的本網(wǎng)漫游,需要漫游到 WCDMA韩脑。奈何短信機(jī)是臺(tái)紅米 1S氢妈,不支持 WCDMA,導(dǎo)致收不到各種驗(yàn)證碼段多。于是打算使用 3G 模塊接收短信首量,并轉(zhuǎn)發(fā)到 Telegram。
經(jīng)過(guò)一番調(diào)查后發(fā)現(xiàn):
- 日本早在 2012 年 5 月 31 日全面停止了 2G 網(wǎng)絡(luò)的服務(wù)进苍,于是 GSM 不可行加缘。
- 打電話給電信回復(fù)說(shuō):CDMA 維護(hù),恢復(fù)時(shí)間不定觉啊,請(qǐng)使用 WCDMA 網(wǎng)絡(luò)拣宏。這說(shuō)明異網(wǎng)漫游是正常的。
- DoCoMo 的 WCDMA 頻段是 Band 1杠人,6勋乾,9,19嗡善。
然后去日拍翻了下辑莫,剛好有一款符合要求:
標(biāo)準(zhǔn) AT 指令操作,支持收發(fā)短信罩引。
測(cè)試入網(wǎng)
正常入網(wǎng)各吨,接著測(cè)試 AT 指令
一切正常。
配置 LEDE
首先需要安裝 USB 驅(qū)動(dòng)
opkg install kmod-usb-serial kmod-usb-serial-wwan kmod-usb-serial-option usb-modeswitch
主要就是這些袁铐,可能還有其他需要安裝的我沒(méi)有列出來(lái)揭蜒,具體請(qǐng)參考 Use 3g/UMTS USB Dongle for WAN connection
之后就是配置短信服務(wù),這里用到的是 smstools3剔桨,所以
opkg install smstools3
接著需要配置 smstool3屉更,配置文件在 /etc/smsd.conf
,修改完之后應(yīng)該是這樣:
#
# Description: Main configuration file for the smsd
#
devices = GSM1
incoming = /var/spool/sms/incoming
outgoing = /var/spool/sms/outgoing
checked = /var/spool/sms/checked
failed = /var/spool/sms/failed
sent = /var/spool/sms/sent
receive_before_send = no
autosplit = 3
incoming_utf8 = yes
# 輪詢短信間隔時(shí)間
delaytime = 1
[GSM1]
# 將短信保存在上網(wǎng)卡中(更快洒缀,不支持的話請(qǐng)使用 SM 模式)
init = AT+CPMS="ME","ME","ME"
# 將短信保存在 USIM 卡中
# init = AT+CPMS="SM","SM","SM"
device = /dev/ttyUSB0
incoming = yes
# 如果 USIM 卡有設(shè)置 pin 碼需要設(shè)置這行來(lái)解鎖
# pin = 0000
# 波特率瑰谜,一般都是 115200,不對(duì)的話請(qǐng)自行修改
baudrate = 115200
# 如果檢測(cè)得到信息但是無(wú)法讀取需要修改這里的讀取模式
check_memory_method = 2
report_device_details = yes
signal_quality_ber_ignore = yes
check_network = no
如果你的上網(wǎng)卡的串口路徑不是 /dev/ttyUSB0
請(qǐng)查找后自行修改:
ls /dev | grep tty
...
ttyUSB0
ttyUSB1
ttyUSB2
...
啟動(dòng)服務(wù)
/etc/init.d/smstools3 restart
這樣一來(lái)就能正常收到短信了。
順便一提似舵,E173U 的指示燈:
- 閃綠燈:初始化正常,正在搜索網(wǎng)絡(luò)
- 閃藍(lán)燈:正常入網(wǎng)
不出意外的話葱峡,在配置好上網(wǎng)卡的軟件包后插上上網(wǎng)卡就能看到藍(lán)燈閃爍砚哗,代表正常入網(wǎng)。
測(cè)試 smstools
剛剛配置好了 smstools 但是不知道是否工作正常砰奕,于是查看日志:
2017-06-08 03:25:34,7, smsd: Done: startup_check (shell), execution time 0 sec., status: 0 (0)
2017-06-08 03:25:34,4, smsd: File mode creation mask: 022 (0644, rw-r--r--).
2017-06-08 03:25:34,5, smsd: Outgoing file checker has started. PID: 9684.
2017-06-08 03:25:34,7, smsd: All PID's: 9684,9686
2017-06-08 03:25:34,5, GSM1: Modem handler 0 has started. PID: 9686.
2017-06-08 03:25:34,5, GSM1: Using check_memory_method 1: CPMS is used.
2017-06-08 03:25:34,6, GSM1: Checking device for incoming SMS
2017-06-08 03:25:34,6, GSM1: Checking if modem is ready
2017-06-08 03:25:34,7, GSM1: -> AT
2017-06-08 03:25:34,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:35,7, GSM1: <- OK
2017-06-08 03:25:35,6, GSM1: Pre-initializing modem
2017-06-08 03:25:35,7, GSM1: -> ATE0+CMEE=1;+CREG=2
2017-06-08 03:25:35,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:35,7, GSM1: <- OK
2017-06-08 03:25:35,6, GSM1: Checking if modem needs PIN
2017-06-08 03:25:35,7, GSM1: -> AT+CPIN?
2017-06-08 03:25:35,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:36,7, GSM1: <- +CPIN: READY OK
2017-06-08 03:25:36,6, GSM1: Initializing modem
2017-06-08 03:25:36,7, GSM1: -> AT^CURC=0
2017-06-08 03:25:36,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:36,7, GSM1: <- OK
2017-06-08 03:25:36,7, GSM1: -> AT+CSQ
2017-06-08 03:25:36,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:37,7, GSM1: <- +CSQ: 12,99 OK
2017-06-08 03:25:37,6, GSM1: Signal Strength Indicator: (12,99) -89 dBm (Workable)
2017-06-08 03:25:37,6, GSM1: Checking if Modem is registered to the network
2017-06-08 03:25:37,7, GSM1: -> AT+CREG?
2017-06-08 03:25:37,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:37,7, GSM1: <- +CREG: 2,5, 66, C018 OK
2017-06-08 03:25:37,6, GSM1: Modem is registered to a roaming partner network
2017-06-08 03:25:37,6, GSM1: Location area code: 66, Cell ID: C018
2017-06-08 03:25:37,7, GSM1: -> AT+CSQ
2017-06-08 03:25:37,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:38,7, GSM1: <- +CSQ: 13,99 OK
2017-06-08 03:25:38,6, GSM1: Signal Strength Indicator: (13,99) -87 dBm (Workable)
2017-06-08 03:25:38,6, GSM1: Selecting PDU mode
2017-06-08 03:25:38,7, GSM1: -> AT+CMGF=0
2017-06-08 03:25:38,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:38,7, GSM1: <- OK
2017-06-08 03:25:38,7, GSM1: -> AT+CIMI
2017-06-08 03:25:38,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:39,7, GSM1: <- 204046003816003 OK
2017-06-08 03:25:39,7, GSM1: -> AT+CGSN
2017-06-08 03:25:39,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:39,7, GSM1: <- 352216049835909 OK
2017-06-08 03:25:39,5, GSM1: CGSN: 352216049835909
2017-06-08 03:25:39,5, GSM1: IMSI: 204046003816003
2017-06-08 03:25:39,6, GSM1: Checking if reading of messages is supported
2017-06-08 03:25:39,7, GSM1: -> AT+CPMS?
2017-06-08 03:25:39,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:25:40,7, GSM1: <- +CPMS: "SM",1,40,"SM",1,40,"SM",1,40 OK
2017-06-08 03:25:40,7, GSM1: ## Start of device details
2017-06-08 03:25:40,7, GSM1: # Manufacturer identification:
2017-06-08 03:25:40,7, GSM1: -> AT+CGMI
2017-06-08 03:25:40,7, GSM1: <- huawei
OK
2017-06-08 03:25:40,7, GSM1: # Model identification:
2017-06-08 03:25:40,7, GSM1: -> AT+CGMM
2017-06-08 03:25:41,7, GSM1: <- E173
OK
2017-06-08 03:25:41,7, GSM1: # Revision identification:
2017-06-08 03:25:41,7, GSM1: -> AT+CGMR
2017-06-08 03:25:41,7, GSM1: <- 11.126.83.00.00
OK
2017-06-08 03:25:41,7, GSM1: # New message indications, list of supported modes:
2017-06-08 03:25:41,7, GSM1: -> AT+CNMI=?
2017-06-08 03:25:42,7, GSM1: <- +CNMI: (0,1,2),(0,1,2,3),(0,2),(0,1,2),(0,1)
OK
2017-06-08 03:25:42,7, GSM1: # New message indications, current settings:
2017-06-08 03:25:42,7, GSM1: -> AT+CNMI?
2017-06-08 03:25:42,7, GSM1: <- +CNMI: 0,0,0,0,0
OK
2017-06-08 03:25:42,7, GSM1: # Preferred message storage, list of supported mem's:
2017-06-08 03:25:42,7, GSM1: -> AT+CPMS=?
2017-06-08 03:25:43,7, GSM1: <- +CPMS: ("ME","MT","SM","SR"),("ME","MT","SM","SR"),("ME","MT","SM","SR")
OK
2017-06-08 03:25:43,7, GSM1: # Phonebook storage, available mem's:
2017-06-08 03:25:43,7, GSM1: -> AT+CPBS=?
2017-06-08 03:25:43,7, GSM1: <- +CPBS: ("SM","EN","ON")
OK
2017-06-08 03:25:43,7, GSM1: # List messages, list of supported stat's:
2017-06-08 03:25:43,7, GSM1: -> AT+CMGL=?
2017-06-08 03:25:44,7, GSM1: <- +CMGL: (0-4)
沒(méi)毛病蛛芥。
發(fā)個(gè)短信試試
2017-06-08 03:28:34,6, GSM1: Checking memory size
2017-06-08 03:28:34,7, GSM1: -> AT+CPMS?
2017-06-08 03:28:34,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:28:34,7, GSM1: <- +CPMS: "SM",2,40,"SM",2,40,"SM",2,40 OK
2017-06-08 03:28:34,6, GSM1: Used memory is 2 of 40
2017-06-08 03:28:34,6, GSM1: Trying to get stored message 1
2017-06-08 03:28:34,7, GSM1: -> AT+CMGR=1
2017-06-08 03:28:34,7, GSM1: Command is sent, waiting for the answer
2017-06-08 03:28:35,7, GSM1: <- +CMGR: 0,,146 07911356044902000405A15955F5000871608020820023829A8C8BC17801003300370034003000340039FF0C60A86B63572875284E007F51901A767B5F55FF0C5982975E672C4EBA64CD4F5CFF0C8BF780547CFB0039003500350035003530028BF752FF57284EFB4F5577ED4FE1621690AE4EF694FE63A57684987597624E2D8F9351659A8C8BC17801FF010020005B62DB554694F6884C005D OK
2017-06-08 03:28:35,5, GSM1: SMS received, From: 95555
2017-06-08 03:28:35,6, GSM1: Wrote an incoming message file: /var/spool/sms/incoming/GSM1.IgkBhN
2017-06-08 03:28:35,7, GSM1: Running eventhandler: /root/sms RECEIVED /var/spool/sms/incoming/GSM1.IgkBhN
2017-06-08 03:28:36,7, GSM1: Done: eventhandler, execution time 1 sec., status: 0 (0)
沒(méi)毛病。
查看 incoming 文件夾中的文件 /var/spool/sms/incoming/GSM1.IgkBhN
From: 95555
From_TOA: A1 national, ISDN/telephone
From_SMSC: 316540942000
Sent: 17-06-08 02:28:00
Received: 17-06-08 03:28:35
Subject: GSM1
Modem: GSM1
IMSI: 204046003816003
Report: no
Alphabet: UCS2
Length: 65
????xxxxxxx?
`?kcW(u(Q?v{_U?
Y??^g,N?d?O\?
??T|?955550??R?W(N?OUw?O?b??N??c?v??u?bN-??Qe????x? [b?UF??L]
這里的正文顯示為亂碼军援。上面也有提到 Alphabet: UCS2
仅淑,于是使用 iconv
轉(zhuǎn)換。
opkg install iconv
這里需要注意的是胸哥,需要去掉前面的 ASCII 部分涯竟,不然會(huì)變成亂碼。
sed -i '1,12d' /var/spool/sms/incoming/GSM1.IgkBhN
iconv -f UTF-16BE -t UTF-8 < /var/spool/sms/incoming/GSM1.IgkBhN
驗(yàn)證碼xxxxxx空厌,您正在用一網(wǎng)通登錄庐船,如非本人操作,請(qǐng)聯(lián)系95555嘲更。請(qǐng)勿在任何短信或郵件鏈接的頁(yè)面中輸入驗(yàn)證碼筐钟! [招商銀行]
于是我們就得到了完整的短信。
轉(zhuǎn)發(fā)短信
因?yàn)槿粘J褂?Telegram 比較多赋朦,自然就想到了 Telegram Bot篓冲。
找 BotFather 要一個(gè) Bot
找 @get_id_bot 這個(gè) Bot 拿到自己的 chat id
然后我們需要一個(gè) hook,在收到短信的時(shí)候執(zhí)行 shell宠哄,所幸 smstools3 有這樣的 hook 給我們直接用壹将。
編輯 /etc/smsd.conf
在 [GSM1]
的末尾加上:
eventhandler = /root/sms
這樣在收到短信時(shí)就會(huì)調(diào)用這個(gè) shell 并傳遞兩個(gè)參數(shù):
- 事件類型,比如收到短信是
RECIVED
- 短信存檔路徑
于是我們的 /root/sms
如下:
#!/bin/sh
TMPFILE1=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 8)
TMPFILE2=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 8)
cat $2 > $TMPFILE2
awk 'NR==1 || NR==4 || NR==5' $TMPFILE2 >> $TMPFILE1
sed -i '1,12d' $TMPFILE2
iconv -f UTF-16BE -t UTF-8 < $TMPFILE2 >> $TMPFILE1
CONTENT=`cat $TMPFILE1`
echo SMSD alarm: $1 $2 >> /root/sms.txt
curl -i -X POST \
-F "chat_id=<chat_id>" \
-F "text=${CONTENT}" \
"https://api.telegram.org/bot<apikey>/sendMessage"
rm $TMPFILE1 $TMPFILE2
記得加上執(zhí)行權(quán)限
chmod +x /root/sms
其中 chat_id
為你自己的 chat id琳拨,并替換網(wǎng)址中的 apikey瞭恰。
最后記得在 Telegram 中找到你的 Bot 發(fā)送 /start
不然 Bot 沒(méi)有權(quán)限給你發(fā)送消息。
升級(jí) curl
測(cè)試腳本后發(fā)現(xiàn)并不能正常運(yùn)行狱庇,原因是 LEDE 17.01.1 的 curl 有一些 bug 尚未修復(fù)惊畏。
于是我們需要升級(jí) curl 和 libcurl 到 snapshot 的版本。
https://downloads.lede-project.org/snapshots/packages/
在這里找到路由器對(duì)應(yīng)的指令集密任,進(jìn)入 base 文件夾下載 curl 和 libcurl 兩個(gè)軟件包颜启。
比如 ar71xx 的是這兩個(gè):
curl
libcurl
使用 opkg 安裝:
wget https://downloads.lede-project.org/snapshots/packages/mips_24kc/base/curl_7.54.0-1_mips_24kc.ipk
wget https://downloads.lede-project.org/snapshots/packages/mips_24kc/base/libcurl_7.54.0-1_mips_24kc.ipk
opkg remove curl
opkg remove libcurl
opkg install libcurl_7.54.0-1_mips_24kc.ipk
opkg install curl_7.54.0-1_mips_24kc.ipk
這樣 curl 就能正常工作了。
測(cè)試 Bot
重啟 smstools3
/etc/init.d/smstools3 restart
發(fā)送短信并查看日志
2017-06-08 04:35:01,6, GSM1: Checking memory size
2017-06-08 04:35:02,7, GSM1: -> AT+CPMS?
2017-06-08 04:35:02,7, GSM1: Command is sent, waiting for the answer
2017-06-08 04:35:02,7, GSM1: <- +CPMS: "SM",2,40,"SM",2,40,"SM",2,40 OK
2017-06-08 04:35:02,6, GSM1: Used memory is 2 of 40
2017-06-08 04:35:02,6, GSM1: Trying to get stored message 1
2017-06-08 04:35:02,7, GSM1: -> AT+CMGR=1
2017-06-08 04:35:02,7, GSM1: Command is sent, waiting for the answer
2017-06-08 04:35:02,7, GSM1: <- +CMGR: 0,,146 07911356044902000405A15955F5000871608030434423829A8C8BC17801003200300035003600380037FF0C60A86B63572875284E007F51901A767B5F55FF0C5982975E672C4EBA64CD4F5CFF0C8BF780547CFB0039003500350035003530028BF752FF57284EFB4F5577ED4FE1621690AE4EF694FE63A57684987597624E2D8F9351659A8C8BC17801FF010020005B62DB554694F6884C005D OK
2017-06-08 04:35:02,5, GSM1: SMS received, From: 95555
2017-06-08 04:35:02,6, GSM1: Wrote an incoming message file: /var/spool/sms/incoming/GSM1.NHChff
2017-06-08 04:35:02,7, GSM1: Running eventhandler: /root/sms RECEIVED /var/spool/sms/incoming/GSM1.NHChff
2017-06-08 04:35:04,7, GSM1: Done: eventhandler, execution time 2 sec., status: 0 (0)
2017-06-08 04:35:04,3, GSM1: Exec: eventhandler said something:
2017-06-08 04:35:04,3, GSM1: ! HTTP/1.1 100 Continue
2017-06-08 04:35:04,3, GSM1: !
2017-06-08 04:35:04,3, GSM1: ! HTTP/1.1 200 OK
2017-06-08 04:35:04,3, GSM1: ! Server: nginx/1.10.0
2017-06-08 04:35:04,3, GSM1: ! Date: Wed, 07 Jun 2017 19:35:04 GMT
2017-06-08 04:35:04,3, GSM1: ! Content-Type: application/json
2017-06-08 04:35:04,3, GSM1: ! Content-Length: 637
2017-06-08 04:35:04,3, GSM1: ! Connection: keep-alive
2017-06-08 04:35:04,3, GSM1: ! Access-Control-Allow-Origin: *
2017-06-08 04:35:04,3, GSM1: ! Access-Control-Allow-Methods: GET, POST, OPTIONS
2017-06-08 04:35:04,3, GSM1: ! Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
2017-06-08 04:35:04,3, GSM1: ! Strict-Transport-Security: max-age=31536000; includeSubdomains
2017-06-08 04:35:04,3, GSM1: !
2017-06-08 04:35:04,3, GSM1: ! {"ok":true,"result":{"message_id":15,"from":{"id":362363438,"first_name":"SMS Forward Bot","username":"sms_forward_bot"},"chat":{"id":70962251,"first_name":"Jesse","last_name":"Zhu","username":"ITJesse","type":"private"},"date":1496864104,"text":"From: 95555\nSent: 17-06-08 03:34:44\nReceived: 17-06-08 04:35:02\n\u9a8c\u8bc1\u7801205687\uff0c\u60a8\u6b63\u5728\u7528\u4e00\u7f51\u901a\u767b\u5f55\uff0c\u5982\u975e\u672c\u4eba\u64cd\u4f5c\uff0c\u8bf7\u8054\u7cfb95555\u3002\u8bf7\u52ff\u5728\u4efb\u4f55\u77ed\u4fe1\u6216\u90ae\u4ef6\u94fe\u63a5\u7684\u9875\u9762\u4e2d\u8f93\u5165\u9a8c\u8bc1\u7801\uff01 [\u62db\u5546\u94f6\u884c]"}}
2017-06-08 04:35:04,3, GSM1: Exec: eventhandler encountered errors:
2017-06-08 04:35:04,3, GSM1: ! % Total % Received % Xferd Average Speed Time Time Time Current
2017-06-08 04:35:04,3, GSM1: ! Dload Upload Total Spent Left Speed
100 1111 100 637 100 474 337 251 0:00:01 0:00:01 --:--:-- 366
2017-06-08 04:35:04,6, GSM1: Deleting message 1
2017-06-08 04:35:05,7, GSM1: -> AT+CMGD=1
2017-06-08 04:35:05,7, GSM1: Command is sent, waiting for the answer
2017-06-08 04:35:05,7, GSM1: <- OK
并且 Telegram 收到推送
結(jié)語(yǔ)
最后用 1200 日元解決了這個(gè)世紀(jì)難題浪讳,不用背兩手機(jī)在包里缰盏,直接插路由器上也不用單獨(dú)供電。
雖然還能實(shí)現(xiàn)用 bot 發(fā)送短信,但是沒(méi)有這個(gè)需求就不繼續(xù)做了口猜。
然后立馬登陸網(wǎng)銀還了信用卡 :)