autoflash.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. source ./config
  3. function quit() {
  4. if [ x"${BASH_SOURCE[0]}" == x"$0" ]; then
  5. exit $*
  6. else
  7. return $*
  8. fi
  9. }
  10. function curl_admin() {
  11. curl -fsS --basic -u admin:admin $@
  12. }
  13. # download missing firmware images
  14. models="tp-link-tl-wr841n-nd-v8 tp-link-tl-wr841n-nd-v9"
  15. models="${models} tp-link-tl-wdr3500-v1"
  16. models="${models} tp-link-tl-wdr3600-v1"
  17. models="${models} tp-link-tl-wdr4300-v1"
  18. for model in $models; do
  19. filename="${base_fw_name}-${model}.bin"
  20. imagefile="images/${filename}"
  21. if [ ! -r $imagefile ]; then
  22. echo -en "Downloading image for '$model' ... "
  23. wget -q "${base_fw_url}${filename}" -O "$imagefile"
  24. if [ $? -eq 0 ]; then
  25. echo "OK"
  26. else
  27. echo "ERROR"
  28. rm -f "$imagefile"
  29. echo "Failed to download firmware. Please ensure the firmware for '${base_fw_name}-${model}' is present in images/ directory."
  30. quit 3
  31. fi
  32. fi
  33. done
  34. ping -n -c 1 -W 1 192.168.0.1 > /dev/null
  35. if [ $? -ne 0 ]; then
  36. echo "ROUTER OFFLINE? cannot ping 192.168.0.1 :("
  37. quit 1
  38. fi
  39. mac=$(arp -i eth0 -a 192.168.0.1 |grep -oE " [0-9a-f:]+ " |tr -d ' ')
  40. echo "mac address: $mac"
  41. model=$(curl_admin http://192.168.0.1/ | grep -oE "WD?R[0-9]+N?")
  42. echo "found model: $model"
  43. hwver_page="http://192.168.0.1/userRpm/SoftwareUpgradeRpm.htm"
  44. hwver=$(curl_admin -e http://192.168.0.1/userRpm/MenuRpm.htm $hwver_page | grep -oE "$model v[0-9]+")
  45. echo "hw version: $hwver"
  46. uploadurl="http://192.168.0.1/incoming/Firmware.htm"
  47. image=""
  48. if [ "$hwver" = "WR841N v9" ]; then
  49. image="${base_fw_name}-tp-link-tl-wr841n-nd-v9.bin"
  50. elif [ "$hwver" = "WR841N v8" ]; then
  51. image="${base_fw_name}-tp-link-tl-wr841n-nd-v8.bin"
  52. elif [ "$hwver" = "WDR3500 v1" ]; then
  53. image="${base_fw_name}-tp-link-tl-wdr3500-v1.bin"
  54. elif [ "$hwver" = "WDR3600 v1" ]; then
  55. image="${base_fw_name}-tp-link-tl-wdr3600-v1.bin"
  56. elif [ "$hwver" = "WDR4300 v1" ]; then
  57. image="${base_fw_name}-tp-link-tl-wdr4300-v1.bin"
  58. else
  59. echo "UNKNOWN MODEL ($hwver), SORRY :("
  60. quit 2
  61. fi
  62. # prepend images/ subdirectory to filename
  63. image="images/$image"
  64. echo -en "flashing image: $image ... "
  65. curl_admin -e $hwver_page -F Filename=@$image $uploadurl > /dev/null
  66. curl_admin -e $uploadurl http://192.168.0.1/userRpm/FirmwareUpdateTemp.htm > /dev/null
  67. echo "done :)"
  68. echo -en "waiting for router to come up again "
  69. while ! ping -n -c 1 -W 2 192.168.1.1 > /dev/null; do
  70. echo -en "."
  71. sleep 1
  72. done
  73. echo " \o/"
  74. # upload authorized keys if present
  75. if [ -e authorized_keys ]; then
  76. echo -en "uploading authorized_keys ... "
  77. keys=`cat authorized_keys`
  78. curl -fsS -F cbi.submit=1 -F "cbid.system._keys._data=$keys" http://192.168.1.1/cgi-bin/luci/admin/index > /dev/null
  79. if [ $? -eq 0 ]; then
  80. echo "OK"
  81. else
  82. quit 4
  83. fi
  84. fi
  85. echo