如何通过SFTP+输入密码批量上传?

How to batch-upload via SFTP + entering password?

我正在尝试创建 upload.sh 让我可以通过 SFTP 上传多个文件,就像这样:

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

在每种情况下,都应提示用户输入 remoteFS 密码。

它可能看起来像这样:

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

以上不正确,因为 tmp.txt 将包含文件名列表,它应该包含 sftp put foo/file.bar 命令列表。

但即使我能够正常工作(目前我正在通过手动创建 tmp.txt 文件进行模拟),我 运行 遇到了这个问题:How to send password using sftp batch file

即而不是 sftp 请求密码,它只是失败了。

有没有办法强制要求输入密码?

将密码存储在 .sh 中是一种不好的做法,此时我不想涉及 SSH 密钥(我正在使用此方案向数学学生演示 shell-脚本,而且我不想增加复杂性)。

试试这个:

#!/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