123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #!/bin/bash
-
- file=''
- host=''
- debug=false
-
- while [[ $# -gt 0 ]]
- do
- key="$1"
- case $key in
- --file)
- if [[ -z "$2" ]]; then
- echo "Option --file requires a file path as argument, specify the file you want to upload to the server"
- exit 1
- fi
- file="$2"
- shift
- shift
- ;;
- --debug)
- debug=true
- shift
- ;;
- -v)
- debug=true
- shift
- ;;
- --host)
- if [[ -z "$2" ]]; then
- echo "Option --host requires a host name as argument, specify the hostname of the server where the upload.php script is located"
- exit 1
- fi
- host="$2"
- shift
- shift
- ;;
- -h|--help)
- echo "Options:"
- echo "--file <file path> - Specify the file to upload to the server"
- echo "--host <host name> - Specify the hostname of the server where the upload.php script is located"
- echo "-h, --help - Show this help message"
- exit 0
- ;;
- *)
- if [[ $key == -* ]]; then
- echo "Unknown option: $key"
- else
- echo "Unknown command: $key"
- fi
- echo "Run the script with -h or --help for a list of options"
- exit 1
- ;;
- esac
- done
-
- if [[ -z $file || -z $host ]]; then
- echo "Both options --file and --host are required"
- exit 1
- fi
-
- if ! [ -x "$(command -v curl)" ]; then
- echo 'Error: curl is not installed.' >&2
- echo 'You can install it by running: sudo apt-get install curl' >&2
- exit 1
- fi
-
- if ! [ -x "$(command -v jq)" ]; then
- echo 'Error: jq is not installed.' >&2
- echo 'You can install it by running: sudo apt-get install jq' >&2
- exit 1
- fi
-
- echo "Uploading file, please wait..."
-
- # Upload the file using curl
- curl_command="curl -s -X POST -F \"uploaded_file=@$file\" https://$host/upload.php -w \"%{http_code}\""
- response=$(eval $curl_command)
-
- if $debug
- then
- echo "Executing the following curl command: $curl_command"
- echo "Response is: $response"
- fi
-
- debug_info=$(curl -s -X POST -F "uploaded_file=@$file" https://$host/upload.php)
-
- http_status=${response: -3}
- response=${response%???}
-
- if [[ $http_status -eq 403 ]]; then
- echo -e "\e[91mError: 403 Forbidden\e[0m"
- echo -e "\e[91mThe server did not allow the file to be uploaded\e[0m"
- debug_info=$(echo "$debug_info" | sed -r 's/Reference #/Reference #/g' | sed -r 's/././g')
- reference=$(echo "$debug_info" | grep -oP 'Reference #\K[^<]+')
- echo -e "\e[91mReference ID is: $reference\e[0m"
- exit 1
- elif [[ $http_status -eq 404 ]]; then
- echo -e "\e[91mError: 404 Not Found\e[0m"
- echo -e "\e[91mThe upload.php script could not be located on the server\e[0m"
- debug_info=$(echo "$debug_info" | sed -r 's/Reference #/Reference #/g' | sed -r 's/././g')
- reference=$(echo "$debug_info" | grep -oP 'Reference #\K[^<]+')
- echo -e "\e[91mReference ID is: $reference\e[0m"
- exit 1
- fi
-
- success=$(echo $response | jq -r '.success')
- error=$(echo $response | jq -r '.error')
- file_path=$(echo $response | jq -r '.file_path')
-
- if [[ $success != "null" ]]; then
- echo -e "\e[92mSuccess: $success\e[0m"
- echo -e "\e[92mThe file was successfully uploaded and was stored as $file_path\e[0m"
- else
- echo -e "\e[91mError: $error\e[0m"
- fi
|