Pequeño script para subir un archivo, borrarlo o descargarlo usando curl
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

  1. #!/bin/bash
  2. # fanta <fanta@56k.es> 03/May/2022
  3. # Script sencillo para subir un archivo a un server ftp
  4. # Permite tambien borrar un archivo y descargarlo
  5. # Ejemplos:
  6. #
  7. # Upload a file example: curlFTP -u /var/backups/vzdump-qemu-109-2022_05_03-01_47_35.vma.lzo
  8. # Remove remote file example: curlFTP -r vzdump-qemu-109-2022_05_03-01_47_35.vma.lzo
  9. # Download a file example: curlFTP -d vzdump-qemu-109-2022_05_03-01_47_35.vma.lzo
  10. user=""
  11. passwd=""
  12. ftpHost=""
  13. mode="$1" # -d -u -r
  14. file="$2"
  15. function main(){
  16. checkCurl
  17. checkFTP
  18. checkMode
  19. }
  20. function checkCurl(){
  21. [ -z "$(whereis -b curl | awk {'print$2'})" ] && echo "curl - Maybe it is not installed on the system. Sorry but I can't continue." && exit
  22. }
  23. function checkFTP(){
  24. ftpProtocol=$(curl -V | grep -i "Protocols" | tr " " "\n" | grep -w ftp)
  25. if [ $ftpProtocol != "ftp" ]; then echo "Please compile curl with ftp protocol support"; exit; fi
  26. }
  27. function checkMode(){
  28. [ -z "$mode" ] && echo -e "Please specifies an option: -d -u -r\n -d download a file\n -u upload a file\n -r remove a file" && exit
  29. if [ $mode == "-u" ] ; then checkFile; fi
  30. if [ $mode == "-u" ] ; then uploadFile ; fi
  31. if [ $mode == "-r" ] ; then removeRemoteFile ; fi
  32. if [ $mode == "-d" ] ; then downloadFile ; fi
  33. }
  34. function checkFile(){
  35. [ -z "$file" ] && echo -e "Please specifies a filename." && exit
  36. if [ -f "$file" ]; then echo "File: $file"; else echo "i can't find the file [ $file ] in that path"; exit; fi
  37. }
  38. function uploadFile(){
  39. curl -T "$file" ftp://"$user":"$passwd"@"$ftpHost"/
  40. }
  41. function downloadFile(){
  42. curl --output $file ftp://"$user":"$passwd"@"$ftpHost"/"$file"
  43. }
  44. function removeRemoteFile(){
  45. curl -u "$user:$passwd" --quote "DELE $file" ftp://"$ftpHost"
  46. }
  47. main