java Jsch 或 SSHJ 或 Ganymed SSH-2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5097514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jsch or SSHJ or Ganymed SSH-2?
提问by user605596
I need to connect to server(username,pasw,host)-- easy
enter 3-10 commands -- command="dir;date;cd;dir" is there an easier way ?, without writing 20 lines: while(smtng) { a lot of stuff+ mysterious print to scr:D }
download a file-- easy
write another downloaded file to the same file (add not owerride) -- any ideas how?
我需要连接到服务器(用户名、密码、主机)——很简单
输入 3-10 个命令 -- command="dir;date;cd;dir" 有没有更简单的方法?,不用写 20 行:while(smtng) { 很多东西+ 神秘打印到 scr:D }
下载文件——简单
将另一个下载的文件写入同一个文件(添加而不是 owerride) - 任何想法如何?
So to perform these increadible easy tasks, which might seem impossible if you dare to use Jsch(awsome documentation), there is a choise between Jsch,sshj,Ganymed any suggestions?
因此,要执行这些令人难以置信的简单任务,如果您敢于使用 Jsch(糟糕的文档),这似乎是不可能的,Jsch、sshj、Ganymed 之间有一个选择,有什么建议吗?
Mystery:
神秘:
2) multiple commands entering
2)多条命令输入
4) adding to the existing txt file more txt :D (probably there is a build in command) or not?
4)向现有的txt文件添加更多的txt :D(可能有一个内置命令)或不?
/* just for download/owerride : sftpChannel.get("downloadfile.txt", "savefile.txt");*/
采纳答案by user207421
I can't comment on the others but Ganymed works very well indeed.
我不能评论其他人,但 Ganymed 确实很好用。
回答by nIKUNJ
I don't know about Ganymed. But I have used JSch extensively for remote login and script executions. I used Google's Expect4j with Jsch for executing scripts on remote machines in expect mode(send/wait). You can get the whole output of executed command or scripts in your code using JSch/Expect4j/Closures.
我不知道Ganymed。但我已经广泛使用 JSch 进行远程登录和脚本执行。我使用 Google 的 Expect4j 和 Jsch 在期望模式(发送/等待)下在远程机器上执行脚本。您可以使用 JSch/Expect4j/Closures 在代码中获取已执行命令或脚本的完整输出。
For jsch, go to http://www.jcraft.com/jsch/
For Expect4j, go to http://code.google.com/p/expect4j/
对于 jsch,请访问http://www.jcraft.com/jsch/
对于 Expect4j,请访问http://code.google.com/p/expect4j/
The following is a small code sample for logging in and executing file for remote Java class.
以下是远程Java类的登录和执行文件的小代码示例。
private Expect4j SSH(String hostname, String username,String password, int port) throws Exception {
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
if (password != null) {
session.setPassword(password);
}
Hashtable<String,String> config = new Hashtable<String,String>();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect(60000);
channel = (ChannelShell) session.openChannel("shell");
Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
channel.connect();
return expect;
}
This method will open up a SSH stream to the remote server which will be used by expect4j for sending commands.
此方法将向远程服务器打开 SSH 流,expect4j 将使用该流发送命令。
private boolean executeCommands() {
boolean isSuccess = true;
Closure closure = new Closure() {
public void run(ExpectState expectState) throws Exception {
buffer.append(expectState.getBuffer());//buffer is string buffer for appending output of executed command
expectState.exp_continue();
}
};
List<Match> lstPattern = new ArrayList<Match>();
String[] regEx = SSHConstants.linuxPromptRegEx;
if (regEx != null && regEx.length > 0) {
synchronized (regEx) {
for (String regexElement : regEx) {//list of regx like, :>, /> etc. it is possible command prompts of your remote machine
try {
RegExpMatch mat = new RegExpMatch(regexElement, closure);
lstPattern.add(mat);
} catch (MalformedPatternException e) {
return false;
} catch(Exception e) {
return false;
}
}
lstPattern.add(new EofMatch( new Closure() { // should cause entire page to be collected
public void run(ExpectState state) {
}
}));
lstPattern.add(new TimeoutMatch(defaultTimeOut, new Closure() {
public void run(ExpectState state) {
}
}));
}
}
try {
Expect4j expect = SSH(objConfig.getHostAddress(), objConfig.getUserName(), objConfig.getPassword(), SSHConstants.SSH_PORT);
expect.setDefaultTimeout(defaultTimeOut);
if(isSuccess) {
for(String strCmd : lstCmds)
isSuccess = isSuccess(lstPattern,strCmd);
}
boolean isFailed = checkResult(expect.expect(lstPattern));
return !isFailed;
} catch (Exception ex) {
return false;
} finally {
closeConnection();
}
}
private boolean isSuccess(List<Match> objPattern,String strCommandPattern) {
try {
boolean isFailed = checkResult(expect.expect(objPattern));
if (!isFailed) {
expect.send(strCommandPattern);
expect.send("\r");
return true;
}
return false;
} catch (MalformedPatternException ex) {
return false;
} catch (Exception ex) {
return false;
}
}
回答by David Kocher
Ganymed now has a new home at Google Codewhere it is maintained. Recently patches for improved transfer rate have been commited among others.
Ganymed 现在在Google Code有一个新家,它在那里得到维护。最近已经提交了用于提高传输速率的补丁。