Java操作SSH2實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令
引入依賴
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
SSH2Util 工具類封裝
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class SSH2Util {
//指定默認(rèn)編碼
private static String DEFAULT_CHARSET = "UTF-8";
/**
* 建立SSH2連接
* @param host 主機(jī)地址
* @param username 用戶名
* @param password 密碼
* @return Connection
*/
public static Connection openConnection(String host,String username,String password) {
try {
Connection connection = new Connection(host);
//建立ssh2連接
connection.connect();
//檢驗(yàn)用戶名
boolean login = connection.authenticateWithPassword(username,password);
if (login){
logger.info(host + " 連接成功");
return connection;
}else {
throw new RuntimeException(host + " 用戶名密碼不正確");
}
} catch (Exception e) {
throw new RuntimeException(host +" "+ e);
}
}
/**
* 執(zhí)行命令
* @param connection ssh2連接對(duì)象
* @param command 命令語(yǔ)句
* @return 執(zhí)行結(jié)果, 封裝執(zhí)行狀態(tài)和執(zhí)行結(jié)果
*/
public static ExecCmdResult execCommand(Connection connection,String command){
ExecCmdResult execCmdResult = new ExecCmdResult();
Session session = null;
try{
session = connection.openSession();
//執(zhí)行命令
session.execCommand(command);
//解析結(jié)果
String result = parseResult(session.getStdout());
//解析結(jié)果為空,則表示執(zhí)行命令發(fā)生了錯(cuò)誤,嘗試解析錯(cuò)誤出輸出
if (result == null||result.length()==0){
result = parseResult(session.getStderr());
}else {
execCmdResult.setSuccess(true);
}
//設(shè)置響應(yīng)結(jié)果
execCmdResult.setResult(result);
logger.info(command + " ==>> " +execCmdResult.getResult());
return execCmdResult;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static String parseResult(InputStream inputStream) throws IOException{
//讀取輸出流內(nèi)容
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,DEFAULT_CHARSET));
StringBuffer resultSB = new StringBuffer();
String line;
while((line = br.readLine()) != null){
resultSB.append(line+"\n");
}
//替換換行符
String result = resultSB.toString().replaceAll("[\\t\\n\\r]", "");
return result;
}
}
ExecCmdResult 定義返回結(jié)果類
public class ExecCmdResult {
//命令執(zhí)行是否成功
private boolean flag ;
//輸出結(jié)果
private String result;
public void setFlag(boolean success){
this.flag = success;
}
public boolean getFlag() {
return flag;
}
public String getResult(){
return result;
}
public void setResult(String result){
this.result = result;
}
}SSH2Demo 測(cè)試
import ch.ethz.ssh2.Connection;
public class SSH2Demo {
public static void main(String[] args) {
try {
String host = "168.192.22.7";
String username = "root";
String password = "123456";
Connection connection = SSH2Util.openConnection(host,username,password);
String cpuInfo = "cat /proc/cpuinfo | grep \"cpu cores\" | uniq"; //服務(wù)器核數(shù)
ExecCmdResult cup = SSH2Util.execCommand(connection,cpuInfo);
connection.close();
}
catch (Exception a){
a.printStackTrace();
}
}
}
到此這篇關(guān)于Java操作SSH2實(shí)現(xiàn)遠(yuǎn)程執(zhí)行l(wèi)inux命令的文章就介紹到這了,更多相關(guān)Java SSH2遠(yuǎn)程執(zhí)行l(wèi)inux命令內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目使用yml文件鏈接數(shù)據(jù)庫(kù)異常問(wèn)題解決方案
在使用SpringBoot時(shí),利用yml進(jìn)行數(shù)據(jù)庫(kù)連接配置需小心數(shù)據(jù)類型區(qū)分,如果用戶名或密碼是數(shù)字,必須用雙引號(hào)包裹以識(shí)別為字符串,避免連接錯(cuò)誤,特殊字符密碼也應(yīng)用引號(hào)包裹2024-10-10
controller層如何同時(shí)接收兩個(gè)實(shí)體類
這篇文章主要介紹了controller層如何同時(shí)接收兩個(gè)實(shí)體類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot配置log4j2的實(shí)現(xiàn)示例
SpringBoot中默認(rèn)使用Logback作為日志框架,本文主要介紹了SpringBoot配置log4j2的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Spring?Boot?lombok在高版本idea中注解不生效的解決辦法
這篇文章主要介紹了Spring?Boot?lombok在高版本idea中注解不生效的解決辦法,文中介紹了三種解決方案,分別是使用ptg插件生成方法、添加Lombok依賴或指定1.18.30版本并清除構(gòu)建配置,需要的朋友可以參考下2025-05-05
java 對(duì)文件夾目錄進(jìn)行深度遍歷實(shí)例代碼
這篇文章主要介紹了java 對(duì)文件夾目錄進(jìn)行深度遍歷實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Spring Boot全局異常處理與日志監(jiān)控全解析
本文給大家介紹Spring Boot全局異常處理與日志監(jiān)控全解析,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-08-08
spring-mvc/springboot使用MockMvc對(duì)controller進(jìn)行測(cè)試
這篇文章主要介紹了spring-mvc/springboot使用MockMvc對(duì)controller進(jìn)行測(cè)試,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

