[移动网络]转发SMS消息到微信和Telegram

发布于 2019-07-23  21 次阅读


需求

· 多卡短信检测

· 转发给微信和Telegram

· 24效时稳定在线

硬件要求

· Huawei E180卡托 (已经停产了,我打算多买几个屯着)

· Linux机器(我用的是我自己的虚拟化平台,可以用树莓派)

下载&解压

wget http://smstools3.kekekasvi.com/packages/smstools3-3.1.21.tar.gz
tar zxvf smstools3-3.1.21.tar.gz 

编译 & 安装

依赖

yum groupinstall "Development Tools"
yum install gcc-c++

编译

cd smstools3/
make
make install

配置文件

/etc/smsd.conf

devices = GSM卡1手机号,GSM卡2手机号

logfile = /var/log/smsd.log
loglevel = 7

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
delaytime = 1
eventhandler = /etc/pushsms

[GSM卡1手机号]
init = AT+CPMS="ME","ME","ME"
check_memory_method = 2
device = /dev/ttyUSB0
incoming = yes
baudrate = 115200

[GSM卡2手机号]
init = AT+CPMS="ME","ME","ME"
check_memory_method = 2
device = /dev/ttyUSB3
incoming = yes
baudrate = 115200

PS: /dev/ttyUSB0/dev/ttyUSB3是卡托的AT指令端口

/etc/pushsms

#!/bin/sh
if [ "$1" == "RECEIVED" ]; then
  FROM=$(grep -a "From:" $2 | awk -F ': ' '{printf $2}')
  SENT=$(grep -a "Sent:" $2 | awk -F ': ' '{printf $2}')
  RECEIVED=$(grep -a "Received:" $2 | awk -F ': ' '{printf $2}')
  Alphabet=$(grep -a "Alphabet:" $2 | awk -F ': ' '{printf $2}')

  if [ "$Alphabet" == "UCS2" ]; then
    Content=$(sed -e '1,/^$/ d' < "$2" | iconv -f UNICODEBIG -t UTF-8)
  else
    Content=$(sed -e '1,/^$/ d' < "$2")
  fi

  TEXT=$(cat <<EOF
发件人: ${FROM}
发件时间: ${SENT}
收件时间: ${RECEIVED}
${Content}
EOF)
  curl -d "text=$FROM&desp=$TEXT" -X POST http://sc.ftqq.com/[填写自己的AUTHKEY].send
  curl -X POST "https://api.telegram.org/bot[Token]/sendMessage" -d "chat_id=[YourChatID]&text=$FROM%0a$TEXT"
fi

PS:记得给权限 chmod +x /etc/pushsms

/etc/systemd/system/smsd.service

[Unit]
Description=SMS server
After=network.target
Wants=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/smsd
ExecStop=/bin/pkill smsd
ExecReload=/bin/pkill smsd
KillMode=process
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

PS:记得加启动项systemctl enable smsd