Disasterkid Disasterkid -4 years ago 72
Bash Question

Where to put <<EOF when looping using sshpass?

I am using the following script to loop through all files in a local folder and upload them to an

sftp
location using
sshpass
. I have already tried the
put
statement to upload a single file. But I somehow have problem on where to put my
<<EOF
and
EOF
correctly? Or is it something else missing?

#!/bin/bash

FILES= /MY_LOCAL_FOLDER_PATH/*
sshpass -p 'MY_PASSWORD' sftp -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-dss MY_USERNAME@FTP_URL:/Import
<<EOF
for f in $FILES
do
chmod +X $f
put $f
done
EOF


I put the
chmod
there because I got "permission denied" on my files.

Answer Source

If you want to generate the put commands for sftp as input, you'll have to pipe them. You can't process loops within a here-document. Also, you shouldn't use sshpass -p, as the program's command line is visible to all processes on the system via proc and ps, even those of other users. (very briefly, but visible anyway.) Environment variables are only visible to the same user, though in general, ssh keys might still be preferable.

Something like this might work:

#!/bin/bash
export SSHPASS=MY_PASSWORD
for f in /path/* ; do 
    echo put "$f"
done | sshpass -e sftp user@whereever:/path
unset SSHPASS

Any commands inside the loop are executed locally, anything you want to pass to sftp as commands should be printed via echo or such. (Of course, the commands shouldn't print anything that's not supposed to go to sftp.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download