use*_*596 5 java ssh jsch sshj
我需要连接到服务器(用户名,密码,主机) - 简单
输入3-10命令 - command ="dir; date; cd; dir"是否有更简单的方法?,不写20行:while(smtng){很多东西+神秘的打印到scr:D}
下载文件 - 简单
将另一个下载的文件写入同一个文件(添加不是owerride) - 任何想法如何?
因此,为了执行这些可增加的简单任务,如果您敢于使用Jsch(awsome文档),这似乎是不可能的,Jsch,sshj,Ganymed之间有选择任何建议吗?
神秘:
2)多个命令进入
4)在现有的txt文件中添加更多txt:D(可能还有一个内置命令)或不?
/* just for download/owerride : sftpChannel.get("downloadfile.txt", "savefile.txt");*/
Run Code Online (Sandbox Code Playgroud)
我不知道Ganymed.但我已广泛使用JSch进行远程登录和脚本执行.我使用Google的Expect4j和Jsch在期望模式下执行远程机器上的脚本(发送/等待).您可以使用JSch/Expect4j/Closures在代码中获取已执行命令或脚本的全部输出.
有关jsch的信息,请访问http://www.jcraft.com/jsch/
对于Expect4j,请访问http://code.google.com/p/expect4j/
以下是用于登录和执行远程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;
}
Run Code Online (Sandbox Code Playgroud)
此方法将打开到远程服务器的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;
}
}
Run Code Online (Sandbox Code Playgroud)