0

I am trying to create upload.sh that lets me upload multiple files via SFTP doing something like this:

upload.sh file1 file2 file3
upload.sh subfolder/*

In each case, the user should be prompted to enter the remoteFS password.

It might look like this:

ls -1 $*  > tmp.txt
sftp -b tmp.txt root@1.2.3.4:/remotefs/path/to/html/

The above isn't correct, because tmp.txt would contain a list of filenames, and it should be containing a list of sftp put foo/file.bar commands.

But even if I get that working (at the moment I'm simulating by manually creating a tmp.txt file), I am running into this problem: How to send password using sftp batch file

i.e. Rather than sftp requesting a password, it is just failing.

Is there any way to force it to request a password?

Storing the password inside the .sh would be bad practice, and I don't want to involve SSH keys at this point (I'm using this scenario to demonstrate shell-scripting to a maths student, and I don't want to escalate the complexity).

Community
  • 1
  • 1
P i
  • 29,020
  • 36
  • 159
  • 267

1 Answers1

1

Try this:

#!/bin/bash

[[ ${#} -eq 0 ]] && exit 1

(
  echo "cd /remotefs/path/to/html";
  for file in "$@"; do
    echo "put '$file'"
  done
) | sftp root@1.2.3.4
Cyrus
  • 84,225
  • 14
  • 89
  • 153