如何在Jython中使用ExpectJ?

2024-05-15 00:44:35 发布

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

在我们公司,我们用Jython是有原因的。我需要用ExpectJ来扩展它,但我不知道怎么做。在

我设法下载了expectj-2.0.7.jarexpectj-2.0.7-来源.jarexpectj-2.0.7-javadoc.jar文件文件,并使Jython和Java本身也可以访问这些文件。在

所以我可以在python脚本中导入它,JVM也可以找到jar(通过使用classpath loader hack)。但根据ExpectJ's docs的说法,还是有问题。在

import expectj

ex = expectj.ExpectJ()                       # I cannot use the second form of the
                                             # constructor where I can pass a timeout
                                             # parameter

sh = ex.spawn(targetshell, 22, usr, passw)   # There is no spawn method in 
                                             # ExpectJ - but why???

这就是我被困的地方。{expect1}为什么没有方法?有人能解决这个问题吗?在


Tags: 文件the脚本来源公司原因jythonjava
1条回答
网友
1楼 · 发布于 2024-05-15 00:44:35

下面的解决方案是确保生成的进程在执行下一个命令之前完成。它保证了“send-expect-wait for completion of the command send in send”和“send again-expect again-wait for completion”的循环。在

为了等待命令提示符完成执行派生的进程,请使用shell.expect("")。在这种情况下,如果预期的命令是按顺序执行的,那么在这种情况下可以继续发送命令。如果没有shell.expect(""),则执行下一步shell.send("exit\n"),而不等待已经生成的进程的完成,在下面的情况下,scp命令在发出下一个命令之前完成。在

import expectj
expectinator = expectj.ExpectJ();
shell = expectinator.spawn(expectj.SshSpawn(host_name, 22, usr_name, ssh_pwd));
shell.send("scp -r src:usr:dest" + "\r")
shell.expect(remote_box_usr + "'s password:");
shell.send(ssh_pwd + "\r");
shell.expect("");
shell.send("exit\n");
shell.expectClose();

相关问题 更多 >

    热门问题