auto_privisioning.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. CONFIGDIR="/oem/.record"
  3. SYSTEMCONF="/etc/speaker.conf"
  4. FIRMWARECONF="${CONFIGDIR}/firmware_url"
  5. action="$1"
  6. address="$2"
  7. type="$3"
  8. [ -z "${action}" ] && echo "Usage:$0 dhcp|static" && exit 1
  9. [ ! -d ${CONFIGDIR} ] && mkdir -p ${CONFIGDIR}
  10. if [ ! -f ${FIRMWARECONF} ];then
  11. touch ${FIRMWARECONF}
  12. cat << END > ${FIRMWARECONF}
  13. [firmware]
  14. url=test
  15. END
  16. fi
  17. [ ! -d ${CONFIGDIR} ] && mkdir -p ${CONFIGDIR}
  18. get_mac() {
  19. real_model="`/etc/scripts/getmodel.sh`"
  20. if [ "foo${real_model}" = "fooX10" -o "foo${real_model}" = "fooX10_V2" ];then
  21. interface="br0"
  22. else
  23. interface="eth0"
  24. fi
  25. echo "`ifconfig ${interface} | grep HWaddr | awk '{print $5}' | tr -d ':' | tr -d ' '`"
  26. }
  27. handle_conf() {
  28. /etc/scripts/conf_handle.sh export_${format}
  29. res="`/etc/scripts/import_compare.sh ${CONFIGDIR}/new.json /tmp/conf`"
  30. if [ ! -z "${res}" ];then
  31. cp ${CONFIGDIR}/new.json /tmp/conf
  32. res="`/etc/scripts/conf_handle.sh import`"
  33. if [ -z "${res}" ];then
  34. rm -rf ${CONFIGDIR}/new.json
  35. echo "Auto privisioning Successfully"
  36. if [ "foopnp" != "foo${type}" ];then
  37. /etc/scripts/shell_action.sh showReboot
  38. sleep 1
  39. /sbin/reboot
  40. fi
  41. else
  42. echo "Import configuration failed"
  43. rm -rf ${CONFIGDIR}/new.json
  44. exit 1
  45. fi
  46. else
  47. rm -rf ${CONFIGDIR}/new.json
  48. echo "Configuration same as local, dont need apply"
  49. exit 0
  50. fi
  51. }
  52. mac="`get_mac`"
  53. model="`sysconf ${SYSTEMCONF} get system oem_model`"
  54. [ -z "${model}" ] && model="`sysconf ${SYSTEMCONF} get system model`"
  55. softwareVersion="`sysconf ${SYSTEMCONF} get system firmware`"
  56. [ -z "${mac}" ] && echo "Please check network interface" && exit 1
  57. format="`sysconf ${SYSTEMCONF} get auto_privisioning format`"
  58. [ -z "${format}" ] && echo "Please check auto privisioning format" && exit 1
  59. if [ "foo${action}" = "foodhcp" ];then
  60. [ -z "${address}" ] && echo "Not found http url or tftp server" && exit 1
  61. result=$(echo ${address} | grep "://")
  62. if [ -z ${result} ];then
  63. mode="tftp"
  64. else
  65. mode="http"
  66. final_char="${address: -1}"
  67. if [ "foo${final_char}" = "foo/" ];then
  68. address="${address%?}"
  69. fi
  70. fi
  71. else
  72. mode="`sysconf ${SYSTEMCONF} get auto_privisioning mode`"
  73. address="`sysconf ${SYSTEMCONF} get auto_privisioning address`"
  74. [ -z "${address}" -o -z "${mode}" ] && echo "Need http url and mode" && exit 1
  75. final_char="${address: -1}"
  76. if [ "foo${final_char}" = "foo/" ];then
  77. address="${address%?}"
  78. fi
  79. fi
  80. if [ "foo${mode}" = "footftp" ];then
  81. #get config file from tftp server
  82. tftp -g -l ${CONFIGDIR}/new.json -r ${mac}.${format} ${address}
  83. retcode="$?"
  84. [ "foo${retcode}" = "foo1" ] && echo "Failed download file form tftp server" && exit 1
  85. handle_conf
  86. elif [ "foo${mode}" = "foohttp" ];then
  87. #get config file from http server
  88. #"Zycoo-X10 v_s1.2.5 68692E290C5D"
  89. user_agent="Zycoo-${model} ${softwareVersion} ${mac}"
  90. if [ "foopnp" = "foo${type}" ];then
  91. retcode="`/usr/bin/curl -o ${CONFIGDIR}/new.json -k --user-agent "${user_agent}" --connect-timeout 3 -w %{http_code} ${address}`"
  92. else
  93. retcode="`/usr/bin/curl -o ${CONFIGDIR}/new.json -k --user-agent "${user_agent}" --connect-timeout 3 -w %{http_code} ${address}/${mac}.${format}`"
  94. fi
  95. if [ "foo${retcode}" = "foo200" ];then
  96. handle_conf
  97. else
  98. rm -rf ${CONFIGDIR}/new.json
  99. exit 1
  100. fi
  101. fi
  102. exit 0