資源簡介
請參考http://blog.csdn.net/clariones/article/details/8455345
是解決用JSCH連接遠(yuǎn)程,執(zhí)行命令時,又需要輸入密碼的問題。 例如用JSCH執(zhí)行“su - root"是需要輸入的密碼。

代碼片段和文件信息
package?pkgname.jsh;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.InputStream;
import?java.util.Properties;
import?com.jcraft.jsch.ChannelShell;
import?com.jcraft.jsch.JSch;
import?com.jcraft.jsch.Session;
public?class?JSchShellMain?{
????/**
?????*?ription>
?????*?
?????*?@param?args
?????*?@throws?Exception?
?????*/
????public?static?void?main(String[]?args)?throws?Exception?{
????????//?Change?to?your?server?name
????????String?serverUrl?=?“xxx.xxx.xxx.xxx“;
????????String?userName?=?“oracle“;
????????String?password?=?“oracle“;
????????
????????JSch?jsch?=?new?JSch();
????????
????????Properties?config?=?new?Properties();
????????config.put(“StrictHostKeyChecking“?“no“);
????????config.put(“compression.s2c“?“zlibnone“);
????????config.put(“compression.c2s“?“zlibnone“);
????????
????????Session?session?=?jsch.getSession(userName?serverUrl);
????????session.setConfig(config);
????????session.setPort(22);
????????session.setPassword(password);
????????session.connect();
????????
????????ChannelShell?channel?=?(ChannelShell)?session.openChannel(“shell“);
????????MySshIOManager?mySshIo?=?new?MySshIOManager();
????????String?filename?=?“JunitResource/cmd.list“;
????????FileInputStream?fins?=?new?FileInputStream(filename);
????????mySshIo.loadCommands(fins);
????????fins.close();
????????
????????channel.setInputStream(mySshIo.getInputStream());
????????channel.setOutputStream(mySshIo.getOutputStream());
????????InputStream?is?=?channel.getExtInputStream();
????????channel.setPtyType(“MO80x25“);
????????channel.connect();
????????
????????while(!channel.isClosed()){
????????????Thread.sleep(1000);
????????}
????????
????????channel.disconnect();
????????session.disconnect();
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????652??2012-12-28?16:23??WydLab\.classpath
?????文件?????????382??2012-11-28?09:14??WydLab\.project
?????文件?????????744??2012-12-31?15:05??WydLab\JunitResource\cmd.list
?????文件??????227436??2012-12-28?15:48??WydLab\lib\jsch-0.1.49.jar
?????文件????????1815??2012-12-31?15:23??WydLab\src\pkgname\jsh\JSchShellMain.java
?????文件????????1086??2012-12-31?13:58??WydLab\src\pkgname\jsh\MyCommand.java
?????文件????????1817??2012-12-31?14:57??WydLab\src\pkgname\jsh\MyInputStream.java
?????文件????????1599??2012-12-31?14:57??WydLab\src\pkgname\jsh\MyOutputStream.java
?????文件????????3387??2012-12-31?15:09??WydLab\src\pkgname\jsh\MySshIOManager.java
?????文件?????????602??2012-12-31?14:57??WydLab\test\pkgname\jsh\MySshIOTest.java
評論
共有 條評論