【问题标题】:Pipe Python script to ssh, but do other bash commands first将 Python 脚本通过管道传输到 ssh,但先执行其他 bash 命令
【发布时间】:2015-03-01 02:04:19
【问题描述】:

在远程服务器上执行 Python 脚本的一种非常方便的方法是 pipe it to ssh:

cat script.py | ssh user@address.com python -

- 似乎是可选的。

如何在以这种方式运行 Python 脚本之前执行其他 bash 命令?

这不起作用:

cat script.py | ssh user@address.com "cd ..; python -" # WRONG!

有趣的是,这会发送一个不确定性损坏的 Python 脚本版本,每次运行时都会在不同的地方给出语法错误!

【问题讨论】:

  • 提示:如果您使用此方法,请使用python -u 进行无缓冲输出,以便打印后立即显示stdout。
  • 损坏总是发生在前几行吗?
  • @YatharthROCK 我不记得了,抱歉。

标签: python bash ssh remote-execution


【解决方案1】:

你可以创建一个子shell:

cat script.py | ssh user@address.com "(cd ..; python -)"

或临时文件:

cat script.py | ssh user@address.com "tee >/tmp/tmp.py; cd ..; python /tmp/tmp.py; rm /tmp/tmp.py"

【讨论】:

  • 谢谢,不知道如何捕获标准输入。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 2020-10-08
  • 2012-12-30
相关资源
最近更新 更多