Jsch shell/Expecdt4j 简单示例?
我正在寻找一个如何在 Jsch 中使用 Expect4j 的简单示例(使用 Shell 而不是 exec) 我的意思是如何将命令(~8)发送到服务器,以及如何打印响应。
到目前为止我有这个:
JSch jsch=new JSch();
String host="www.superserver.uk.com";
String user="tom1234";
String passwd="12345a";
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
session.connect();
Channel channel=session.openChannel("shell");//only shell
channel.setInputStream(System.in);// enter lrp_list
channel.setOutputStream(System.out);
我想发送这样的命令:command=("lrp_list;newgrp xxx;date");发送(命令); 还有一些我发现的例子只适用于时间限制;我需要像上面的代码一样执行命令,即使执行需要 15 分钟。
I'm looking for a simple example how to use Expect4j in Jsch(using Shell not exec)
I mean how to send commands(~8) to the server, and how to printout the response.
so far I have this:
JSch jsch=new JSch();
String host="www.superserver.uk.com";
String user="tom1234";
String passwd="12345a";
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
session.setConfig("StrictHostKeyChecking", "no"); // if yes nothing works, but we're secure!
session.connect();
Channel channel=session.openChannel("shell");//only shell
channel.setInputStream(System.in);// enter lrp_list
channel.setOutputStream(System.out);
I want to send commands like this: command=("lrp_list;newgrp xxx;date"); send(command);
also some examples i have found only work with time restrictions; and i need something like in above code that would excecute a command even if excecution takes 15 min.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置 SCPInfo 对象来保存用户名、密码、端口:22 和 ip。
Setup an SCPInfo object to hold the username, password, port:22 and ip.
您拥有的代码将 Shell 的输入流/输出流连接到本地 java 进程的相同流。要在 shell 中执行命令,您必须在 shell 的输入流中提交这些命令(即不将其连接到本地输入),如下所示:
然后等待“date”的结果到来,并关闭通道。 (您可能想先发送“退出”或“注销”。)
我不知道expect4j,但我假设您可以向它提供一对InputStream和OutputStream - 然后使用
getInputStream
而不是setOutputStream
在这里。好的,找到源代码Expect4j.java,我会这样做:
The code you have connects the Inputstream/outputStream of the Shell to the same streams of the local java process. For executing commands in the shell, you have to submit these commands in the inputstream of the shell (i.e. not connect it to the local input), like this:
Then wait until the result of "date" comes, and close the channel. (You may want to send an "exit" or "logout" first.)
I don't know expect4j, but I assume you can feed it a pair of InputStream and OutputStream - then use
getInputStream
instead ofsetOutputStream
here.Okay, having found the source for Expect4j.java, I would do it like this: