From 91b6b387e19ad2b2f686e8a27bfc0a027e9eb4d9 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Mon, 22 Mar 2021 23:25:34 +0300 Subject: eliminate a lot of copypasta by using functions --- e3372-clear-traffic-stats.sh | 10 ++-------- e3372-dataswitch-off.sh | 10 ++-------- e3372-dataswitch-on.sh | 10 ++-------- e3372-dhcp-setting.sh | 8 +------- e3372-readsms-xmlstarlet.sh | 13 ++++--------- e3372-readsms.sh | 11 +++-------- e3372-reboot.sh | 7 ++----- e3372-sendsms.sh | 11 ++++------- e3372-stats.sh | 22 +++++++++------------- e3372.sh | 16 ++++------------ include.sh | 21 +++++++++++++++++++++ 11 files changed, 54 insertions(+), 85 deletions(-) diff --git a/e3372-clear-traffic-stats.sh b/e3372-clear-traffic-stats.sh index 56b27b4..96409a5 100755 --- a/e3372-clear-traffic-stats.sh +++ b/e3372-clear-traffic-stats.sh @@ -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 "1" >modem_status.xml - -cat modem_status.xml +post_xml "monitoring/clear-traffic" \ + "1" diff --git a/e3372-dataswitch-off.sh b/e3372-dataswitch-off.sh index 83ae7e0..446708a 100755 --- a/e3372-dataswitch-off.sh +++ b/e3372-dataswitch-off.sh @@ -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 "0" >modem_status.xml - -cat modem_status.xml +post_xml "dialup/mobile-dataswitch" \ + "0" \ No newline at end of file diff --git a/e3372-dataswitch-on.sh b/e3372-dataswitch-on.sh index f270561..9662a7d 100755 --- a/e3372-dataswitch-on.sh +++ b/e3372-dataswitch-on.sh @@ -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 "1" >modem_status.xml - -cat modem_status.xml +post_xml "dialup/mobile-dataswitch" \ + "1" \ No newline at end of file diff --git a/e3372-dhcp-setting.sh b/e3372-dhcp-setting.sh index f76e6ae..c3af87b 100755 --- a/e3372-dhcp-setting.sh +++ b/e3372-dhcp-setting.sh @@ -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" diff --git a/e3372-readsms-xmlstarlet.sh b/e3372-readsms-xmlstarlet.sh index 17399a8..b8fc086 100755 --- a/e3372-readsms-xmlstarlet.sh +++ b/e3372-readsms-xmlstarlet.sh @@ -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 "1101001" >modem_status.xml +post_xml "sms/sms-list" \ + "1101001" > $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]}" diff --git a/e3372-readsms.sh b/e3372-readsms.sh index c265dfa..cf54c5f 100755 --- a/e3372-readsms.sh +++ b/e3372-readsms.sh @@ -4,15 +4,10 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" . "$DIR/include.sh" get_token +post_xml "sms/sms-list" "1101001" -curl -s -X POST "http://$MODEM_IP/api/sms/sms-list" \ - -H "Cookie: $COOKIE" \ - -H "__RequestVerificationToken: $TOKEN" \ - -H "Content-Type: text/xml" \ - -d "1101001" >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------" diff --git a/e3372-reboot.sh b/e3372-reboot.sh index 8f09b95..f4e8526 100755 --- a/e3372-reboot.sh +++ b/e3372-reboot.sh @@ -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 "1" +post_xml "device/control" \ + "1" \ No newline at end of file diff --git a/e3372-sendsms.sh b/e3372-sendsms.sh index 9740775..6b7e3fa 100755 --- a/e3372-sendsms.sh +++ b/e3372-sendsms.sh @@ -3,12 +3,9 @@ DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" . "$DIR/include.sh" -get_token +SMS_DATA="-1363USAGE51-1" -curl -s -X POST "http://$MODEM_IP/api/sms/send-sms" \ - -H "Cookie: $COOKIE" \ - -H "__RequestVerificationToken: $TOKEN" \ - -H "Content-Type: text/xml" \ - -d "-1363USAGE51-1" >modem_status.xml +get_token +post_xml "sms/send-sms" "$SMS_DATA" > $STATUS_FILE -cat modem_status.xml \ No newline at end of file +cat $STATUS_FILE \ No newline at end of file diff --git a/e3372-stats.sh b/e3372-stats.sh index 1df3f5e..413fe9c 100755 --- a/e3372-stats.sh +++ b/e3372-stats.sh @@ -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 "" | sed -e 's/<[^>]*>//g') -CurrDownload=$(cat modem_status.xml | grep "" | sed -e 's/<[^>]*>//g') -TotalUpload=$(cat modem_status.xml | grep "" | sed -e 's/<[^>]*>//g') -TotalDownload=$(cat modem_status.xml | grep "" | sed -e 's/<[^>]*>//g') -TotalConnectTime=$(cat modem_status.xml | grep "" | sed -e 's/<[^>]*>//g') +CurConnTime=$(cat $STATUS_FILE | grep CurrentConnectTime | sed -e 's/<[^>]*>//g') +CurrUpload=$(cat $STATUS_FILE | grep "" | sed -e 's/<[^>]*>//g') +CurrDownload=$(cat $STATUS_FILE | grep "" | sed -e 's/<[^>]*>//g') +TotalUpload=$(cat $STATUS_FILE | grep "" | sed -e 's/<[^>]*>//g') +TotalDownload=$(cat $STATUS_FILE | grep "" | sed -e 's/<[^>]*>//g') +TotalConnectTime=$(cat $STATUS_FILE | grep "" | 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" diff --git a/e3372.sh b/e3372.sh index 2310c72..1e1ad16 100755 --- a/e3372.sh +++ b/e3372.sh @@ -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" diff --git a/include.sh b/include.sh index e09000b..01c0cbf 100755 --- a/include.sh +++ b/include.sh @@ -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" } \ No newline at end of file -- cgit v1.2.3