通过ssh向要在远程计算机上运行的python子进程传递命令

2024-09-30 20:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写python脚本,它将传递以下命令以在特定服务器中执行。

ssh server 'mongo /path/to/file --eval "print('123456\t78');"'

上面的命令被传递到脚本中的以下函数:

^{pr2}$

结果,我得到了以下输出:

the command: ssh server 'mongo /path/to/file.js --eval "print('123456\t78');"'
MongoDB shell version: 2.6.11
connecting to: test
2016-01-26T09:42:51.084+0000 SyntaxError: Unexpected token ILLEGAL

但是,当我尝试直接在服务器上执行'mongo /path/to/file.js --eval "print('123456\t78');"'时,它会很好地工作,因为我得到的结果是:

MongoDB shell version: 2.6.11
connecting to: test
123456  78
test
1
0

我怀疑,这一定与ssh server后的(单)引号有关。所以我的问题是,用特殊字符(如引号或斜杠)传递命令的最佳方法是如何与ssh命令一起运行的?


Tags: topathtest命令服务器脚本servermongo