選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

nma 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. #########################################################################################
  3. ### androidNotifty.sh #
  4. ### Script for sending notifications using "Notify My Androids" API #
  5. ### Author : Markp1989@gmail.com Version: 25JULY2011 #
  6. #########################################################################################
  7. ## Requirements: curl #
  8. #########################################################################################
  9. ## Usage: androidNotify.sh "Application Name" "Event Name" "Some Details" [priority] #
  10. #########################################################################################
  11. ## API Documentation: https://www.notifymyandroid.com/api.jsp #
  12. #########################################################################################
  13. loadConf()
  14. {
  15. if [ -e "$1" ]
  16. then
  17. echo "[ info ] Found a config file: $1"
  18. . "$1"
  19. fi
  20. }
  21. #####start user configurable settings####
  22. APIkey="0" ## API Key must me set here, go to http://www.notifymyandroid.com/ to get one
  23. limit=5 # how many times to attempt to run curl, can help on unstable networks.
  24. pinghost="google.fr" ##host to ping before running anything to verify that the internet is up.
  25. #####end user configurable settings######
  26. loadConf /etc/nma.conf
  27. loadConf ~/.nma.conf
  28. ##renaming parameters to make rest of the code more readable.
  29. command=$0
  30. application=$1
  31. event=$2
  32. description=$3
  33. priority=$4
  34. ##the urls that are used to send the notification##
  35. baseurl="https://www.notifymyandroid.com/publicapi"
  36. verifyurl="$baseurl/verify"
  37. posturl="$baseurl/notify"
  38. ##choosing a unique temp file based on the time (date,month,hour,minute,nano second),to avoid concurent usage interfering with each other
  39. notifyout=/tmp/androidNotify$(date '+%d%m%Y%H%M%S%N')
  40. usage="Usage: $command Application Event Description [priority]"
  41. ##exit functions so i dont have to keep writing the same exit messages for every fail.
  42. function error_exit {
  43. ##this function is used to exit the program in the event of an error.
  44. printf '%s\n\t%s\n\t\t%s\n' "[ Error ] Notification not sent:" "$errormessage" "$usage"
  45. printf '\n%s\n' "$application , $event , $description, priority=$priority" >> $notifyout ##adding info used to tmp file.
  46. exit 1
  47. }
  48. function clean_exit {
  49. printf '%s\n' "[ info ] Notification sent"
  50. rm "$notifyout" ##removing output file when notification was sent okay
  51. exit 0
  52. }
  53. function pre_check {
  54. ##this function checks that curl is installed, and that an internet connection is available
  55. which curl >/dev/null || { errormessage="curl is required but it's not installed, exiting."; error_exit; }
  56. ##checking that the machine can connect to the internet by pinging the host defined in $pinghost
  57. ping -c 1 -w 10 "$pinghost" >/dev/null || { errormessage="internet connection is required but not connected, exiting."; error_exit; }
  58. }
  59. function input_check {
  60. ##this section checks that the parameters are an acceptable length if any of these tests fail then the program exits.
  61. ##the API will send an error back if the data sent is invalid, but no point in knowingly sending incorrect information.
  62. #API key must be 48 characters long, just because the APIkey is the right length doesnt mean its valid,the API will complain if it is invalid.
  63. if [[ "${#APIkey}" -ne "48" ]]; then
  64. errormessage="APIkey must be 48 characters long, you gave me ${#APIkey}"
  65. error_exit
  66. #application must be between 1 and 256 characters long
  67. elif [[ "${#application}" -gt "256" || "${#application}" -lt "1" ]]; then
  68. errormessage="[ error ] the application parameter is invalid or missing"
  69. error_exit
  70. #event must be between 1 and 1000 characters long
  71. elif [[ "${#event}" -gt "1000" || "${#event}" -lt "1" ]]; then
  72. errormessage="[ error ] the event parameter is invalid or missing"
  73. error_exit
  74. #description must be between 1 and 1000 characters long
  75. elif [[ "${#description}" -gt "1000" || "${#description}" -lt "1" ]]; then
  76. errormessage="[ error ] the description parameter is invalid or missing"
  77. error_exit
  78. ##priority is expected to be between -2 and 2, if other numbers are given than default(0) is used.
  79. fi
  80. if [ -z $priority ]; then
  81. printf '%s\n' "priority is not set , must be between -2 and 2, using 0 instead"
  82. priority=0
  83. elif [[ "$priority" -gt "2" || $priority -lt "-2" ]]; then
  84. printf '%s\n' "priority $priority is invalid, must be between -2 and 2, using 0 instead"
  85. priority=0
  86. fi
  87. if [[ -n "$5" ]]; then
  88. errormessage="[ error ] too many parameters have been provided:"
  89. error_exit
  90. fi
  91. }
  92. function send_notification {
  93. ##sending the notification using curl
  94. #if curl failes to complete then it will try again, untill the "limit" is met,or curl runs ok.
  95. complete=0
  96. tries=0
  97. while [[ $complete -lt 1 && $tries -lt $limit ]]
  98. do
  99. #if curl is already running that we wait up to a minute before proceding
  100. if [ "$(pidof curl)" ]
  101. then
  102. printf '%s\n' "another instance of curl is running, waiting up to 1 minute before trying."
  103. sleep $((RANDOM % 60)) ##waiting up to 1 minute as running multiple instances of curl at a time was causing some to fail on my machine.
  104. fi
  105. curl --silent --data-ascii "apikey=$APIkey" --data-ascii "application=$application" --data-ascii "event=$event" --data-asci "description=$description" --data-asci "priority=$priority" $posturl -o $notifyout && complete=1
  106. tries=$tries+1
  107. done
  108. }
  109. function check_notification {
  110. ##checking that the notification was sent ok.
  111. ##api returns message 200 if the notification was sent
  112. if grep -q 200 $notifyout; then
  113. clean_exit
  114. else
  115. ##getting the error reported by the API, this may break if the API changes its output, will change as soon as I can find a commandline xml parser
  116. errormessage="$(cut -f4 -d'>' $notifyout | cut -f1 -d'<')"
  117. error_exit
  118. fi
  119. }
  120. ##running the functions
  121. pre_check
  122. input_check
  123. send_notification
  124. check_notification