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.

Did you consider using the -b (batchfile) option?

Would you also consider something equivalent to:-

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

im still stuck on this. here's the script i have:

                        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?

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

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.

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.

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 :b:

Thanks,
R. Singh