You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SSH及标准Linux工具直接用远程二进制处理本地文件?

当然可以!用SSH管道直接流式处理本地文件

完全不用来回拷贝文件,借助SSH的管道能力和标准Linux工具,就能直接让远程程序处理本地文件,结果直接传回本地。下面分几种常见场景给出具体方案:

场景1:远程程序支持从标准输入(stdin)读取数据

这是最简单的情况,直接通过管道把本地文件内容传给远程程序,处理后的输出再导回本地:

cat local-input.txt | ssh your-user@remote-server '/path/to/your-remote-program' > local-output.txt

或者更简洁的写法(用输入重定向替代cat):

ssh your-user@remote-server '/path/to/your-remote-program' < local-input.txt > local-output.txt

原理:本地文件的内容通过SSH的标准输入流发送到远程,远程程序读取这个流进行处理,处理后的标准输出再通过SSH传回本地,重定向到输出文件。

场景2:远程程序必须接收文件名作为参数(不读stdin)

有些程序设计成只能处理指定路径的文件,这时候可以用/dev/stdin作为远程程序的参数,让它读取标准输入的内容:

ssh your-user@remote-server '/path/to/your-remote-program /dev/stdin' < local-input.txt > local-output.txt

如果程序需要额外参数,直接追加在命令里即可:

ssh your-user@remote-server '/path/to/your-remote-program --verbose --output-format csv /dev/stdin' < local-input.txt > local-output.csv

场景3:需要处理多个本地输入文件

如果远程程序要同时处理多个本地文件,可以用tar打包后流式传输到远程,临时解压处理后再返回结果:

tar -cf - file1.txt file2.txt | ssh your-user@remote-server '
  mkdir -p /tmp/tmp-processing && cd /tmp/tmp-processing && 
  tar -xf - && 
  /path/to/your-remote-program file1.txt file2.txt && 
  rm -rf /tmp/tmp-processing
' > local-combined-output.txt

这个命令会:

  • 本地把多个文件打包成tar流(-表示输出到stdout)
  • 通过SSH把tar流传到远程,创建临时目录并解压
  • 远程程序处理解压后的文件
  • 处理完成后删除临时目录,避免残留文件
  • 结果通过SSH传回本地,保存为输出文件

额外小贴士

  • 如果远程服务器用非默认SSH端口,加上-p参数:ssh -p 2222 your-user@remote-server ...
  • 要是需要sudo执行远程程序,可以用ssh your-user@remote-server 'sudo /path/to/your-remote-program /dev/stdin' < local-input.txt > local-output.txt(建议配置免密sudo,避免交互输入密码)

内容的提问来源于stack exchange,提问作者Vladislavs Dovgalecs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:03:39