Guest User

stealthMode.sh

a guest
Mar 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.52 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Turn off/on all led for Asus routers RT-N16 and RT-N66u and RT-AC66u
  4. #
  5. # Version: 0.10 "Sunset"
  6. # Authors: Monter, Shibby
  7. #
  8. # Unlock lan ports, Permanent On function, Sunset mode,
  9. # Scheduler mode and Clear All option written by Monter
  10.  
  11. # configuration
  12. VERSION="v0.10"
  13. # /configuration
  14.  
  15. # Please do not make any changes in the code below without authors permission
  16. # Send your bug report to monter[at]techlog.pl
  17.  
  18. dir=`/bin/echo $( cd "$(/usr/bin/dirname "$0")" ; /bin/pwd -P )`
  19. stms=`/usr/bin/basename ${0}`
  20. N16=`/bin/nvram get t_model_name | /bin/grep RT-N16 | /usr/bin/wc -l`
  21. N66U=`/bin/nvram get t_model_name | /bin/grep RT-N66U | /usr/bin/wc -l`
  22. AC66U=`/bin/nvram get t_model_name | /bin/grep RT-AC66U | /usr/bin/wc -l`
  23.  
  24. if /usr/bin/[ "$N16" == "1" -o "$N66U" == "1" -o "$AC66U" == "1" -o true ]; then
  25.     case "$1" in
  26.         on)
  27.             $dir/$stms perm_on
  28.             /usr/bin/logger -p6 -s -t stealthMode Activated
  29.             /usr/sbin/cru a stealthmode "*/5 * * * * $dir/$stms perm_on"
  30.         ;;
  31.         sun)
  32.             if /usr/bin/[ "$2" != "" -a "0$(/bin/echo $2 | /usr/bin/tr -d ' ')" -ge 0 ] 2>/dev/null; then
  33.                 /usr/bin/logger -p6 -s -t stealthMode Sunset Enabled
  34.                 /usr/sbin/cru a stealthsun_on "0 2 * * * $dir/$stms sun_on $2"
  35.                 $dir/$stms sun_on $2
  36.             else
  37.                 /usr/bin/logger -p3 -s -t stealthMode Sunset error - the city code does not specified!
  38.                 $dir/$stms
  39.                 exit 1
  40.             fi
  41.         ;;
  42.         sun_on)
  43.             # check internet connection
  44.             CONERR=1
  45.             YAHOOAPIS="weather.yahooapis.com"
  46.  
  47.             if /usr/bin/[ `/bin/echo $YAHOOAPIS | /usr/bin/wc -c` -gt 2 ]; then
  48.               /bin/ping -q -w 1 -c 1 $YAHOOAPIS > /dev/null
  49.               if /usr/bin/[ $? == 0 ]; then CONERR=0; fi
  50.             fi
  51.  
  52.             # result of internet connection check
  53.             if /usr/bin/[ "$CONERR" -ne 0 ] ; then
  54.                 /usr/bin/logger -p3 -s -t stealthMode Sunset error - no active Internet connection!
  55.                 $dir/$stms
  56.                 exit 1
  57.             else
  58.                 if /usr/bin/[ "$2" != "" -a "0$(/bin/echo $2 | /usr/bin/tr -d ' ')" -ge 0 ] 2>/dev/null; then
  59.                     $dir/$stms sch_clear # clear scheduled tasks when sunset is enabled
  60.                     sun=`l=$2;/usr/bin/wget -q -O - http://weather.yahooapis.com/forecastrss?w=$l | /bin/grep astronomy | /usr/bin/awk -F\" '{print $4}' | /usr/bin/awk -F\" '{split($1, A, " ");split(A[1], B, ":");HR=B[1];MIN=B[2];if(A[2] == "pm") HR+=12;$1=sprintf("%d %d", MIN, HR);}1'`
  61.                     if /usr/bin/[ "$sun" = "" ] ; then /usr/bin/logger -p3 -s -t stealthMode Weather sunset time error!; exit 1; else /usr/sbin/cru a stealthsunset $sun "* * * $dir/$stms on"; fi
  62.                     sur=`l=$2;/usr/bin/wget -q -O - http://weather.yahooapis.com/forecastrss?w=$l | /bin/grep astronomy | /usr/bin/awk -F\" '{print $2}' | /usr/bin/awk -F\" '{split($1, A, " ");split(A[1], B, ":");HR=B[1];MIN=B[2];if(A[2] == "pm") HR+=12;$1=sprintf("%d %d", MIN, HR);}1'`
  63.                     if /usr/bin/[ "$sur" = "" ] ; then /usr/bin/logger -p3 -s -t stealthMode Weather sunrise time error!; exit 1; else /usr/sbin/cru a stealthsunrise $sur "* * * $dir/$stms off"; fi
  64.                     /usr/bin/logger -p6 -s -t stealthMode Sunset city code: $2, sunset: $( /bin/echo $sun | /usr/bin/awk '{$1=sprintf("%02d", $1); print $2":"$1 }' ), sunrise: $( /bin/echo $sur | /usr/bin/awk '{$1=sprintf("%02d", $1); print $2":"$1 }' )
  65.                     HOUR=$(/bin/date +%k); SUNRUN=$( /bin/echo $sun | /usr/bin/awk '{ print $2 }' ); SURRUN=$( /bin/echo $sur | /usr/bin/awk '{ print $2 }' ); if /usr/bin/[ "$HOUR" -ge "$SURRUN" -a "$HOUR" -le "$SUNRUN" ] ; then
  66.                         /usr/bin/logger -p6 -s -t stealthMode The time now is $( /bin/date +%H:%M), so StealthMode will be activated at next sun set
  67.                     else
  68.                         $dir/$stms on
  69.                     fi
  70.                 else
  71.                     /usr/bin/logger -p3 -s -t stealthMode Sunset error - the city code does not specified!
  72.                     $dir/$stms
  73.                     exit 1
  74.                 fi
  75.             fi
  76.         ;;
  77.         perm_on)
  78.             /sbin/gpio enable 0             # Power LED On
  79.             /sbin/gpio enable 6             # WAN LED On
  80.             /sbin/gpio enable 9             # LAN LED On
  81.             /sbin/gpio enable 3             # USB 2.0 LED On
  82.             /sbin/gpio enable enable 14            # USB 3.0 LED On
  83.             /usr/sbin/wl -i eth1 leddc 1     # Wireless LED On
  84.         ;;
  85.         sun_off)
  86.             /usr/bin/logger -p6 -s -t stealthMode Sunset Disactivated
  87.             /usr/sbin/cru d stealthsun_on
  88.             /usr/sbin/cru d stealthsunset
  89.             /usr/sbin/cru d stealthsunrise
  90.             $dir/$stms off
  91.         ;;
  92.         sch_on)
  93.             if /usr/bin/[ "${2}" != "" -a "${2}" -le 23 -a "0$(/bin/echo $2 | /usr/bin/tr -d ' ')" -ge 0 ] 2>/dev/null; then
  94.               if /usr/bin/[ "${3}" != "" -a "${3}" -le 59 -a "${3}" -eq "${3}" ] 2>/dev/null; then SCHMIN=${3}; else SCHMIN="0"; fi
  95.               /usr/sbin/cru a stealthsheduleon $SCHMIN $2 "* * * $dir/$stms on"
  96.               /usr/bin/logger -p6 -s -t stealthMode Scheduled On set to $2:$(/usr/bin/printf "%02d" $SCHMIN)
  97.             else
  98.               /usr/bin/logger -p3 -s -t stealthMode Scheduled On error - Hour / Minutes does not specified!
  99.               $dir/$stms
  100.               exit 1
  101.             fi
  102.         ;;
  103.         sch_off)
  104.             if /usr/bin/[ "${2}" != "" -a "${2}" -le 23 -a "0$(/bin/echo $2 | /usr/bin/tr -d ' ')" -ge 0 ] 2>/dev/null; then
  105.               if /usr/bin/[ "${3}" != "" -a "${3}" -le 59 -a "${3}" -eq "${3}" ] 2>/dev/null; then SCHMIN=${3}; else SCHMIN="0"; fi
  106.               /usr/sbin/cru a stealthsheduleoff $SCHMIN $2 "* * * $dir/$stms off"
  107.               /usr/bin/logger -p6 -s -t stealthMode Scheduled Off set to $2:$(/usr/bin/printf "%02d" $SCHMIN)
  108.             else
  109.               /usr/bin/logger -p3 -s -t stealthMode Scheduled Off error - Hour / Minutes does not specified!
  110.               $dir/$stms
  111.               exit 1
  112.             fi
  113.         ;;
  114.         sch_clear)
  115.             /usr/sbin/cru d stealthsheduleon
  116.             /usr/sbin/cru d stealthsheduleoff
  117.             /usr/bin/logger -p6 -s -t stealthMode Scheduler Tasks Deleted
  118.         ;;
  119.         clear_all)
  120.             $dir/$stms sun_off
  121.             $dir/$stms sch_clear
  122.             /usr/bin/logger -p6 -s -t stealthMode Complete shutdown and delete all jobs from Crontab - done
  123.         ;;
  124.         off)
  125.             /usr/sbin/cru d stealthmode
  126.             /sbin/gpio disable 0             # Power LED Off
  127.             /sbin/gpio disable 6             # WAN LED Off
  128.             /sbin/gpio disable 9             # LAN LED Off
  129.             /sbin/gpio disable 3             # USB 2.0 LED Off
  130.             /sbin/gpio disable 14            # USB 3.0 LED Off
  131.             /usr/sbin/wl -i eth1 leddc 0     # Wireless LED Off
  132.             /usr/bin/logger -p6 -s -t stealthMode Disactivated
  133.         ;;
  134.         *)
  135.             /bin/echo "stealthMode Sunset ${VERSION} by Monter"
  136.             /usr/bin/logger -p1 -s -t Usage "$stms {on|off|sun <city_code>|sun_off|sch_on <H> <M>|sch_off <H> <M>|sch_clear|clear_all}"
  137.             /bin/echo
  138.             /bin/echo " [Standard mode]"
  139.             /bin/echo "   on | off        - enable or disable steathMode in real time"
  140.             /bin/echo
  141.             /bin/echo " [Sunset mode]"
  142.             /bin/echo "   sun <city_code> - will daily get the sunrise and sunset times of a specific"
  143.             /bin/echo "                     location automatically and activate Sunset mode"
  144.             /bin/echo "                     To be able to determine your <city_code> you need to first"
  145.             /bin/echo "                     go to http://weather.yahoo.com/ and look up your location"
  146.             /bin/echo "                     The last NUMBERS in the URL will be your <city_code>"
  147.             /bin/echo "                     Example: \"$stms sun 514048\" if you live in Poznan/Poland ;)"
  148.             /bin/echo "                     This feature requires a working Internet connection"
  149.             /bin/echo "   sun_off         - fully deactivate Sunset mode"
  150.             /bin/echo
  151.             /bin/echo " [Scheduled mode]"
  152.             /bin/echo "   sch_on <H> <M>  - set the hour and minutes of the scheduled enable/disable"
  153.             /bin/echo "   sch_off <H> <M>   stealthMode in Standard mode and adding jobs to the Crontab"
  154.             /bin/echo "                     Hour and minute time must be a numbers without any additional"
  155.             /bin/echo "                     characters, where hour is a mandatory parameter, while not"
  156.             /bin/echo "                     specifying an minute will assign a default 00 value"
  157.             /bin/echo "                     These options add just the right job for Crontab, nothing more"
  158.             /bin/echo "   sch_clear       - removes tasks from Crontab for scheduled enable/disable"
  159.             /bin/echo "                     stealthMode function set by sch_on and sch_off switches"
  160.             /bin/echo
  161.             /bin/echo " [Repair / debug]"
  162.             /bin/echo "   clear_all       - removes all jobs from Crontab and completely disables all"
  163.             /bin/echo "                     available stealthMode modes"
  164.             /bin/echo
  165.             exit 1
  166.     esac
  167. else
  168.     /usr/bin/logger -p3 -s -t stealthMode Router does not supported by this feature
  169. fi
Advertisement
Add Comment
Please, Sign In to add comment