在我们公司,我们出于某种原因使用 Jython。我需要用 ExpectJ 扩展它,但我不知道该怎么做。
我成功下载了expectj-2.0.7.jar、expectj-2.0.7-sources.jar和expectj-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???
这就是我陷入困境的地方。为什么 ExpectJ 对象没有 spawn
方法?有人有解决办法吗?
最佳答案
以下解决方案是确保生成的进程在执行下一个命令之前完成。它保证了“发送-期望-等待send中发送的命令完成”然后“再次发送-再次期望-等待完成”的循环。
为了等待命令提示符完成执行生成的进程,请使用shell.expect("")
。如果此后还有进一步的expectJ send和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();
关于java - 如何在 Jython 中使用 ExpectJ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15950937/