您好,登錄后才能下訂單哦!
這篇文章主要介紹了通過Java實現bash命令過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1、BASH 命令簡介
2、Java實現 BASH命令執行Shell腳本
1)代碼實現如下:
import ch.ethz.ssh3.Connection; import ch.ethz.ssh3.Session; import ch.ethz.ssh3.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; } } /** * 創建連接并認證 * @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; } } /** * 關閉連接 */ public void close() { try { conn.close(); conn = null; } catch (Exception e) { e.printStackTrace(); } } /** * 執行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"; // 此處根據實際情況,換成自己需要訪問的主機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("執行的結果如下: \n" + result.toString()); } }
2)執行結果如下:
執行的結果如下: /home/miracle luna.txt Hello, I'm SshUtil. Nice to meet you.^_^
3)pom.xml引用依賴包如下:
<!-- 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-ssh3</artifactId> <version>262</version> </dependency>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。