#!/bin/bash # Company: PowerCraft Technology # Author: Copyright Jelle de Jong # Note: Please send me an email if you enhanced the script # Version: 0.0.3 # Date: 31-03-2008 / 2008-12-20 / 2009-01-14 # System: Ubuntu/Debian GNU/Linux # Description: Closing HSDPA USB Huawei E220/E156G/E180 Monitoring # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more de/usr/bin/tails. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. #~ check=$(lsmod | grep option) #~ [ -n "$check" ] && sudo rmmod option #~ #~ check=$(lsmod | grep usbserial) #~ [ -n "$check" ] && sudo rmmod usbserial #~ #~ sudo modprobe usbserial vendor=0x12d1 product=0x1003 # set -x # exec 2>/var/log/pct-mobile-internet-close.log convert() { value="$1" # credits: http://www.linuxjournal.com/article/9293 kilo=$( echo "scale=2; $value / 1024" | bc ) kiloint=$( echo "$value / 1024" | bc ) mega=$( echo "scale=2; $kilo / 1024" | bc ) megaint=$( echo "$kilo / 1024" | bc ) giga=$( echo "scale=2; $mega / 1024" | bc ) gigaint=$( echo "$mega / 1024" | bc ) if [ $kiloint -lt 1 ]; then echo "$value bytes" elif [ $megaint -lt 1 ]; then echo "${kilo}KB" elif [ $gigaint -lt 1 ]; then echo "${mega}MB" else echo "${giga}GB" fi } active=$(ps aux | grep ppp-monitor | grep bwm-ng) [ -n "$active" ] && /usr/bin/screen -x ppp-monitor -X quit && echo "closing existing screen session" device=ppp0 deviceinfo=$(grep $device /proc/net/dev) if [ -z "$deviceinfo" ]; then echo "no device info found" exit 1 fi deviceinfo=${deviceinfo##*":"} receive=$(echo $deviceinfo | awk '{print $1}') transmit=$(echo $deviceinfo | awk '{print $9}') DATANFILE="/var/lib/pct-mobile-internet/pct-mobile-internet.log" if [ ! -e $DATANFILE ] then echo 'sum_receive=0 sum_transmit=0 sum_transfer=0 abbo_start=2008/12/20 abbo_duration=30 abbo_days_left=0 last_receive_human_readable=0 last_transmit_human_readable=0 sum_receive_human_readable=0 sum_transmit_human_readable=0 sum_transfer_human_readable=0' > $DATANFILE fi sum_receive=$(grep --no-messages ^sum_receive= "$DATANFILE") sum_transmit=$(grep --no-messages ^sum_transmit= "$DATANFILE") sum_transfer=$(grep --no-messages ^sum_transfer= "$DATANFILE") abbo_start=$(grep --no-messages ^abbo_start= "$DATANFILE") abbo_duration=$(grep --no-messages ^abbo_duration= "$DATANFILE") sum_receive=${sum_receive##*"="} sum_transmit=${sum_transmit##*"="} sum_transfer=${sum_transfer##*"="} abbo_start=${abbo_start##*"="} abbo_duration=${abbo_duration##*"="} unixtime_abbo_start=$(date -d "$abbo_start" +%s) unixtime_abbo_end=$(date -d "$abbo_start + $abbo_duration days" +%s) unixtime_today=$(date +%s) remaining=$(( $unixtime_abbo_end - $unixtime_today )) abbo_days_left=$(($remaining/3600/24)) find="abbo_days_left=.*" replace="abbo_days_left=$abbo_days_left" sed -i -e "s,$find,$replace,g" "$DATANFILE" if [ ! -z $receive ] && [ ! -z $receive ]; then if [ ! $receive -gt 0 ] || [ ! $transmit -gt 0 ]; then echo "receive: $receive, transmit: $transmit" echo "not saving status file" exit 1 fi fi new_receive=$(( $sum_receive + $receive )) new_transmit=$(( $sum_transmit + $transmit )) new_transfer=$(( $new_receive + $new_transmit )) find="sum_receive=.*" replace="sum_receive=$new_receive" sed -i -e "s,$find,$replace,g" "$DATANFILE" find="sum_transmit=.*" replace="sum_transmit=$new_transmit" sed -i -e "s,$find,$replace,g" "$DATANFILE" find="sum_transfer=.*" replace="sum_transfer=$new_transfer" sed -i -e "s,$find,$replace,g" "$DATANFILE" last_receive_human_readable=$(convert $receive) last_transmit_human_readable=$(convert $transmit) find="last_receive_human_readable=.*" replace="last_receive_human_readable=$last_receive_human_readable" sed -i -e "s,$find,$replace,g" "$DATANFILE" find="last_transmit_human_readable=.*" replace="last_transmit_human_readable=$last_transmit_human_readable" sed -i -e "s,$find,$replace,g" "$DATANFILE" new_receive_human_readable=$(convert $new_receive) new_transmit_human_readable=$(convert $new_transmit) new_transfer_human_readable=$(convert $new_transfer) find="sum_receive_human_readable=.*" replace="sum_receive_human_readable=$new_receive_human_readable" sed -i -e "s,$find,$replace,g" "$DATANFILE" find="sum_transmit_human_readable=.*" replace="sum_transmit_human_readable=$new_transmit_human_readable" sed -i -e "s,$find,$replace,g" "$DATANFILE" find="sum_transfer_human_readable=.*" replace="sum_transfer_human_readable=$new_transfer_human_readable" sed -i -e "s,$find,$replace,g" "$DATANFILE" # [ -e /etc/network/interfaces.huawei.backup ] && mv --verbose /etc/network/interfaces.huawei.backup /etc/network/interfaces exit