如何利用Ganymed SSH-2模擬SSH操作
官方地址:http://www.cleondris.ch/en/opensource-ssh2.php
簡(jiǎn)介:
Ganymed SSH-2 for Java is a library which implements the SSH-2 protocol in pure Java (tested on J2SE 1.4.2 and 5.0). It allows one to connect to SSH servers from within Java programs. It supports SSH sessions (remote command execution and shell access), local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP. There are no dependencies on any JCE provider, as all crypto functionality is included.
程序:
@Test
public void testSsh() {
String hostname = "192.168.0.1";
String username = "root";
String password = "password";
try {
/* Create a connection instance */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
System.out.println("connect ok");
/*
* Authenticate. If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/
boolean isAuthenticated = conn.authenticateWithPassword(username,password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
System.out.println("Authentication ok");
/* Create a session */
Session sess = conn.openSession();
sess.execCommand("uname -a");
System.out.println("Here is some information about the remote host:");
/*
* This basic example does not handle stderr, which is sometimes
* dangerous (please read the FAQ).
*/
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}
/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());
/* Close this session */
sess.close();
/* Close the connection */
conn.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
運(yùn)行結(jié)果:
connect ok
Authentication ok
Here is some information about the remote host:
Linux localhost.localdomain 2.6.22 #1 SMP Wed Aug 13 11:24:59 CST 2008 i686 i686 i386 GNU/Linux
ExitCode: 0
相關(guān)文章
springboot單文件下載和多文件壓縮zip下載的實(shí)現(xiàn)
這篇文章主要介紹了springboot單文件下載和多文件壓縮zip下載的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11JFreeChart簡(jiǎn)單實(shí)現(xiàn)光滑曲線繪制
這篇文章主要為大家詳細(xì)介紹了JFreeChart簡(jiǎn)單實(shí)現(xiàn)光滑曲線的繪制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06SpringBoot2零基礎(chǔ)到精通之異常處理與web原生組件注入
SpringBoot是Spring全家桶的成員之一,基于約定優(yōu)于配置的思想(即有約定默認(rèn)值,在不配置的情況下會(huì)使用默認(rèn)值,在配置文件下配置的話會(huì)使用配置的值)。SpringBoot是一種整合Spring技術(shù)棧的方式(或者說(shuō)是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開(kāi)發(fā)的腳手架2022-03-03java開(kāi)發(fā)分布式服務(wù)框架Dubbo調(diào)用過(guò)程
這篇文章主要為大家介紹了java開(kāi)發(fā)分布式服務(wù)框架Dubbo調(diào)用過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11Java小程序賽馬游戲?qū)崿F(xiàn)過(guò)程詳解
這篇文章主要介紹了Java小程序賽馬游戲?qū)崿F(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03