eliminate a lot of copypasta by using functions

This commit is contained in:
Evgeny Zinoviev 2021-03-22 23:25:34 +03:00
parent c8fb7cd398
commit 91b6b387e1
11 changed files with 54 additions and 85 deletions

View File

@ -4,11 +4,5 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
get_token
curl -s -X POST "http://$MODEM_IP/api/monitoring/clear-traffic" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<request><ClearTraffic>1</ClearTraffic></request>" >modem_status.xml
cat modem_status.xml
post_xml "monitoring/clear-traffic" \
"<request><ClearTraffic>1</ClearTraffic></request>"

View File

@ -4,11 +4,5 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
get_token
curl -s -X POST "http://$MODEM_IP/api/dialup/mobile-dataswitch" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<request><dataswitch>0</dataswitch></request>" >modem_status.xml
cat modem_status.xml
post_xml "dialup/mobile-dataswitch" \
"<request><dataswitch>0</dataswitch></request>"

View File

@ -4,11 +4,5 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
get_token
curl -s -X POST "http://$MODEM_IP/api/dialup/mobile-dataswitch" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<request><dataswitch>1</dataswitch></request>" >modem_status.xml
cat modem_status.xml
post_xml "dialup/mobile-dataswitch" \
"<request><dataswitch>1</dataswitch></request>"

View File

@ -4,10 +4,4 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
get_token
curl -s -X GET "http://$MODEM_IP/api/dhcp/settings" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" >modem_status.xml
cat modem_status.xml
get_xml "dhcp/settings"

View File

@ -5,16 +5,11 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
get_token
curl -s -X POST "http://$MODEM_IP/api/sms/sms-list" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<request><PageIndex>1</PageIndex><ReadCount>10</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" >modem_status.xml
post_xml "sms/sms-list" \
"<request><PageIndex>1</PageIndex><ReadCount>10</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" > $STATUS_FILE
#cat modem_status.xml
readarray -t array_phone <<<"$(xmlstarlet sel -t -m "//Phone" -v . -n modem_status.xml)"
readarray -t array_content <<<"$(xmlstarlet sel -t -m "//Content" -v . -n modem_status.xml)"
readarray -t array_phone <<<"$(xmlstarlet sel -t -m "//Phone" -v . -n $STATUS_FILE)"
readarray -t array_content <<<"$(xmlstarlet sel -t -m "//Content" -v . -n $STATUS_FILE)"
for ((i = 0; i < ${#array_content[@]}; i++)); do
echo -e "------\n${array_phone[$i]}"

View File

@ -4,15 +4,10 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
get_token
post_xml "sms/sms-list" "<request><PageIndex>1</PageIndex><ReadCount>10</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>"
curl -s -X POST "http://$MODEM_IP/api/sms/sms-list" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<request><PageIndex>1</PageIndex><ReadCount>10</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" >modem_status.xml
#cat $STATUS_FILE
#cat modem_status.xml
message=$(grep -r -E 'Phone|Content' modem_status.xml | sed -e 's/<[^>]*>//g' | sed -e 's/^[ \t]*/------\n/g')
message=$(grep -r -E 'Phone|Content' $STATUS_FILE | sed -e 's/<[^>]*>//g' | sed -e 's/^[ \t]*/------\n/g')
echo "$message\n------"

View File

@ -5,8 +5,5 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
get_token
curl -s -X POST "http://$MODEM_IP/api/device/control" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<?xml version='1.0' encoding='UTF-8'?><request><Control>1</Control></request>"
post_xml "device/control" \
"<?xml version='1.0' encoding='UTF-8'?><request><Control>1</Control></request>"

View File

@ -3,12 +3,9 @@
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
SMS_DATA="<?xml version='1.0' encoding='UTF-8'?><request><Index>-1</Index><Phones><Phone>363</Phone></Phones><Sca></Sca><Content>USAGE</Content><Length>5</Length><Reserved>1</Reserved><Date>-1</Date></request>"
get_token
post_xml "sms/send-sms" "$SMS_DATA" > $STATUS_FILE
curl -s -X POST "http://$MODEM_IP/api/sms/send-sms" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<?xml version='1.0' encoding='UTF-8'?><request><Index>-1</Index><Phones><Phone>363</Phone></Phones><Sca></Sca><Content>USAGE</Content><Length>5</Length><Reserved>1</Reserved><Date>-1</Date></request>" >modem_status.xml
cat modem_status.xml
cat $STATUS_FILE

View File

@ -4,18 +4,14 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
get_token
get_xml "monitoring/traffic-statistics" > $STATUS_FILE
curl -s -X GET "http://$MODEM_IP/api/monitoring/traffic-statistics" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" > modem_status.xml
CurConnTime=$(cat modem_status.xml | grep CurrentConnectTime | sed -e 's/<[^>]*>//g')
CurrUpload=$(cat modem_status.xml | grep "<CurrentUpload>" | sed -e 's/<[^>]*>//g')
CurrDownload=$(cat modem_status.xml | grep "<CurrentDownload>" | sed -e 's/<[^>]*>//g')
TotalUpload=$(cat modem_status.xml | grep "<TotalUpload>" | sed -e 's/<[^>]*>//g')
TotalDownload=$(cat modem_status.xml | grep "<TotalDownload>" | sed -e 's/<[^>]*>//g')
TotalConnectTime=$(cat modem_status.xml | grep "<TotalConnectTime>" | sed -e 's/<[^>]*>//g')
CurConnTime=$(cat $STATUS_FILE | grep CurrentConnectTime | sed -e 's/<[^>]*>//g')
CurrUpload=$(cat $STATUS_FILE | grep "<CurrentUpload>" | sed -e 's/<[^>]*>//g')
CurrDownload=$(cat $STATUS_FILE | grep "<CurrentDownload>" | sed -e 's/<[^>]*>//g')
TotalUpload=$(cat $STATUS_FILE | grep "<TotalUpload>" | sed -e 's/<[^>]*>//g')
TotalDownload=$(cat $STATUS_FILE | grep "<TotalDownload>" | sed -e 's/<[^>]*>//g')
TotalConnectTime=$(cat $STATUS_FILE | grep "<TotalConnectTime>" | sed -e 's/<[^>]*>//g')
#------------------------------
# Current Connect Time
@ -29,7 +25,7 @@ printf 'Current Connect Time : %d days: %02d hours: %02d minutes: %02d sseconds\
tct_secs=$TotalConnectTime
printf 'Total Connect Time : %d days: %02d hours: %02d minutes: %02d sseconds\n' $((tct_secs / 86400)) $((tct_secs % 86400 / 3600)) $((tct_secs % 3600 / 60)) $((tct_secs % 60))
#cat modem_status.xml
#cat $STATUS_FILE
#------------------------------
# Current Upload
#------------------------------
@ -44,7 +40,7 @@ else
fi
#------------------------------
# Current Downloae
# Current Download
#------------------------------
if [ $CurrDownload -lt 1024 ]; then
echo "Current Download : ${CurrDownload}B"

View File

@ -4,19 +4,11 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh"
get_token
get_xml "device/information" > $STATUS_FILE
get_xml "device/signal" >> $SIGNAL_FILE
curl -s -X GET "http://$MODEM_IP/api/device/information" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" >modem_status.xml
curl -s -X GET "http://$MODEM_IP/api/device/signal" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" >>modem_status.xml
wmode=$(cat modem_status.xml | grep workmode | sed -e 's/<[^>]*>//g')
rssi=$(cat modem_status.xml | grep rssi | sed -e 's/<[^>]*>//g')
wmode=$(cat $STATUS_FILE | grep workmode | sed -e 's/<[^>]*>//g')
rssi=$(cat $STATUS_FILE | grep rssi | sed -e 's/<[^>]*>//g')
echo "mode: $wmode"
echo "signal: $rssi"

View File

@ -2,10 +2,31 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
[ -z "$MODEM_IP" ] && MODEM_IP="192.168.9.1"
STATUS_FILE="modem_status.xml"
get_token() {
curl -s -X GET "http://$MODEM_IP/api/webserver/SesTokInfo" > ses_tok.xml
COOKIE=$(grep "SessionID=" ses_tok.xml | cut -b 10-147)
TOKEN=$(grep "TokInfo" ses_tok.xml | cut -b 10-41`)
rm ses_tok.xml
}
get_xml() {
local endpoint="$1"
curl -s -X GET "http://$MODEM_IP/api/$endpoint" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml"
}
post_xml() {
local endpoint="$1"
local data="$2"
curl -s -X POST "http://$MODEM_IP/api/$endpoint" \
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "$data"
}