Uploading files to FTP/SFTP using CURL

Hello,

Today I am writing below article which can help you to upload files to SFTP/FTP by using CURL.

Ok why we need that ? Let me tell explain!!

How we login into SFTP/FTP ?





[root@virt03 test]# sftp 192.168.56.110
Connecting to 192.168.56.110...
root@192.168.56.110's password:
sftp> ls
anaconda-ks.cfg       nodes                 post-install          post-install.log

sftp> exit

and uploading files with put command. Its a lengthy way. So recently I have gone through few articles and with some R&D I have modified it as script and command-line argument support.So you can call the script with filename as argument. 

So Lets do this!!!

  1. Command 1 : This is for uploading a single to SFTP/FTP by using CURL. 
SFTP

curl -k  -u virt03:virt03 -T file4  sftp://192.168.56.110/home/virt03/

Syntax : 

curl -k  -u username:password -T filename sftp://IP_Addreess OR Hostname:/path/to/upload 

FTP

curl -k  -u virt03:virt03 -T file6  ftp://192.168.56.110

Example


[root@virt03 test]# curl -k  -u virt03:virt03 -T file4  sftp://192.168.56.110/home/virt03/


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current


                                 Dload  Upload   Total   Spent    Left  Speed


  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0


[root@virt03 test]# touch file6


[root@virt03 test]# curl -k  -u virt03:virt03 -T file6  ftp://192.168.56.110


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current


                                 Dload  Upload   Total   Spent    Left  Speed


  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0





[root@virt03 test]# ls /home/virt03/ | grep file


file1


file2


file3


file4


file5


file6


[root@virt03 test]#



So hope you understand whats happening now. Lets make some script!!! 



[root@virt03 test]# ls


file1  file3  file5  file7  file9  lab2.sh     upload1.sh


file2  file4  file6  file8  lab1   rackspace3  upload2.sh


[root@virt03 test]# ls /home/virt03/ | grep file


file1


file2


file3


file4


file5


file6


[root@virt03 test]# ./upload1.sh file7


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current


                                 Dload  Upload   Total   Spent    Left  Speed


  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0


[root@virt03 test]# ls /home/virt03/ | grep file


file1


file2


file3


file4


file5


file6


file7


[root@virt03 test]# ./upload2.sh file8 file9


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current


                                 Dload  Upload   Total   Spent    Left  Speed


  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0


  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0


[root@virt03 test]# ls /home/virt03/ | grep file


file1


file2


file3


file4


file5


file6


file7


file8


file9


[root@virt03 test]# 

And the scripts are 






[root@virt03 test]# tail upload*


==> upload1.sh <==


#!/bin/bash





curl -k  -u virt03:virt03 -T "{$1}" sftp://192.168.56.110/home/virt03/





==> upload2.sh <==


#!/bin/bash


curl -k  -u virt03:virt03 -T "{$1,$2}" sftp://192.168.56.110/home/virt03/


[root@virt03 test]#


I hope you can understand what the scripts are doing. To upload more than 2 files you can use for loop like below.






for i in file1 file2 file3 file4 file5


do


./upload1.sh $i


done


Example: 



[root@virt03 test]# for i in file10 file11 file12


> do


> ./upload1.sh $i


> done


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current


                                 Dload  Upload   Total   Spent    Left  Speed


  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current


                                 Dload  Upload   Total   Spent    Left  Speed


  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current


                                 Dload  Upload   Total   Spent    Left  Speed


  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0





[root@virt03 test]# ls /home/virt03/ | grep file1


file1


file10


file11


file12


[root@virt03 test]#


Hope you understand!! , If any doubts please dont hesitate to ask.









0 Comments