我很长时间在互联网上搜索如何在基于debian的linux系统上通过命令行发送短信与华为E3131和HiLink。所有这些都没有奏效。看来,该软件有更新。
HiLink向我展示了以下版本:
以下问题:如何使用E3131在基于debian的linux系统上通过命令行发送/接收短信?
There is a follow up question for setting up the hardware on a headless system on superuser
答案 0 :(得分:11)
需要3个步骤:
第1步 - 获取会话ID
为了获取会话ID,我在自己的shell脚本中使用以下命令:
#!/bin/bash
curl -b session.txt -c session.txt http://192.168.8.1/html/index.html > /dev/null 2>&1
第2步 - 获取令牌
为了获取令牌,我还使用以下命令,也在自己的shell脚本中:
#!/bin/bash
TOKEN=$(curl -s -b session.txt -c session.txt http://192.168.8.1/html/smsinbox.html)
TOKEN=$(echo $TOKEN | cut -d'"' -f 10)
echo $TOKEN > token.txt
第3步A部分 - 发送短信
最后是第三个用于发送短信的shell脚本,它还调用另外两个脚本:
#!/bin/bash
NUMBER=$1
MESSAGE=$2
./session.sh
./token.sh
LENGTH=${#MESSAGE}
TIME=$(date +"%Y-%m-%d %T")
TOKEN=$(<token.txt)
SMS="<request><Index>-1</Index><Phones><Phone>$NUMBER</Phone></Phones><Sca/><Content>$MESSAGE</Content><Length>$LENGTH</Length><Reserved>1</Reserved><Date>$TIME</Date></request>"
echo $SMS
curl -v -b session.txt -c session.txt -H "X-Requested-With: XMLHttpRequest" --data "$SMS" http://192.168.8.1/api/sms/send-sms --header "__RequestVerificationToken: $TOKEN" --header "Content-Type:text/xml"
用法是:
command phonenumber "text"
第3步B部分 - 接收短信
为了接收最后一个未读短信(或者,如果不可用,最后读取的短信)我使用以下脚本:
#!/bin/bash
./session.sh
./token.sh
TOKEN=$(<token.txt)
DATA="<request><PageIndex>1</PageIndex><ReadCount>1</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>"
curl -b session.txt -c session.txt -H "X-Requested-With: XMLHttpRequest" --data "$DATA" http://192.168.8.1/api/sms/sms-list --header "__RequestVerificationToken: $TOKEN" --header "Content-Type:text/xml"
这可能不是很好的编码,但它有效。
答案 1 :(得分:1)
尽管Peter已经很好地解释了它,但我喜欢使用单个脚本,而且我在OpenWrt路由器而不是Debian上使用它。
所以这是我发送短信的版本:
#!/bin/sh
RESPONSE=`curl -s -X GET http://192.168.8.1/api/webserver/SesTokInfo`
COOKIE=`echo "$RESPONSE"| grep SessionID=| cut -b 10-147`
TOKEN=`echo "$RESPONSE"| grep TokInfo| cut -b 10-41`
NUMBER=$1
SMS=$2
DATA="<?xml version='1.0' encoding='UTF-8'?><request><Index>-1</Index><Phones><Phone>$NUMBER</Phone></Phones><Sca></Sca><Content>$SMS</Content><Length>11</Length><Reserved>1</Reserved><Date>-1</Date></request>"
curl -v http://192.168.8.1/api/sms/send-sms \
-H "Cookie: $COOKIE" -H "__RequestVerificationToken: $TOKEN" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \
--data $DATA
以下是阅读最后三条短信的脚本:
#!/bin/sh
RESPONSE=`curl -s -X GET http://192.168.8.1/api/webserver/SesTokInfo`
COOKIE=`echo "$RESPONSE"| grep SessionID=| cut -b 10-147`
TOKEN=`echo "$RESPONSE"| grep TokInfo| cut -b 10-41`
DATA="<request><PageIndex>1</PageIndex><ReadCount>3</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>"
curl -b $COOKIE -c $COOKIE -H "X-Requested-With: XMLHttpRequest" --data "$DATA" http://192.168.8.1/api/sms/sms-list --header "__RequestVerificationToken: $TOKEN" --header "Content-Type:text/xml"
答案 2 :(得分:1)
我为我的HUAWEI E3276制作了python脚本:
import requests, sys
import xml.etree.ElementTree as ET
msg = "From python"
phone = "PHONE_NUMBER" #To fill
ip = "192.168.1.1" #Dongle ip
#Get token
r = requests.get("http://%s/api/webserver/token" % ip)
root = ET.fromstring(r.content)
token = root[0].text
print "token", token
#Send sms
headers = { "__RequestVerificationToken": token, "Content-Type": "text/xml" }
data = "<request><Index>-1</Index><Phones><Phone>%s</Phone></Phones><Sca/><Content>%s</Content><Length>%d</Length><Reserved>1</Reserved><Date>$TIME</Date></request>" % ( phone, msg, len(msg) )
r = requests.post( "http://%s/api/sms/send-sms" % ip, data=data, headers=headers )
print "send-sms", r.headers, r.content