h8rt3rmin8r

set-android-rules.sh

Dec 29th, 2019
196
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env bash
  2. #==============================================================================#
  3. #
  4. # [ SET ANDROID RULES ]
  5. #
  6. #    Configure global Android connection rules on Linux
  7. #
  8. #    Adapted from information at ubuntuforums.org/showthread.php?t=2326450
  9. #    on 20191229 by h8rt3rmin8r
  10. #
  11. #==============================================================================#
  12.  
  13. # declare variables
  14.  
  15. runtime="$(date '+%s%N')"
  16. sh_name="set-android-rules"
  17. here_now="${PWD}"
  18. src_d="https://raw.githubusercontent.com"
  19. src_r="${src_d}/NicolasBernaerts"
  20. src_f="${src_r}/ubuntu-scripts/master/android/51-android.rules"
  21. rules_file="/etc/udev/rules.d/51-android.rules"
  22. rules_backup="/etc/udev/rules.d/51-android.rules_${runtime}.bak"
  23. declare -a depends=(heimdall-flash heimdall-flash-frontend adb)
  24.  
  25. # declare functions
  26.  
  27. function backup_rules() {
  28.     # Create a backup of the current rules file before writing new rules
  29.     ##
  30.     ## INPUTS:    [none]
  31.     ## OUTPUTS:   [none]
  32.     ## REQUESTS:  [none]
  33.     ##
  34.     sudo mv "${rules_file}" "${rules_backup}" 2>/dev/null
  35.     local exit_code="$?"
  36.     sudo touch "${rules_file}"
  37.     return ${exit_code}
  38. }
  39.  
  40. function get_root() {
  41.     # Gain access to 'sudo'
  42.     ##
  43.     ## INPUTS:    [none]
  44.     ## OUTPUTS:   [none]
  45.     ## REQUESTS:  [system password]
  46.     ##
  47.     sudo echo -n &>/dev/null
  48.     return $?
  49. }
  50.  
  51. function get_rules() {
  52.     # Fetch the remote rules file and install it locally
  53.     ##
  54.     ## INPUTS:    [none]
  55.     ## OUTPUTS:   [general verbosity]
  56.     ## REQUESTS:  [none]
  57.     ##
  58.     sudo wget -q --show-progress -O "${rules_file}" "${src_f}"
  59.     local ec_a="$?"
  60.     sudo chmod a+r "${rules_file}"
  61.     local ec_b="$?"
  62.     if [[ "${ec_a}${ec_b}" == "00" ]]; then
  63.         return 0
  64.     else
  65.         return 1
  66.     fi
  67. }
  68.  
  69. function has_rules() {
  70.     # Check if the system has a rules file
  71.     ##
  72.     ## INPUTS:    [none]
  73.     ## OUTPUTS:   TRUE | FALSE
  74.     ## REQUESTS:  [none]
  75.     ##
  76.     if [[ -f "${rules_file}" ]]; then
  77.         echo "TRUE"
  78.         return 0
  79.     else
  80.         echo "FALSE"
  81.         return 1
  82.     fi
  83. }
  84.  
  85. function install_depends() {
  86.     # Install required software packages and libraries
  87.     ##
  88.     ## INPUTS:    [none]
  89.     ## OUTPUTS:   [general verbosity]
  90.     ## REQUESTS:  [user confirmation]
  91.     ##
  92.     sudo apt-get update
  93.     for i in "${depends[@]}"; do
  94.         sudo apt-get install "${i}"
  95.     done
  96.     return $?
  97. }
  98.  
  99. function tc_f() {
  100.     # Output verbosity styling function
  101.     ##
  102.     ## INPUTS:    <STYLE_PARAMETER>
  103.     ## OUTPUTS:   [self-named stylized verbosity]
  104.     ## REQUESTS:  [none]
  105.     ##
  106.     local s_n=${sh_name}
  107.     local s_na=$(echo '['$(printf "\e[1;92m${s_n}\e[0m")'] ')
  108.     case "$1" in
  109.         -e|--e|-err|--err)
  110.             local err_x=$(echo -en '[_\e[38;5;196merror\e[0m_]')
  111.             echo "${s_na}${err_x} $(date '+%s%N') |"
  112.             ;;
  113.         -i|--i|-inf|--inf)
  114.             local inf_x=$(echo -en '[_\e[38;5;25minfo\e[0m__]')
  115.             echo "${s_na}${inf_x} $(date '+%s%N') |"
  116.             ;;
  117.         -r|--r|-req|--req)
  118.             local req_x=$(echo -en '[\e[0;49;35mrequest\e[0m]')
  119.             echo "${s_na}${req_x} $(date '+%s%N') |"
  120.             ;;
  121.         -s|--s|-sus|--sus)
  122.             local sus_x=$(echo -en '[\e[38;5;82msuccess\e[0m]')
  123.             echo "${s_na}${sus_x} $(date '+%s%N') |"
  124.             ;;
  125.         -eb|--eb|-errb|--errb)
  126.             local err_y=$(echo -en '[_\e[5m\e[38;5;196merror\e[0m_]')
  127.             echo "${s_na}${err_y} $(date '+%s%N') |"
  128.             ;;
  129.         -ib|--ib|-infb|--infb)
  130.             local inf_y=$(echo -en '[_\e[5m\e[38;5;25minfo\e[0m__]')
  131.             echo "${s_na}${inf_y} $(date '+%s%N') |"
  132.             ;;
  133.         -rb|--rb|-reqb|--reqb)
  134.             local req_y=$(echo -en '[\e[5;49;35mrequest\e[0m]')
  135.             echo "${s_na}${req_y} $(date '+%s%N') |"
  136.             ;;
  137.         -sb|--sb|-susb|--susb)
  138.             local sus_y=$(echo -en '[\e[5m\e[38;5;82msuccess\e[0m]')
  139.             echo "${s_na}${sus_y} $(date '+%s%N') |"
  140.             ;;
  141.         -ex|--ex|-errx|--errx)
  142.             local err_z=$(echo -en '[_\e[1;92merror\e[0m_]')
  143.             echo "${s_na}${err_z} $(date '+%s%N') |"
  144.             ;;
  145.         -ix|--ix|-infx|--infx)
  146.             local inf_z=$(echo -en '[_\e[1;92minfo\e[0m__]')
  147.             echo "${s_na}${inf_z} $(date '+%s%N') |"
  148.             ;;
  149.         -rx|--rx|-reqx|--reqx)
  150.             local req_z=$(echo -en '[\e[1;92mrequest\e[0m]')
  151.             echo "${s_na}${req_z} $(date '+%s%N') |"
  152.             ;;
  153.         -sx|--sx|-susx|--susx)
  154.             local sus_z=$(echo -en '[\e[1;92msuccess\e[0m]')
  155.             echo "${s_na}${sus_z} $(date '+%s%N') |"
  156.             ;;
  157.         *|'')
  158.             local s_nb=$(echo '['$(printf "\e[1;92m${s_n}\e[0m")']')
  159.             echo "${s_nb} $(date '+%s%N') |"
  160.             ;;
  161.     esac
  162.     return 0
  163. }
  164.  
  165. function udev_restart() {
  166.     # Restart the local udev service
  167.     ##
  168.     ## INPUTS:    [none]
  169.     ## OUTPUTS:   [general verbosity]
  170.     ## REQUESTS:  [none]
  171.     ##
  172.     sudo service udev restart
  173.     return $?
  174. }
  175.  
  176. # execute operations
  177.  
  178. echo "$(tc_f -i) Beginning configuration of Android connection rules"
  179. echo "$(tc_f -i) Gaining access to 'sudo' (admin password is required)"
  180. get_root
  181. if [[ "$?" -ne 0 ]]; then
  182.     echo "$(tc_f -e) Could not gain access to 'sudo'"
  183.     echo "$(tc_f -i) Aborting configuration operations"
  184.     exit 1
  185. fi
  186. echo "$(tc_f -s) Access to sudo acquired"
  187. echo "$(tc_f -i) Installing required software packages and libraries"
  188. install_depends
  189. echo "$(tc_f -s) Done"
  190. echo "$(tc_f -i) Checking for existing rules file"
  191. rf_check=$(has_rules &>/dev/null; echo $?)
  192. if [[ "${rf_check}" -ne 0 ]]; then
  193.     echo "$(tc_f -i) No rules file was found"
  194.     echo "$(tc_f -i) Creating new rules file: ${rules_file}"
  195.     sudo touch "${rules_file}"
  196.     echo "$(tc_f -s) Done"
  197. else
  198.     echo "$(tc_f -i) Rules file found"
  199.     echo "$(tc_f -i) Creating backup file: ${rules_backup}"
  200.     backup_rules
  201.     if [[ "$?" -eq 0 ]]; then
  202.         echo "$(tc_f -s) Done"
  203.     else
  204.         echo "$(tc_f -e) Encountered unknown errors while making backup file"
  205.         echo "$(tc_f -i) Aborting configuration operations"
  206.         exit 1
  207.     fi
  208. fi
  209. echo "$(tc_f -i) Installing new Android rules file from remote source"
  210. echo "$(tc_f -i) Source: ${src_f}"
  211. get_rules
  212. if [[ "$?" -ne 0 ]]; then
  213.     echo "$(tc_f -e) Encountered unknown errors whille installing new rules file"
  214.     echo "$(tc_f -i) Recommended: Check system internet connection"
  215.     echo "$(tc_f -i) Recommended: Check remote source file"
  216.     echo "$(tc_f -i) Aborting configuration operations"
  217.     exit 1
  218. else
  219.     echo "$(tc_f -s) Done"
  220. fi
  221. echo "$(tc_f -i) Restarting udev service"
  222. udev_restart
  223. if [[ "$?" -ne 0 ]]; then
  224.     echo "$(tc_f -e) Encountered errors while restarting service: udev"
  225.     echo "$(tc_f -i) Recommended: Check system service, udev"
  226.     echo "$(tc_f -i) Aborting configuration operations"
  227.     exit 1
  228. fi
  229. echo "$(tc_f -s) Done"
  230. endtime="$(date '+%s%N')"
  231. echo "$(tc_f -i) Operations started:    ${runtime}"
  232. echo "$(tc_f -i) Operations finished:   ${endtime}"
RAW Paste Data