Sftp script to get multiple files at the same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sftp script to get multiple files at the same time
# 1  
Old 12-20-2016
Sftp script to get multiple files at the same time

i have to log into an sftp server to get multiple files.

im typing this post from a remote location and i dont have the script i wrote with me. but i got the sftp script to work by logging into the sftp server file by file. meaning, sftp to the server, "mget -p" or "get -p" one file at a time.

this obviously is not efficient.

i tried issuing the command "mget -p file1.txt file2.txt file3.txt". this doesnt seem to work. it can only get one file at a time apparently.

is there something im doing wrong here? id like to be able to get multiple files at once without having to sftp to the server for each file.
# 2  
Old 12-20-2016
Did you consider using the -b (batchfile) option?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-20-2016
Would you also consider something equivalent to:-
Code:
mget file*.txt

I don't recognise the -p flag. The nearest I know is the -P flag of get which specified a local path to get the file from.

If the above doesn't help (and you don't want all the files) then fire off sftp and get a listing of the files back, then use this to loop round and get the ones you want. Using the -b flag of sftp as suggested by RudiC is probably the best option however you choose to get the files, and far better than manual commands or using a here document (essentially coded in-line input to a command)

Would you care to share what you have created so far and we can suggest improvements? I'm assuming that you have SSH keys generated and shared so that you have password-less authentication for the account you connect to.



Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 12-20-2016
im still stuck on this. here's the script i have:

Code:
                        echo "${myedir}${myefile}"
                        export nserver="${HOSTLIST}"
                        export username="${USERSNAME}"
                        export usepas="$NUPSSWD"
                        export remotecom="${REMOCOM}"
                        export filetograb="${myedir}${myefile}"
                        /usr/bin/expect -c '
                                set timeout 30
                                spawn sftp $env(username)@$env(nserver)
                                expect {
                                        "*password*" {
                                                        send "$env(usepas)\r"
                                        }
                                        "*yes/no*" {
                                                        send "yes\r" ; exp_continue
                                        }
                                        "*password:*" {
                                                        send "$env(usepas)\r"
                                        }
                                }
                                set prompt "sftp"
                                expect -re $prompt
                                        send -- "$env(remotecom) $env(filetograb)\r"
                                expect -re $prompt
                                        send -- "quit \r"
                                expect {
                                        "*Permanently added*" {
                                                                exp_continue
                                        }
                                        "assword:" {
                                                        send "$env(usepas)\r"
                                        }
                                }
                        '

how do i incorporate heredoc into that?

i tried this other script, which works, but i have to enter the password manually. how do i incorporate the below heredoc into the above?

Code:
sftp f-dream1@myhost.com sftp://myhost.com << --EOF--
cd $directory
get $srcfile
ls
quit
--EOF--

# 5  
Old 12-20-2016
Hello SkySmart,

In case password less scp is allowed in your environment. Then you could get rid of expect by simply doing password less sftp to another server. Here are few links which could help you in same too.
Use SSH Keys for Password-less SFTP Logins - easyPress - Made in Canada

EDIT: I have just tried in my test server as follows are the steps on same.

i- I have made password less sftp as mentioned above.
ii- Now run following script to get files from source server or put files into target server.
Code:
cat script.ksh
stp username@server_name << ENDSFTP
cd /home/singh
get *.txt
quit
ENDSFTP

So above code has logged into my target box and took the files(which have .txt extensions) with them to current directory successfully.
iii- Now let's take an example of putting the files to a target server as follows.
Code:
cat script.ksh
sftp user_name@server_name << ENDSFTP
cd /home/Singh_is_King
put *.xls
quit
ENDSFTP

I hope this helps you. Enjoy learning Smilie

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-21-2016 at 12:12 AM.. Reason: Added few examples of get and put of sftp successfully now for OP.
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename (move) multiple files on remote server using sftp

I want to rename (move) multiple files on remote server. I tried the following command to move all TXT files from my_dir directory to /new_dir. But it does not work. Any help? #!/bin/ksh sftp -dev3 << ABC cd my_dir $(for i in TXT; do echo "ls *.$i" ; rename $x /new_dir/$x;... (1 Reply)
Discussion started by: Soham
1 Replies

2. Shell Programming and Scripting

SFTP Multiple files

Hi :), I am new to Unix and Shell Script I have a urgent requirement, where i am new to shell script and Unix my requirement is I have a files in a folder like sales-prod-India-details.txt sales-prod-japan-details.txt sales-prod-china-details.txt My SFTP server has ... (3 Replies)
Discussion started by: spradeep86
3 Replies

3. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

4. UNIX for Dummies Questions & Answers

Sftp multiple files in single command

Hi All, I would like to sftp 2 files with a single command. I tried the below options, sftp suer@test13:"/u01/home/oracle/SetDb.sh /u01/home/oracle/.profile" ./ But what actually happens is Fetching /u01/home/oracle/SetDb.sh to /u01/home/oracle/.profile /u01/home/oracle/SetDb.sh ... (3 Replies)
Discussion started by: sid2013
3 Replies

5. Shell Programming and Scripting

Ftp multiple files one at a time

Hi, I have a problem while ftp'ing zip files one after the other from linux source directory to a ftp host machine. here is the shell script: #!/bin/ksh dir=data/dir1/dir2 # this dir is linux source directory where zip files located. rmtdir='/home/' echo $dir for i in /$dir/*; do if ;... (7 Replies)
Discussion started by: raj78
7 Replies

6. Shell Programming and Scripting

Shell Script to delete files within a particular time frame under multiple sub folders

Greetings! I'm looking for starting information for a shell script. Here's my scenario: I have multiple folders(100) for example: /www/test/applications/app1/logs /www/test/applications/app2/logs Within these folders there are log files files that need to be deleted after a month. ... (3 Replies)
Discussion started by: whysolucky
3 Replies

7. Shell Programming and Scripting

ftp multiple files at the same time

Hi All, I am using ncftpput in one of my c-shell script to upload files to a remote location. The source files keep coming continuosly and to upload each file ncftpput opens a new connection everytime. It means ncftp uploads the file1 than file2 than file3 .... There is gap 20-25 secs between... (10 Replies)
Discussion started by: sraj142
10 Replies

8. Shell Programming and Scripting

help to upload multiple files through SFTP

Hi Experts, Please help me to write the expect script for uploading multiple files in one shot . Below is my program that I have written. #!/usr/local/bin/expect -f #/home/kulbhushan/sftp_prog.sh # procedure to attempt connecting; result 0 if OK, 1 otherwise proc connect {passw} { expect... (1 Reply)
Discussion started by: kulbhushan
1 Replies

9. Solaris

how to get multiple files using sftp from a windows server

I need to get multiple files from a windows server to a solaris server using sftp, I tried it but only can get one file at a time ( I'm unable to use a wild card character using sftp) hoe do i do this. any light on this is appreciated. Ram. (3 Replies)
Discussion started by: ramky79
3 Replies

10. UNIX for Dummies Questions & Answers

sftp multiple files and subdirectories

Hi, I'm new to using the sftp command prompt within the unix shell (usually I just use an FTP windows program). I've successfully connected to a remote server from within ssh. I've also been able to copy multiple files using the mget command e.g. mget *.html How do I copy all files,... (3 Replies)
Discussion started by: robbieg
3 Replies
Login or Register to Ask a Question