Java關於SSH連線Linux系統
阿新 • • 發佈:2019-01-02
先給一份樣例:
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("[
[email protected] ~]#"); 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); } } }