【发布时间】:2019-12-20 05:59:38
【问题描述】:
我想在远程服务器上运行本地 python 脚本使用 Posh-SSH,它是 Powerhsell 中的一个模块。 这个topic 通过执行以下操作在常规 ssh 中提及它:
Get-Content hello.py | ssh user@192.168.1.101 python -
我想在这里使用 Posh-SSH 模块,但我不知道如何实现它...
我尝试过这样的事情,但它不起作用
Invoke-SSHCommand -Index $sessionid.sessionid -Command "$(Get-Content hello.py) | python3 -u -" -ErrorAction Stop
编辑
没有显示错误,只是卡在上面,什么也不做......
EDIT2
好的,我现在明白了,为什么我在发送多行 py 文件时会出现此错误。
新行不会被重新转录,看看什么命令将被发送到远程服务器:
python3 -u - <<"--ENDOFPYTHON--"
chevre="basic" print("Hello, World!")
--ENDOFPYTHON--
而不是:
python3 -u - <<"--ENDOFPYTHON--"
chevre="basic"
print("Hello, World!")
--ENDOFPYTHON--
EDIT3
终于完成了! 多亏了这个topic,我执行了将空格更改为换行符的操作。 就为了这个
( (Get-Content hello.py) -join "`r`n")
而不是简单的
$(Get-Content hello.py)
最后一行是:
$cmd = "python3 -u - <<`"--ENDOFPYTHON--`"`n$((Get-Content $pyscript) -join "`r`n")`n--ENDOFPYTHON--"
Invoke-SSHCommand -Index $sessionid.sessionid -Command $cmd -ErrorAction Stop
也别忘了删除线
#!/usr/bin/env python3
如果存在于您的 py 文件之上,否则它将无法工作。
【问题讨论】:
-
您是否遇到任何错误?我们需要更多信息来提供帮助
-
没有错误...只是卡在那里,就是这样。
标签: python powershell ssh posh-ssh