1

在我们公司,我们出于某种原因使用 Jython。我需要用 ExpectJ 扩展它,但我不知道该怎么做。

我设法下载了expectj-2.0.7.jarexpectj-2.0.7-sources.jarexpectj-2.0.7-javadoc.jar文件,并使 Jython 和 Java 本身也可以访问它们。

所以我可以在我的 python 脚本中导入它,JVM 也可以找到 jars(通过使用类路径加载程序 hack)。但是根据ExpectJ 的文档,还是有问题。

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方法?有人对此有解决方案吗?

4

1 回答 1

1

以下解决方案是确保生成的进程在执行下一个命令之前完成。它保证循环“发送-期望-等待发送中发送的命令完成”,然后“再次发送-再次期望-等待完成”。

为了等待命令提示符完成执行生成的进程,请使用shell.expect(""). 如果在此之后还有进一步的 expectJ send 和 expect 命令,则可以确保顺序执行。如果没有等待它已经派生的进程完成就执行shell.expect("")下一步,在以下情况下,scp 命令在发出下一个命令之前完成。shell.send("exit\n")

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();
于 2014-12-11T20:09:40.657 回答