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" . "$DIR/include.sh"
get_token get_token
post_xml "monitoring/clear-traffic" \
curl -s -X POST "http://$MODEM_IP/api/monitoring/clear-traffic" \ "<request><ClearTraffic>1</ClearTraffic></request>"
-H "Cookie: $COOKIE" \
-H "__RequestVerificationToken: $TOKEN" \
-H "Content-Type: text/xml" \
-d "<request><ClearTraffic>1</ClearTraffic></request>" >modem_status.xml
cat modem_status.xml

View File

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

View File

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

View File

@ -4,10 +4,4 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh" . "$DIR/include.sh"
get_token get_token
get_xml "dhcp/settings"
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

View File

@ -5,16 +5,11 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
get_token get_token
curl -s -X POST "http://$MODEM_IP/api/sms/sms-list" \ post_xml "sms/sms-list" \
-H "Cookie: $COOKIE" \ "<request><PageIndex>1</PageIndex><ReadCount>10</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>" > $STATUS_FILE
-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 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)"
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)"
for ((i = 0; i < ${#array_content[@]}; i++)); do for ((i = 0; i < ${#array_content[@]}; i++)); do
echo -e "------\n${array_phone[$i]}" echo -e "------\n${array_phone[$i]}"

View File

@ -4,15 +4,10 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
. "$DIR/include.sh" . "$DIR/include.sh"
get_token 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" \ #cat $STATUS_FILE
-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 modem_status.xml message=$(grep -r -E 'Phone|Content' $STATUS_FILE | sed -e 's/<[^>]*>//g' | sed -e 's/^[ \t]*/------\n/g')
message=$(grep -r -E 'Phone|Content' modem_status.xml | sed -e 's/<[^>]*>//g' | sed -e 's/^[ \t]*/------\n/g')
echo "$message\n------" echo "$message\n------"

View File

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

View File

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

View File

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

View File

@ -2,6 +2,7 @@
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
[ -z "$MODEM_IP" ] && MODEM_IP="192.168.9.1" [ -z "$MODEM_IP" ] && MODEM_IP="192.168.9.1"
STATUS_FILE="modem_status.xml"
get_token() { get_token() {
curl -s -X GET "http://$MODEM_IP/api/webserver/SesTokInfo" > ses_tok.xml curl -s -X GET "http://$MODEM_IP/api/webserver/SesTokInfo" > ses_tok.xml
@ -9,3 +10,23 @@ get_token() {
TOKEN=$(grep "TokInfo" ses_tok.xml | cut -b 10-41`) TOKEN=$(grep "TokInfo" ses_tok.xml | cut -b 10-41`)
rm ses_tok.xml 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"
}