Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

upload_file.sh 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash
  2. file=''
  3. host=''
  4. debug=false
  5. while [[ $# -gt 0 ]]
  6. do
  7. key="$1"
  8. case $key in
  9. --file)
  10. if [[ -z "$2" ]]; then
  11. echo "Option --file requires a file path as argument, specify the file you want to upload to the server"
  12. exit 1
  13. fi
  14. file="$2"
  15. shift
  16. shift
  17. ;;
  18. --debug)
  19. debug=true
  20. shift
  21. ;;
  22. -v)
  23. debug=true
  24. shift
  25. ;;
  26. --host)
  27. if [[ -z "$2" ]]; then
  28. echo "Option --host requires a host name as argument, specify the hostname of the server where the upload.php script is located"
  29. exit 1
  30. fi
  31. host="$2"
  32. shift
  33. shift
  34. ;;
  35. -h|--help)
  36. echo "Options:"
  37. echo "--file <file path> - Specify the file to upload to the server"
  38. echo "--host <host name> - Specify the hostname of the server where the upload.php script is located"
  39. echo "-h, --help - Show this help message"
  40. exit 0
  41. ;;
  42. *)
  43. if [[ $key == -* ]]; then
  44. echo "Unknown option: $key"
  45. else
  46. echo "Unknown command: $key"
  47. fi
  48. echo "Run the script with -h or --help for a list of options"
  49. exit 1
  50. ;;
  51. esac
  52. done
  53. if [[ -z $file || -z $host ]]; then
  54. echo "Both options --file and --host are required"
  55. exit 1
  56. fi
  57. if ! [ -x "$(command -v curl)" ]; then
  58. echo 'Error: curl is not installed.' >&2
  59. echo 'You can install it by running: sudo apt-get install curl' >&2
  60. exit 1
  61. fi
  62. if ! [ -x "$(command -v jq)" ]; then
  63. echo 'Error: jq is not installed.' >&2
  64. echo 'You can install it by running: sudo apt-get install jq' >&2
  65. exit 1
  66. fi
  67. echo "Uploading file, please wait..."
  68. # Upload the file using curl
  69. curl_command="curl -s -X POST -F \"uploaded_file=@$file\" https://$host/upload.php -w \"%{http_code}\""
  70. response=$(eval $curl_command)
  71. if $debug
  72. then
  73. echo "Executing the following curl command: $curl_command"
  74. echo "Response is: $response"
  75. fi
  76. debug_info=$(curl -s -X POST -F "uploaded_file=@$file" https://$host/upload.php)
  77. http_status=${response: -3}
  78. response=${response%???}
  79. if [[ $http_status -eq 403 ]]; then
  80. echo -e "\e[91mError: 403 Forbidden\e[0m"
  81. echo -e "\e[91mThe server did not allow the file to be uploaded\e[0m"
  82. debug_info=$(echo "$debug_info" | sed -r 's/Reference&#32;&#35;/Reference #/g' | sed -r 's/&#46;/./g')
  83. reference=$(echo "$debug_info" | grep -oP 'Reference #\K[^<]+')
  84. echo -e "\e[91mReference ID is: $reference\e[0m"
  85. exit 1
  86. elif [[ $http_status -eq 404 ]]; then
  87. echo -e "\e[91mError: 404 Not Found\e[0m"
  88. echo -e "\e[91mThe upload.php script could not be located on the server\e[0m"
  89. debug_info=$(echo "$debug_info" | sed -r 's/Reference&#32;&#35;/Reference #/g' | sed -r 's/&#46;/./g')
  90. reference=$(echo "$debug_info" | grep -oP 'Reference #\K[^<]+')
  91. echo -e "\e[91mReference ID is: $reference\e[0m"
  92. exit 1
  93. fi
  94. success=$(echo $response | jq -r '.success')
  95. error=$(echo $response | jq -r '.error')
  96. file_path=$(echo $response | jq -r '.file_path')
  97. if [[ $success != "null" ]]; then
  98. echo -e "\e[92mSuccess: $success\e[0m"
  99. echo -e "\e[92mThe file was successfully uploaded and was stored as $file_path\e[0m"
  100. else
  101. echo -e "\e[91mError: $error\e[0m"
  102. fi