Push Multiple Files on sftp

Guys,
I need your help, I am creating a shell script for sftp connection and sending multiple files.

When I am running this shell manually it is working fine and putting both the files:


'''''''''''''''''''''''
#!/usr/bin/expect
spawn sftp UserID@host
expect "password:"
send Pass@123\n
expect "sftp>"
send "cd Daily_Data\n"
expect "sftp>"
send "put /MAPSIDX/import_dire/MIS/ecom*zip\n"
send "put /MAPSIDX/import_dire/MIS/prem*zip\n"
expect "sftp>"
send "exit\n"
'''''''''''''''''''''''''''''''''

But when I am putting this shell with others in scheduler getting only one file transferred instead of two:

''''''''''''''''''''''''''''''''''''
spawn sftp UserID@host
-----------------------------------------------------------------------
UNAUTHORISED ACCESS IS PROHIBITED!!
-----------------------------------------------------------------------
UserID@host's password: 
Connected to host.
sftp> cd Daily_Data
sftp> put /MAPSIDX/import_dire/MIS/ecom*zip
Uploading /MAPSIDX/import_dire/MIS/ecom15Feb2021.zip to /bfslbilling/Campaign-Data/Daily_Data/ecom15Feb2021.zip

/MAPSIDX/import_dire/MIS/ecom15Feb2021.zip      0%    0     0.0KB/s   --:-- ETA
/MAPSIDX/import_dire/MIS/ecom15Feb2021.zip    100% 6474KB  39.1MB/s   00:00    
sftp> put /MAPSIDX
'''''''''''''''''''''

It is getting exited midway. Can you please suggest.

Thanks,
Amit

Maybe search the forums for this issue, which has been addressed many times over the years?

Hint:

Normally we use sshpass to authenticate, not expect scripts.

Hi @Amitzapak,

using sshpass and a here document, you could try a simplified version without expect:

#!/bin/bash

sshpass -p pass sftp -o BatchMode=no -b- user@host << EOFTP
cd Daily_Data
put /MAPSIDX/import_dire/MIS/ecom*zip
put /MAPSIDX/import_dire/MIS/prem*zip
EOFTP

Better than using a password is creating a private/public key pair.

Hi,

I strongly agree with bendingrodriguez here. As they have said, the solution that's going to work best for you, if it's at all possible for you to use it, is SSH key-based authentication.

The idea behind this is that the connecting account's public key on the client is added to a list of authorized keys which are allowed to connect to the destination account on the server. Once that is in place, any attempts by the client to connect to the server as that specific user, using that specific key, no longer require a password, with the key exchange functioning as an authentication mechanism.

So whilst you can write an expect script to do this, or find various other creative and harder-to-maintain ways to resolve this issue, if you want things to work as easily and reliably as possible, without the headache of having to maintain an ever-growing contraption of your own construction to keep the connection working in the future, then SSH keys are 100% the best way to go here.

Yes, it's always best to set up password-less SSH using private/public key pairs when you have a "relationship" between the SSH client and the SSHD host.

On top of that, I would recomend to use different keys for different purposes.
Be aware that you can name them upon creation, so they do not overwrite the default file.