如何在同一个shell命令中打开sftp连接并执行sftp命令

ibe*_*ski 6 linux ssh bash sftp

是否有可能打开到远程服务器的 sftp 连接并在 shell 的同一行中执行 sftp 命令。

像这样的东西:

sftp user@example.com && put /tmp/test.txt /tmp/
Run Code Online (Sandbox Code Playgroud)

其中 put 是 sftp 命令。

我的想法是将文件从本地服务器从 bash 脚本放到远程服务器,其中远程服务器上只允许使用 sftp。

Sve*_*ven 9

方法一:

echo "put /tmp/test.txt /tmp/" | sftp user@example.com
Run Code Online (Sandbox Code Playgroud)

方法二:

使用-b选项:

-b 批处理文件

批处理模式从输入批处理文件而不是标准输入中读取一系列命令。由于它缺乏用户交互,因此应与非交互式身份验证结合使用。-' may be used to indicate standard input. sftp will abort if any of the following commands fail: get, put, rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp, lpwd, df, symlink, and lmkdir. Termination on error can be suppressed on a command by command basis by prefixing the command with a-' 字符的批处理文件(例如,-rm /tmp/blah*)。