etherwake.init 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2009 OpenWrt.org
  3. NAME='etherwake'
  4. START=60
  5. PROGRAM=''
  6. start()
  7. {
  8. local searchlist=''
  9. local section=''
  10. local value=''
  11. config_load "${NAME}"
  12. # check for available program
  13. config_get searchlist 'setup' 'pathes'
  14. PROGRAM=$(search_program "${searchlist}")
  15. [ -z "${PROGRAM}" ] && {
  16. echo "${initscript}: No ${NAME} program installed. Check: opkg list | grep ${NAME}"
  17. exit 1
  18. }
  19. # sudo
  20. config_get_bool value 'setup' 'sudo' '0'
  21. [ "${value}" -ne 0 ] && PROGRAM="sudo ${PROGRAM}"
  22. # interface
  23. config_get value 'setup' 'interface'
  24. [ -n "${value}" ] && append PROGRAM "-i ${value}"
  25. # broadcast
  26. config_get_bool value 'setup' 'broadcast' '0'
  27. [ "${value}" -ne 0 ] && append PROGRAM '-b'
  28. # wake up targets
  29. config_foreach etherwake_start target $*
  30. }
  31. etherwake_start()
  32. {
  33. local section="$1"
  34. shift
  35. local names="$*"
  36. local value=''
  37. local target=''
  38. if [ -z "${names}" ]
  39. then
  40. # check if boot target
  41. config_get_bool value "${section}" 'wakeonboot' '0'
  42. [ "${value}" -eq 0 ] && return 0
  43. # wake up target
  44. do_etherwake "${section}"
  45. return $?
  46. else
  47. # name
  48. config_get value "${section}" 'name'
  49. [ -z "${value}" ] && return 0
  50. for target in ${names}
  51. do
  52. [ "${value}" != "${target}" ] && continue
  53. # wake up target
  54. do_etherwake "${section}"
  55. return $?
  56. done
  57. fi
  58. }
  59. # execute etherwake command for target
  60. do_etherwake()
  61. {
  62. local section="$1"
  63. local value=''
  64. local password=''
  65. local args=''
  66. # password
  67. config_get value "${section}" 'password'
  68. [ -n "${value}" ] && {
  69. password=$(etherwake_password "${value}")
  70. append args "-p ${password}"
  71. }
  72. # mac address
  73. config_get value "${section}" 'mac'
  74. [ -z "${value}" ] && { echo "${initscript}: Target ${section} has no MAC address"; return 1; }
  75. append args "${value}"
  76. # name
  77. config_get value "${section}" 'name'
  78. [ -z "${value}" ] && value="{section}"
  79. # execute command
  80. echo "${initscript}: Waking up ${value} via ${PROGRAM}${args:+ ${args}}"
  81. ${PROGRAM} ${args}
  82. return $?
  83. }
  84. # find first available program from searchlist
  85. search_program()
  86. {
  87. local searchlist="$1"
  88. local test=''
  89. local program=''
  90. for test in ${searchlist} ; do
  91. [ -x "${test}" ] && {
  92. program="${test}"
  93. break;
  94. }
  95. done
  96. [ -n "${program}" ] && echo "${program}"
  97. return
  98. }
  99. # prepare hex password
  100. etherwake_password()
  101. {
  102. local delimiter=':'
  103. local password=`echo "$1" | sed "s/../&${delimiter}/g"`
  104. echo "${password%${delimiter}}"
  105. return
  106. }