#!/bin/sh # # tmo.sh - an Asuswrt-Merlin script to monitor and make changes to the # T-Mobile Sagemcom Fast Gateway # # You can check/enable/disable the 2 WiFi radios # And examine some of the existing gateway configuration # # this version (tmo.sh) uses dialog for a more graphical UI # but also supports a non-interactive mode # # location where configutration data is read from the Sagemcom and stored SCRIPTNAME="tmo" SCRIPTDIR="/jffs/addons/$SCRIPTNAME" SCRIPTVER="0.9.3" PWENC=-pbkdf2 CONFIG="$SCRIPTDIR/config.txt" CONFIGC="$SCRIPTDIR/configc.txt" CONFIGP="$SCRIPTDIR/configp.txt" RADIOS="$SCRIPTDIR/radios.txt" # dialog text formatting BOLD="\Zb" NORMAL="\Zn" RED="\Z1" GREEN="\Z2" # dialog variables DIALOG_CANCEL=1 DIALOG_ESC=255 DIALOG_QUIT="Q" HEIGHT=19 WIDTH=0 display_result() { dialog --title "$1" \ --no-collapse \ --colors \ --msgbox "$result" 8 20 } display_file() { dialog --title "$1" \ --no-collapse \ --textbox "$2" 0 0 } display_info() { dialog --infobox "$1" "$2" "$3" sleep $4 } selectband() { while true; do exec 3>&1 selection=$(dialog \ --backtitle "Select WiFi Band to Monitor" \ --title "WiFi Band" \ --clear \ --cancel-label "Exit" \ --menu "Please select:" 9 30 4 \ "1" "2.4 GHz Band" \ "2" "5 GHz Band" \ 2>&1 1>&3) exit_status=$? exec 3>&- case $exit_status in $DIALOG_CANCEL) clear menu ;; $DIALOG_ESC) clear menu ;; esac case $selection in 1 ) barsm=A return ;; 2 ) barsm=X return esac done } # # username and password when logging into the T-Mobile Sagemcom Fast gateway # needed for scripts that call tmo. Sorry, in plain text... # sourced from /jffs/scripts/tmo/tmo.conf # init_tmo() { if [ -d "/opt/bin" ] && [ ! -L "/opt/bin/tmo" ]; then ln -s "/jffs/scripts/tmo.sh" /opt/bin/tmo chmod 0755 "/opt/bin/tmo" fi write_tmo_config } write_tmo_config() { mkdir -p "${SCRIPTDIR}" echo "# TMO login settings #" > "${SCRIPTDIR}/tmo.conf" echo "USER="admin"" >> "${SCRIPTDIR}/tmo.conf" echo "PWENC="-pbkdf2"" >> "${SCRIPTDIR}/tmo.conf" echo "# PASS is encoded in tmopw.enc" >> "$SCRIPTDIR/tmo.conf" if [ ! -x /opt/bin/opkg ]; then echo "NOMENU=1" >> "${SCRIPTDIR}/tmo.conf" else echo "NOMENU=0" >> "${SCRIPTDIR}/tmo.conf" fi echo "# if you want to keep track for how many reboots have occured, set to 1 and define the log file (REBOOTLOGFILE)" >> "${SCRIPTDIR}/tmo.conf" echo "LOGREBOOTS=0" >> "${SCRIPTDIR}/tmo.conf" echo "REBOOTLOGFILE=$SCRIPTDIR/rebootcnt" >> "${SCRIPTDIR}/tmo.conf" # generate initial fake password echo -n "NotArealPassword" | /usr/sbin/openssl aes-256-cbc $PWENC -out "${SCRIPTDIR}/tmopw.enc" -pass pass:ditbabot,isoi } getbars() { bars24=$(jq '.signal."4g".bars' $CONFIG) bars5=$(jq '.signal."5g".bars' $CONFIG) band24=$(grep -m 2 "bands" $CONFIGC | head -1 | sed 's/bands //') band5=$(grep -m 2 "bands" $CONFIGC | tail -1 | sed 's/bands //') if [ ! -z "$1" ]; then if [ "$1" = "A" ]; then monbar="$bars24" monband="$band24" else monbar="$bars5" monband="$band5" fi fi } printbars() { printf "\\n\\n Signal Strength \\n\\n" while true do signal getbars bar24=$(expr $bars24 \* 20) bar5=$(expr $bars5 \* 20) printf "\\r 4G LTE$band24: $(expr $bars24 \* 20)%% 5G$band5: $(expr $bars5 \* 20)%% " if read -r -t 3; then printf "\\n\\n" return fi done } wifistate() { tmostatus if [ $(grep $1 $RADIOS | grep -c Enabled) -gt 0 ]; then if [ $2 = "0" ]; then echo "ON" else echo "${GREEN}ON" fi else if [ $2 = "0" ]; then echo "OFF" else echo "${RED}OFF" fi fi } input_password() { data=$SCRIPTDIR.tmp # trap and remove temporary file trap "rm -f $data" 0 1 2 5 15 # get password with the --insecure option dialog --title "Gateway Password" \ --clear \ --insecure \ --passwordbox "Enter the password of the Gateway" 9 40 2> $data ret=$? # make decison case $ret in 1) display_info "Cancel pressed" 4 20 2 rm -f $data ;; 255) display_info "Cancel pressed" 4 20 2 echo "NotARealPassword" > $data retutn ;; esac } set_tmopwdi() { if [ ! -f "${SCRIPTDIR}/tmo.conf" ]; then write_tmo_config fi . "${SCRIPTDIR}/tmo.conf" /usr/sbin/openssl version | awk '$2 ~ /(^0\.)|(^1\.(0\.|1\.0))/ { exit 1 }' && PWENC=-pbkdf2 if [ ! -f "${SCRIPTDIR}/tmopw.enc" ]; then printf "\\n There is no password set for $USER - please set one now\\n" else echo [ -f "${SCRIPTDIR}/tmopw.enc" ] && echo "Current password for $USER: $(/usr/sbin/openssl aes-256-cbc $PWENC -d -in "${SCRIPTDIR}/tmopw.enc" -pass pass:ditbabot,isoi)" echo fi while true; do printf "\\n Change password for $USER now? [1=Yes e=Exit] ";read -r continue case "$continue" in 1) printf "\\n Enter new Password: [e=Exit] ";read -r value if [ "$value" != e ]; then PASSWORD=$value echo -n $PASSWORD | /usr/sbin/openssl aes-256-cbc $PWENC -out "${SCRIPTDIR}/tmopw.enc" -pass pass:ditbabot,isoi gettmopwd token fi break;; e|E) break;; *) printf "\\n input is not an option\\n";; esac done } set_tmopwd() { if [ ! -f "${SCRIPTDIR}/tmo.conf" ]; then write_tmo_config fi . "${SCRIPTDIR}/tmo.conf" /usr/sbin/openssl version | awk '$2 ~ /(^0\.)|(^1\.(0\.|1\.0))/ { exit 1 }' && PWENC=-pbkdf2 if [ ! -f "${SCRIPTDIR}/tmopw.enc" ]; then display_info "There is no password set to access the Gateway - please set one now" 5 30 3 else dialog --title "Gateway Password" \ --yesno "Would you like to see the present password?" 6 50 response=$? if [ "$response" = "0" ]; then passis=$(/usr/sbin/openssl aes-256-cbc $PWENC -d -in "${SCRIPTDIR}/tmopw.enc" -pass pass:ditbabot,isoi) dialog --msgbox "$passis" 6 50 fi fi dialog --title "Input Gateway Password" \ --backtitle "tmo" \ --yesno "Input password of Gateway now?" 6 50 response=$? case "$response" in 0 ) input_password if [ -f $data ] && [ ! -z $data ]; then PASSWORD=$(cat $data) rm $data echo -n $PASSWORD | /usr/sbin/openssl aes-256-cbc $PWENC -out "${SCRIPTDIR}/tmopw.enc" -pass pass:ditbabot,isoi gettmopwd token display_info "Password changed" 5 20 2 else display_info "Password not changed" 5 24 2 fi ;; 1 ) if [ -f "{SCRIPTDIR}/tmopw.enc" ]; then dialog --msgbox "Password unchanged" 5 25 else dialog --msgbox "You will need to input the Gateway password before using this program" 7 60 fi ;; * ) dialog --msgbox "Input is not an option" 5 30 ;; esac } verifypw() { config if [ $(grep -c "Authorization" $CONFIG ) -gt 0 ]; then dialog --title "Verify Gateway Password" \ --yesno "The password was rejected by the Gateway. Try to change it?" 7 60 response=$? case $response in 0) set_tmopwd ;; 1) display_info "Ok. You will need to set a correct password before using the program..." 6 20 2 ;; 255) display_info "You will need to set a correct password before using the program..." 6 20 2 ;; esac fi } verifypwi() { config if [ $(grep -c "Authorization" $CONFIG ) -gt 0 ]; then echo "The password was rejected by the Gateway. Try to change it? (Y or N)" read response case $response in Y|y) set_tmopwdi ;; N|n) echo "Ok. You will need to set a correct password before using the program..." exit 1 ;; *) echo "You will need to set a correct password before using the program..." exit 1 ;; esac fi } set_tmouser() { if [ ! -f "${SCRIPTDIR}/tmo.conf" ]; then write_tmo_config fi . "${SCRIPTDIR}/tmo.conf" printf "\\nPresent User: $USER\\n" while true; do printf "\\n Change User name now? [1=Yes e=Exit] ";read -r continue case "$continue" in 1) printf "\\n Enter new User name: [e=Exit] ";read -r value if [ "$value" != e ]; then USER=$value fi sed -i "/USER/d" "${SCRIPTDIR}/tmo.conf" echo "USER=$USER" >> "${SCRIPTDIR}/tmo.conf" break;; e|E) exit;; *) printf "\\n input is not an option\\n";; esac done } gettmopwd() { if [ -f "${SCRIPTDIR}/tmopw.enc" ]; then PASS="$(/usr/sbin/openssl aes-256-cbc $PWENC -d -in "${SCRIPTDIR}/tmopw.enc" -pass pass:ditbabot,isoi)" else printf "\\nError - no password set for TMO Gateway\\n" PASS="" fi } tmoinstall() { echo "Creating script directory ${SCRIPTDIR}" mkdir -p "${SCRIPTDIR}" if [ ! -x /opt/bin/opkg ]; then printf "\\ntmo requires Entware to be installed\\n" printf "\\nInstall Entware using amtm and run tmo.sh install\\n\\n" exit else echo "Checking for and installing required apps" opkg update for app in dialog jq column; do if [ ! -x /opt/bin/$app ]; then echo "Installing $app to /opt/bin" opkg install $app fi done fi init_tmo cat < tmoheader } menu() { while true; do exec 3>&1 verifypw selection=$(dialog \ --backtitle "TMO Sagemcom Gateway Utility $SCRIPTVER" \ --title "Menu" \ --clear \ --colors \ --cancel-label "Exit" \ --menu "Please select:" $HEIGHT $WIDTH 4 \ "1" "Show all Gateway information" \ "2" "Show WiFi Configuration" \ "3" "Show 5G and LTE Signal Status" \ "4" "Display Signal Strength" \ "5" "Show Connected Clients" \ "6" "Toggle 2.4 GHz Radio which is ${BOLD}$(wifistate 2.4 1)${NORMAL}" \ "7" "Toggle 5 GHz Radio which is ${BOLD}$(wifistate 5 1)${NORMAL}" \ "P" "Set Password Used to Access Gateway" \ "R" "Reboot Gateway" \ "Q" "Press Q to Quit" \ "U" "Uninstall tmo" \ 2>&1 1>&3) exit_status=$? exec 3>&- case $exit_status in $DIALOG_CANCEL) clear echo "Program terminated." exit ;; $DIALOG_ESC) clear echo "Program aborted." >&2 exit 1 ;; esac case $selection in 1 ) gwall display_file "Gateway Info" $CONFIGC ;; 2 ) config sed -i 's/5.0ghz/\n5.0ghz/g' $CONFIGC display_file "WiFi Configuration" $CONFIGC ;; 3 ) signal sed -i 's/5g/\n5g/g' $CONFIGC display_file "WAN Radio Signals" $CONFIGC ;; 4 ) selectband if [ "$barsm" = "A" ]; then bandtitle="2.4GHz Band:" else bandtitle="5GHz Band:" fi while true do signal getbars "$barsm" echo $(expr $monbar \* 20) | dialog --keep-window --title "$bandtitle$monband" \ --guage "Press Enter to exit..." 7 70 0 if read -r -t 3; then clear menu fi done ;; 5 ) gclients sed -i 's/signal/\nsignal/g' $CONFIGC display_file "Connected Clients" $CONFIGC ;; 6 ) if [ $(wifistate 2.4 0) = "ON" ]; then wifioff24 result=$(echo "2.4GHz Radio ${BOLD}${RED}Off${NORMAL}") display_result "2.4GHz Wifi" else wifion24 result=$(echo "2.4GHz Radio ${BOLD}${GREEN}On${NORMAL}") display_result "2.4GHz Wifi" fi ;; 7 ) if [ $(wifistate 5 0) = "ON" ]; then wifioff5 result=$(echo "5GHz Radio ${BOLD}${RED}Off${NORMAL}") display_result "5GHz Wifi" else wifion5 result=$(echo "5GHz Radio ${BOLD}${GREEN}On${NORMAL}") display_result "5GHz WiFi" fi ;; R ) dialog --title "Reboot Gateway" \ --backtitle "tmo" \ --defaultno \ --yesno "Are you sure you want to reboot the Gateway (takes ~3 min.s)?" 7 60 response=$? case $response in 0) display_info "Rebooting..." 4 20 5 reboot display_info "See you in 3+ minutes, exiting" 4 35 2 clear exit ;; 1) display_info "Not Rebooting" 5 20 2 ;; 255) display_info "Not Rebooting" 5 20 2 ;; esac ;; P ) set_tmopwd ;; Q ) clear exit ;; U ) dialog --title "Uninstall tmo" \ --defaultno \ --yesno "Are you sure you want to uninstall tmo and all it's files ?" 7 60 response=$? case $response in 0) display_info "Uninstalling..." 4 20 3 removetmo display_info "tmo uninstalled, exiting" 4 35 2 clear exit ;; 1) display_info "tmo not removed" 5 20 2 ;; 255) display_info "tmo not removed" 5 20 2 ;; esac esac done } wifioff24() { curl -s -H "$HEADER" -o $CONFIG http://192.168.12.1/TMI/v1/network/configuration?get=ap sed -i 's/"2.4ghz":{"isRadioEnabled":true/"2.4ghz":{"isRadioEnabled":false/g' $CONFIG curl -s -H "$HEADER" -d @$CONFIG -H "Content-Type: application/json" -X POST -m 1 http://192.168.12.1/TMI/v1/network/configuration?set=ap } wifion24() { curl -s -H "$HEADER" -o $CONFIG http://192.168.12.1/TMI/v1/network/configuration?get=ap sed -i 's/"2.4ghz":{"isRadioEnabled":false/"2.4ghz":{"isRadioEnabled":true/g' $CONFIG curl -s -H "$HEADER" -d @$CONFIG -H "Content-Type: application/json" -X POST -m 1 http://192.168.12.1/TMI/v1/network/configuration?set=ap } wifioff5() { curl -s -H "$HEADER" -o $CONFIG http://192.168.12.1/TMI/v1/network/configuration?get=ap sed -i 's/"5.0ghz":{"isRadioEnabled":true/"5.0ghz":{"isRadioEnabled":false/g' $CONFIG curl -s -H "$HEADER" -d @$CONFIG -H "Content-Type: application/json" -X POST -m 1 http://192.168.12.1/TMI/v1/network/configuration?set=ap } wifion5() { curl -s -H "$HEADER" -o $CONFIG http://192.168.12.1/TMI/v1/network/configuration?get=ap sed -i 's/"5.0ghz":{"isRadioEnabled":false/"5.0ghz":{"isRadioEnabled":true/g' $CONFIG curl -s -H "$HEADER" -d @$CONFIG -H "Content-Type: application/json" -X POST -m 1 http://192.168.12.1/TMI/v1/network/configuration?set=ap } pause() { echo echo -n "Paused, hit any key to continue..." read a } convertConfig() { cat $CONFIG | tr "," "\n" | tr -d "^ " | sed 's/\"//g' | sed 's/:/|/' | sed 's/{//g' | sed 's/}//g' | column -t -s "|" > $CONFIGC jq '.' $CONFIG > $CONFIGP } config() { curl -s -H "$HEADER" 'http://192.168.12.1/TMI/v1/network/configuration?get=ap' -o $CONFIG convertConfig } signal() { curl -s -H "$HEADER" 'http://192.168.12.1/TMI/v1/gateway?get=signal' -o $CONFIG convertConfig } gwall() { curl -s -H "$HEADER" 'http://192.168.12.1/TMI/v1/gateway?get=all' -o $CONFIG convertConfig } gclients() { curl -s -H "$HEADER" 'http://192.168.12.1/TMI/v1/network/telemetry?get=clients' -o $CONFIG convertConfig } reboot() { curl -s -m 1 -X POST -H "$HEADER" 'http://192.168.12.1/TMI/v1/gateway/reset?set=reboot' -d "Content-Length: 0" } tmostatus() { config if [ $(jq '."2.4ghz".isRadioEnabled' $CONFIG) = "true" ]; then echo "2.4 GHz WiFi Enabled" > $RADIOS else echo "2.4 GHz Wifi Disabled" > $RADIOS fi if [ $(jq '."5.0ghz".isRadioEnabled' $CONFIG) = "true" ]; then echo "5.0 GHz WiFi Enabled" >> $RADIOS else echo "5.0 GHz WiFi Disabled" >> $RADIOS fi } wifisettingswait() { echo "Please wait..." sleep 5 status } setradio() { if [ $1 = "2.4" ]; then if [ $2 = "off" ]; then wifioff24 elif [ $2 = "on" ]; then wifion24 else echo "Wrong value off or on" exit 1 fi elif [ $1 = "5" ]; then if [ $2 = "off" ]; then wifioff5 elif [ $2 = "on" ]; then wifion5 else echo "Wrong value off or on" exit 1 fi else echo "Wrong radio 2.4 or 5" fi } printhelp() { cat < /tmp/rbc && mv /tmp/rbc $REBOOTLOGFILE logger -t "tmo.sh" "Logged Reboot Count" fi exit 0 ;; password) set_tmopwdi exit 0 ;; install) tmoinstall exit 0 ;; menu) gomenu exit 0 ;; help) printhelp exit 0 ;; uninstall) tmouninstall exit 0 ;; bars) printbars exit 0 ;; *) echo "Unknown command" exit 1 ;; esac