autoflash.sh 2.8 KB

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