通過(guò)Java實(shí)現(xiàn)bash命令過(guò)程解析
這篇文章主要介紹了通過(guò)Java實(shí)現(xiàn)bash命令過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1、BASH 命令簡(jiǎn)介
2、Java實(shí)現(xiàn) BASH命令執(zhí)行Shell腳本
1)代碼實(shí)現(xiàn)如下:
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class BashUtil {
private Logger logger = LoggerFactory.getLogger(BashUtil.class);
private String hostname;
private String username;
private String password;
private int port;
private Connection conn;
private BashUtil() {
}
public BashUtil(String hostname, String username, String password) {
this(hostname, username, password, 22);
}
public BashUtil(String hostname, String username, String password, Integer port) {
this.hostname = hostname;
this.username = username;
this.password = password;
if (port == null) {
port = 22;
} else {
this.port = port;
}
}
/**
* 創(chuàng)建連接并認(rèn)證
* @return
*/
public Boolean connection() {
try {
conn = new Connection(hostname, port);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
return isAuthenticated;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 關(guān)閉連接
*/
public void close() {
try {
conn.close();
conn = null;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 執(zhí)行shell命令
* @param command
* @return
*/
public List<String> command(String command) {
if (conn == null && !connection()) {
logger.error("Authentication failed.");
return null;
}
List<String> result = new ArrayList<String>();
try {
Session sess = conn.openSession();
sess.execCommand(command);
InputStream stdout = new StreamGobbler(sess.getStdout());
InputStream stderr = new StreamGobbler(sess.getStderr());
BufferedReader br_out = new BufferedReader(new InputStreamReader(stdout, "utf-8"));
BufferedReader br_err = new BufferedReader(new InputStreamReader(stderr, "utf-8"));
StringBuffer sb_err = new StringBuffer();
String line = null;
while ((line = br_out.readLine()) != null) {
result.add(line.trim());
}
while ((line = br_err.readLine()) != null) {
sb_err.append(line + "\n");
}
if (isNotEmpty(sb_err.toString())) {
logger.error(sb_err.toString());
return null;
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static boolean isEmpty(String content) {
if (content == null) {
return true;
} else {
return "".equals(content.trim()) || "null".equalsIgnoreCase(content.trim());
}
}
private static boolean isNotEmpty(String content) {
return !isEmpty(content);
}
public static void main(String[] args){
String hostname = "192.168.123.234"; // 此處根據(jù)實(shí)際情況,換成自己需要訪(fǎng)問(wèn)的主機(jī)IP
String userName = "root";
String password = "password";
Integer port = 22;
String command = "cd /home/miracle&&pwd&&ls&&cat luna.txt";
BashUtil bashUtil = new BashUtil(hostname, userName, password, port);
List<String> resultList = bashUtil.command(command);
StringBuffer result = new StringBuffer("");
resultList.forEach(str -> result.append(str + "\n"));
System.out.println("執(zhí)行的結(jié)果如下: \n" + result.toString());
}
}
2)執(zhí)行結(jié)果如下:
執(zhí)行的結(jié)果如下: /home/miracle luna.txt Hello, I'm SshUtil. Nice to meet you.^_^
3)pom.xml引用依賴(lài)包如下:
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!-- ssh -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Bash 腳本實(shí)現(xiàn)每次登錄到 Shell 時(shí)可以查看 Linux 系統(tǒng)信息
- Python3 執(zhí)行Linux Bash命令的方法
- bash腳本中將密碼傳遞給ssh/scp命令方法詳解
- 在Bash腳本中創(chuàng)建和使用數(shù)組方法總結(jié)
- 深入理解Bash中的尖括號(hào)(適合初學(xué)者)
- bash命令使用詳解
- Java代碼執(zhí)行shell命令的實(shí)現(xiàn)
- Java簡(jiǎn)單實(shí)現(xiàn)調(diào)用命令行并獲取執(zhí)行結(jié)果示例
- java調(diào)用shell命令并獲取執(zhí)行結(jié)果的示例
相關(guān)文章
Java中Maven的依賴(lài)管理問(wèn)題小結(jié)
這篇文章主要介紹了Java中Maven的依賴(lài)管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-05-05
使用SQL保留兩位小數(shù)的實(shí)現(xiàn)方式
SQL中保留兩位小數(shù)有三種方法:1、使用ROUND()函數(shù)進(jìn)行四舍五入;2、使用CONVERT()函數(shù)和3、CAST()函數(shù)進(jìn)行強(qiáng)制類(lèi)型轉(zhuǎn)換,這兩種方法會(huì)截?cái)喽嘤嗟奈粩?shù),ROUND()函數(shù)會(huì)保留0位,而CONVERT()和CAST()會(huì)刪除多余的02024-11-11
Springboot的maven間接依賴(lài)的實(shí)現(xiàn)
這篇文章主要介紹了Springboot的maven間接依賴(lài)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Arthas排查Kubernetes中應(yīng)用頻繁掛掉重啟異常
這篇文章主要為大家介紹了Arthas排查Kubernetes中應(yīng)用頻繁掛掉重啟的異常分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步2022-02-02
Java面向接口編程之簡(jiǎn)單工廠(chǎng)模式示例
這篇文章主要介紹了Java面向接口編程之簡(jiǎn)單工廠(chǎng)模式,結(jié)合實(shí)例形式詳細(xì)分析了java面向接口編程簡(jiǎn)單工廠(chǎng)模式的具體定義與使用方法,需要的朋友可以參考下2019-09-09
java集合類(lèi)arraylist循環(huán)中刪除特定元素的方法
下面小編就為大家?guī)?lái)一篇Java集合類(lèi)ArrayList循環(huán)中刪除特定元素的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
Java concurrency集合之LinkedBlockingDeque_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
LinkedBlockingDeque是雙向鏈表實(shí)現(xiàn)的雙向并發(fā)阻塞隊(duì)列。該阻塞隊(duì)列同時(shí)支持FIFO和FILO兩種操作方式,即可以從隊(duì)列的頭和尾同時(shí)操作(插入/刪除);并且,該阻塞隊(duì)列是支持線(xiàn)程安全。2017-06-06
Java線(xiàn)程間共享與協(xié)作詳細(xì)介紹
這篇文章主要介紹了Java線(xiàn)程間共享與協(xié)作詳細(xì)介紹,Java?支持多個(gè)線(xiàn)程同時(shí)訪(fǎng)問(wèn)一個(gè)對(duì)象或者對(duì)象的成員變量,更多相關(guān)介紹需要的朋友可以參考一下2022-09-09
解決mybatis #{}無(wú)法自動(dòng)添加引號(hào)的錯(cuò)誤
這篇文章主要介紹了解決mybatis #{}無(wú)法自動(dòng)添加引號(hào)的錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

