項目中有這樣一個需求。web程序 調用腳本 來運行服務器上的程序 。執行任務。
想到了ssh 操作 程序的啓動腳本,同時傳遞參數。
一位大佬的 工具類 不廢話 直接分享
package util.ssh;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.*;
/**
* ssh操作類
*
*/
public class RemoteExecuteCommand {
/**
* 字符編碼默認是utf-8
*/
private static final String DEFAULT_CHART = "UTF-8";
/**
* 遠程 IP
*/
private String ip;
/**
* 用戶名
*/
private String userName;
/**
* 密碼
*/
private String password;
public RemoteExecuteCommand(String ip, String userName, String password) {
this.ip = ip;
this.userName = userName;
this.password = password;
}
/**
* 獲取 SSH 遠程連接
*
* @return 遠程連接
*/
public Connection getConnection() throws IOException {
Connection conn = new Connection(ip);
// 連接
conn.connect();
// 認證
boolean flag = conn.authenticateWithPassword(userName, password);
if(!flag) {
throw new IOException("ssh遠程連接認證失敗!");
}
return conn;
}
/**
* 執行指定的命令
*
* @param conn 遠程連接
* @param cmd 指定的命令信息
* @return 執行的結果
*/
public String execute(Connection conn, String cmd) throws IOException {
String result;
Session session = null;
try {
// 打開一個會話
session = conn.openSession();
// 執行命令
session.execCommand(cmd);
result = processStdout(session.getStdout(), DEFAULT_CHART);
// 如果爲得到標準輸出爲空,說明腳本執行出錯了
if ("".equals(result)) {
result = processStdout(session.getStderr(), DEFAULT_CHART);
}
} finally {
if(session != null) {
session.close();
}
}
return result;
}
/**
* 將本地文件目錄下所有的文件存儲到遠程機器指定的目錄上
*
* @param conn 遠程連接
* @param localPath 本地文件目錄
* @param remotePath 遠程文件目錄
*/
public void scpFileDir(Connection conn, String localPath, String remotePath) throws IOException {
SCPClient client = new SCPClient(conn);
File file = new File(localPath);
File[] files = file.listFiles();
if(files == null) {
throw new IOException("the path no file!");
}
String[] localFiles = new String[files.length];
for (int i = 0, length = files.length; i < length; i++) {
localFiles[i] = files[i].getAbsolutePath();
}
client.put(localFiles, remotePath);
}
/**
* 將本地文件存儲到遠程機器指定的目錄上
*
* @param conn 遠程連接
* @param filePath 本地文件
* @param remotePath 遠程文件目錄
*/
public void scpFile(Connection conn, String filePath, String remotePath) throws IOException {
SCPClient client = new SCPClient(conn);
File file = new File(filePath);
if(file.isFile()) {
client.put(filePath, remotePath);
} else {
throw new IOException("the file not find!");
}
}
/**
* 解析腳本執行返回的結果集
* @param in 輸入流對象
* @param charset 編碼
* @return 流獲取結果
*/
private String processStdout(InputStream in, String charset) throws IOException {
InputStream stdout = new StreamGobbler(in);
StringBuilder builder = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
String line;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n");
}
return builder.toString();
}
public void close(Connection connection) {
if(connection != null) {
connection.close();
}
}
}
測試類
public static void main(String[] args) {
RemoteExecuteCommand command = new RemoteExecuteCommand("xxx", "xxx", "xxx");
Connection connection = null;
try {
//linux 本地機器
connection = command.getConnection();
String linux= command.execute(connection, "sh /home//mytest/test.sh a b");
System.out.println(linux);
//hadoop 集羣
String hadoop = command.execute(connection, "hadoop fs -ls impala");
System.out.println(hadoop);
}catch (Exception e){
e.printStackTrace();
}
}