You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

105 lines
2.6 KiB

  1. #!/bin/bash
  2. function display_help() {
  3. echo "${0} Usage :"
  4. echo -e "\t-u --user\tSpecify the username"
  5. echo -e "\t-p --password\tSpecify the password"
  6. echo -e "\t-h or --help\tThis help"
  7. echo ""
  8. echo "Examples :"
  9. echo -e "\tSend \"My message\" to your mobile phone :"
  10. echo -e "\t\t${0} -u myuser -p mypassword My message"
  11. echo -e "\tSend content from STDIN to your mobile phone :"
  12. echo -e "\t\t${0} -u myuser -p mypassword -"
  13. }
  14. GET_OPT=`getopt -o hu:p: --long help,user:,password: -n "${0} parameters" -- "${@}"`
  15. if [ ! "${?}" == "0" ] ; then
  16. display_help
  17. exit 1
  18. fi
  19. eval set -- "$GET_OPT"
  20. while true ; do
  21. case ${1} in
  22. -u|--user)
  23. NOM="${2}"
  24. shift 2
  25. ;;
  26. -p|--password)
  27. PASS="${2}"
  28. shift 2
  29. ;;
  30. -h|--help)
  31. display_help
  32. shift
  33. exit 0
  34. ;;
  35. --)
  36. MESSAGE="`echo ${@} | sed 's|^-- ||g'`"
  37. shift
  38. break
  39. ;;
  40. *)
  41. display_help >&2
  42. exit 1
  43. ;;
  44. esac
  45. done
  46. if [ -z "${NOM}" -o -z "${PASS}" ] ; then
  47. echo "You need to specify Username and Password" >&2
  48. display_help >&2
  49. exit 1
  50. fi
  51. if [ "${MESSAGE}" == "-" ] ; then
  52. if [[ ! -t 0 ]]; then
  53. while read line ; do
  54. MESSAGE="${MESSAGE}%0D${line}"
  55. MESSAGE=`echo ${MESSAGE} | sed 's|^-%0D||g'`
  56. done < <(cat -)
  57. else
  58. echo "STDIN is empty" >&2
  59. exit 1
  60. fi
  61. fi
  62. if [ -z "${MESSAGE}" ] ; then
  63. echo "You need to specify the message to send" >&2
  64. display_help >&2
  65. exit 1
  66. fi
  67. if ! which which > /dev/null 2>&1 ; then
  68. echo "which is not installed"
  69. exit 1
  70. elif which curl > /dev/null 2>&1 ; then
  71. echo using curl
  72. BIN="curl -i"
  73. elif which wget > /dev/null 2<&1 ; then
  74. echo using wget
  75. BIN="wget --save-headers -qO -"
  76. elif true ; then
  77. echo "None of curl or wget installed" >&2
  78. exit 1
  79. fi
  80. envoi=$(${BIN} "https://smsapi.free-mobile.fr/sendmsg?user=${NOM}&pass=${PASS}&msg=${MESSAGE}" 2>&1)
  81. retour_HTTP=$(echo "${envoi}" | awk '/HTTP/ {print $2}')
  82. case $retour_HTTP in
  83. 200)
  84. echo "Le SMS a été envoyé sur votre mobile."
  85. exit 0
  86. ;;
  87. 400)
  88. echo "Un des paramètres obligatoires est manquant.";;
  89. 402)
  90. echo "Trop de SMS ont été envoyés en trop peu de temps.";;
  91. 403)
  92. echo "Le service n'est pas activé sur l'espace abonné, ou login / clé incorrect.";;
  93. 500)
  94. echo "Erreur côté serveur. Veuillez réessayer ultérieurement.";;
  95. esac
  96. exit 1