我正在寻找一个简单的例子如何在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分钟也会超出命令。
答案 0 :(得分:1)
设置SCPInfo对象以保存用户名,密码,端口:22和ip。
List<String> commands = new ArrayList<String>();
commands.add("touch test1.txt");
commands.add("touch test2.txt");
commands.add("touch test3.txt");
runCommands(scpInfo, commands);
public static void runCommands(SCPInfo scpInfo, List<String> commands){
try {
JSch jsch = new JSch();
Session session = jsch.getSession(scpInfo.getUsername(), scpInfo.getIP(), scpInfo.getPort());
session.setPassword(scpInfo.getPassword());
setUpHostKey(session);
session.connect();
Channel channel=session.openChannel("shell");//only shell
channel.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(channel.getOutputStream()); // printStream for convenience
channel.connect();
for(String command: commands) {
shellStream.println(command);
shellStream.flush();
}
Thread.sleep(5000);
channel.disconnect();
session.disconnect();
} catch (Exception e) {
System.err.println("ERROR: Connecting via shell to "+scpInfo.getIP());
e.printStackTrace();
}
}
private static void setUpHostKey(Session session) {
// Note: There are two options to connect
// 1: Set StrictHostKeyChecking to no
// Create a Properties Object
// Set StrictHostKeyChecking to no
// session.setConfig(config);
// 2: Use the KnownHosts File
// Manually ssh into the appropriate machines via unix
// Go into the .ssh\known_hosts file and grab the entries for the hosts
// Add the entries to a known_hosts file
// jsch.setKnownHosts(khfile);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
}
答案 1 :(得分:0)
您拥有的代码将Shell的Inputstream / outputStream连接到本地java进程的相同流。为了在shell中执行命令,你必须在shell的输入流中提交这些命令(即不将它连接到本地输入),如下所示:
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.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(channel.getOutputStream()); // printStream for convenience
channel.connect();
shellStream.println("lrp_list");
shellStream.println("newgrp xxx");
shellStream.println("date");
然后等到“date”的结果到来,然后关闭频道。 (您可能希望先发送“退出”或“退出”。)
我不知道expect4j,但我假设你可以提供一对InputStream和OutputStream - 然后在这里使用getInputStream
代替setOutputStream
。
好的,找到了source for Expect4j.java后,我会这样做:
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
Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
// use expect methods