无法在另一台使用python-pexpect的服务器上使用ssh查找文件

2024-09-15 04:35:45 发布

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

我使用pexpect创建了简单的python脚本,使用

 CurrentCommand = "ssh " + serverRootUserName + "@" + serverHostName
 child = pexpect.spawn(CurrentCommand)

现在我运行一些命令,比如ls-a或“find/opt/license/-name'*.xml'” 使用代码

 child.run(mycommand)

如果从Pycharm运行,它工作得很好,但是如果从终端运行,它不工作,它找不到任何文件,我想它正在查看我的本地系统。你知道吗

谁能给我点建议吗。谢谢


Tags: name命令脚本childlicensexmlfindls
1条回答
网友
1楼 · 发布于 2024-09-15 04:35:45

建议看一下paramiko库(或fabric,它使用它,但有特定的用途),因为这是ssh的python接口。它可能会使您的代码更好,更能抵御bug或攻击。你知道吗

然而,我认为这个问题来自于你对run的使用。你知道吗

This function runs the given command; waits for it to finish; then returns all output as a string. STDERR is included in output. If the full path to the command is not given then the path is searched.

你应该看的是“期望”。也就是说,你用spawn生成,然后你应该用expect等待它到达一个合适的点(比如连接,motd按下后终端准备就绪等等(因为你可能需要在etc中输入用户名和密码)。你知道吗

然后运行sendline向程序发送一行代码。参见示例:

http://pexpect.readthedocs.io/en/latest/overview.html

希望对你有帮助,认真的,看看帕拉米科;)

相关问题 更多 >