2

使用 gnokii 读取 3G 网卡的短信

 2 years ago
source link: https://blog.lilydjwg.me/2012/3/18/use-gnokii-to-read-sms-from-3g-moden.32523.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用 gnokii 读取 3G 网卡的短信

本文来自依云's Blog,转载请注明。

使用 gnokii 读取 3G 网卡短信的方法ArchWiki上有写。安装 gnokii 后复制配置文件并将自己添加到uucp用户组中:

cp /etc/gnokiirc ~/.config/gnokii/config
sudo gpasswd -a `whoami` uucp

然后修改下配置文件,主要是port = /dev/ttyUSB0model = AT这两处。用户组的修改要下次登录时才生效,或者使用newgrp命令来登录到uucp组。据说此命令在某些 shell 里是内建命令,不过在 bash 和 zsh 里没有,只能调用外部命令,所以会开启一个新的 shell。

newgrp uucp

现在就可以读短信了。接收新信息并存储,可使用命令

gnokii --smsreader

完事之后按Ctrl-C中断。要把短信读出来,gnokii 可以把短信存储为 mbox 格式。这是一种邮件格式,使用 mutt 即可阅读。-f后边的sms即是要保存的文件名。注意,gnokii 仍会将消息输出到终端。ArchWiki 上说的是使用 xgnokii GUI 程序来读取,但是我没有找到这个程序(只有 manpage)。

gnokii --getsms SM 0 end -f sms

对于中文短信,这样会导致乱码。所以我写了个 Python 脚本来处理。三件事:一是将内容编码标识为 UTF-8,二是把按字节截断的邮件主题(短信正文的前若干个字节)最后几个无效的编码替换掉,三是将邮件主题按标准进行编码。这些事 Python 处理起来挺容易的 ;-)

smsmboxproc
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import sys
import email.header
for i in os.fdopen(sys.stdin.fileno(), encoding='utf-8', errors='replace'):
if i.startswith('Subject: '):
s = i[9:-1]
print('Content-Type: text/plain; charset=utf-8')
print('Subject:', email.header.Header(s, 'utf-8').encode())
else:
sys.stdout.write(i)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK