2013-04-11 80 views
1

在我们公司,我们使用的Jython出于某种原因。我需要用ExpectJ扩展它,但我无法弄清楚如何去做。如何在Jython中使用ExpectJ?

我设法下载expectj-2.0.7.jarexpectj-2.0.7-sources.jarexpectj-2.0.7-javadoc.jar文件,使他们对Jython的访问, Java本身也是如此。

所以我可以在我的Python脚本导入和JVM也发现罐子(使用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方法?有没有人有这个解决方案?

回答

1

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

为了等待命令提示完成执行衍生的进程,使用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();