先给一份样例:
package com.log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class Test8 {
/**
* 连接linux系统
* @param args
*/
public static void main(String[] args) {
try {
Connection conn = new Connection("192.168.81.129");
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword("root",
"123456");
if (isAuthenticated == false) {
throw new IOException("Authentication failed");
}
Session sess = conn.openSession();
sess.requestPTY("bash");
sess.startShell();
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader stdoutReader = new BufferedReader(
new InputStreamReader(stdout));
BufferedReader stderrReader = new BufferedReader(
new InputStreamReader(stderr));
BufferedReader inputReader = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(sess.getStdin());
String temp = "";
while (!temp.equals("exit")) {
System.out.print("[root@vmone ~]#");
temp = inputReader.readLine();
out.println(temp);
out.flush();
String line = null;
while ((line = stdoutReader.readLine()) != null) {
if (line.length() == 0) {// line等于null从来不会发生,导致程序卡在这里
continue;
} else{
System.out.println(line);
}
}
System.out.println("Here is the output from stderr:");
while (true) {
line = stderrReader.readLine();
if (line == null)
break;
System.out.println(line);
}
}
System.out.println("ExitCode: " + sess.getExitStatus());
sess.close();
conn.close();
System.out.println("close connection");
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
}
基本流程就是:
1.开启一个连接,创建一个Connection对象
//准备一个新Connection对象,然后可以使用该对象建立与指定SSH-2服务器的连接。
Connection(java.lang.String hostname)
//准备一个新Connection对象,然后可以使用该对象建立与指定SSH-2服务器的连接
Connection(java.lang.String hostname, int port)
2.进行身份认证
3.新建一个session
4.执行具体命令
5.获取错误输出和标准输出
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
public class SSHTest{
public static void main(String[] args) {
String hostname = "192.168.192.128";
String username = "root";
String password = "root";
try {
Connection conn = new Connection(hostname);
conn.connect();
//进行身份认证
boolean isAuthenticated = conn.authenticateWithPassword(
username,password);
if (isAuthenticated == false)
throw new IOException("Authentication failed.");
//开启一个Session
Session sess = conn.openSession();
//执行具体命令
sess.execCommand("cat haha.txt");
//获取返回输出
InputStream stdout = new StreamGobbler(sess.getStdout());
//返回错误输出
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader stdoutReader = new BufferedReader(
new InputStreamReader(stdout));
BufferedReader stderrReader = new BufferedReader(
new InputStreamReader(stderr));
System.out.println("Here is the output from stdout:");
while (true) {
String line = stdoutReader.readLine();
if (line == null)
break;
System.out.println(line);
}
System.out.println("Here is the output from stderr:");
while (true) {
String line = stderrReader.readLine();
if (line == null)
break;
System.out.println(line);
}
//关闭Session
sess.close();
//关闭Connection
conn.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(2);
}
}
}