我正在使用java ssh工具与我的学校帐户建立ssh连接并查找文件然后将其删除。但是我创建了三个函数来做同样的事情但是使用不同的文件,我正在寻找一种方法来做同样的事情,而不是一个接一个地做。这是一些代码。基本上我想知道是否有办法在一个ssh连接或某种fork或多线程上完成所有这些。
public void updateinterval() {
try {
JSch jsch = new JSch();
String user = "***********";
String host = "********";
Session session = jsch.getSession(user, host, 22);
session.setPassword("*********");
// username and password will be given via UserInfo interface.
UserInfo userInfo = new SftpUserInfo();
session.setUserInfo(userInfo);
session.connect();
// look for a file named feedinterval
String checkfortimeupdate =
"cd public_html/final;grep '-send' feedinterval";
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(checkfortimeupdate);
// X Forwarding
// channel.setXForwarding(true);
// channel.setInputStream(System.in);
channel.setInputStream(null);
// channel.setOutputStream(System.out);
// FileOutputStream fos=new FileOutputStream("/tmp/stderr");
// ((ChannelExec)channel).setErrStream(fos);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
String returned = new String(tmp, 0, i);
String argument = returned.substring(6);
if (returned.contains("-send")) {
// if its there its calls the removeinterval function
// which removes the file it found
// by doing the same thing this function does but with a
// different ssh command
arduinoupdate hey = new arduinoupdate();
hey.removeinterval();
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {
"system.exe", "-send", argument };
Process proc = rt.exec(commands);
}
catch (IOException e) {
}
}
}
if (channel.isClosed()) {
System.out.println("UpdateInterval Closed exit-status: "
+ channel.getExitStatus());
break;
}
try {
/* Thread.sleep(1000); */
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
} catch (Exception e) {
System.out.println(e);
}
}
答案 0 :(得分:1)
如果您想要运行多个任务,ExpectJ可能会更好地为您服务。 ExpectJ使用JSCH,这可能会让你的生活更轻松。
final ExpectJ expectJ = new ExpectJ();
final Spawn spawn = expectJ.spawn("host", 22, "user", "pass");
spawn.send("cd public_html/final\n");
spawn.expect("someReturn");
...
spawn.send("exit\n");
spawn.expectClose();
答案 1 :(得分:0)
正如评论者所说,更好地将其分解为不同的功能。 然后更清楚如何为多个命令重用相同的连接(即Session)。
public void updateInterval(Session s, String filename) {
String checkfortimeupdate = "fgrep '-send' \"public_html/final/" + filename + "\"";
ChannelExec channel = (ChannelExec)session.openChannel("exec");
channel.setCommand(checkfortimeupdate);
channel.setInputStream(null);
channel.setErrStream(System.err);
BufferedReader in = new BufferedReader(new InputStreamReader(channel.getInputStream(), encoding));
channel.connect();
eatOutput(in);
if (channel.isClosed()) {
System.out.println("UpdateInterval Closed exit-status: "
+ channel.getExitStatus());
break;
}
channel.disconnect();
}
然后在方法readLine
中放入BufferedReader(它有eatOutput
方法)的读数,并使用另一种方法打开会话并调用此处显示的方法的三倍(使用不同的文件)姓名),并再次关闭会议。
答案 2 :(得分:0)
关于ExpectJ的使用, 哪个是用于机器关键生产应用程序的最佳库,用于基于期望的功能?
ExpectJ或Expect4J图书馆?虽然,两个库都使用下面的JSch! 或Apache SSHD - 客户:
SshClient client = SshClient.setUpDefaultClient(); PipedOutputStream PipedIn...?