在我们公司,我们使用Jython是出于某种原因。我需要用ExpectJ来扩展它,但是我想不出怎么做。
我成功地下载了expectj-2.0.7.jar,expectj-2.0.7-sources.jar和expectj-2.0.7-javadoc.jar文件,并使Jython和Java本身也可以访问这些文件。
因此,我可以在我的python脚本中导入它,JVM还可以找到jars (通过使用类路径装载机黑客)。但根据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方法?有人有办法解决这个问题吗?
发布于 2014-12-11 20:09:40
下面的解决方案是确保生成的进程在执行下一个命令之前完成。它保证循环‘发送-预期-等待完成发送的命令发送’,然后‘发送再次-预期再次-等待完成’。
为了等待命令提示符完成生成的进程的执行,请使用shell.expect("")。如果在此之后有进一步的expectJ发送和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();https://stackoverflow.com/questions/15950937
复制相似问题