亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

通過Java實(shí)現(xiàn)bash命令過程解析

 更新時(shí)間:2020年01月14日 09:46:34   作者:龍凌云端  
這篇文章主要介紹了通過Java實(shí)現(xiàn)bash命令過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了通過Java實(shí)現(xiàn)bash命令過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、BASH 命令簡介

Bash,Unix shell的一種,在1987年由布萊恩·??怂篂榱?a rel="external nofollow" target="_blank">GNU計(jì)劃而編寫。1989年發(fā)布第一個(gè)正式版本,原先是計(jì)劃用在GNU操作系統(tǒng)上,但能運(yùn)行于大多數(shù)類Unix系統(tǒng)的操作系統(tǒng)之上,包括Linux與Mac OS X v10.4都將它作為默認(rèn)shell。
Bash是Bourne shell的后繼兼容版本與開放源代碼版本,它的名稱來自Bourne shell(sh)的一個(gè)雙關(guān)語(Bourne again / born again):Bourne-Again SHell。
Bash是一個(gè)命令處理器,通常運(yùn)行于文本窗口中,并能執(zhí)行用戶直接輸入的命令。Bash還能從文件中讀取命令,這樣的文件稱為腳本。和其他Unix shell 一樣,它支持文件名替換(通配符匹配)、管道、here文檔、命令替換、變量,以及條件判斷和循環(huán)遍歷的結(jié)構(gòu)控制語句。包括關(guān)鍵字、語法在內(nèi)的基本特性全部是從sh借鑒過來的。其他特性,例如歷史命令,是從cshksh借鑒而來??偟膩碚f,Bash雖然是一個(gè)滿足POSIX規(guī)范的shell,但有很多擴(kuò)展。

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í)際情況,換成自己需要訪問的主機(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引用依賴包如下:

<!-- 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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot的異常處理流程是什么樣的?

    SpringBoot的異常處理流程是什么樣的?

    今天給大家?guī)淼氖荍ava的相關(guān)知識,文章圍繞著SpringBoot的異常處理流程展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 詳解Spring Boot 打包分離依賴JAR 和配置文件

    詳解Spring Boot 打包分離依賴JAR 和配置文件

    這篇文章主要介紹了Spring Boot 打包分離依賴JAR 和配置文件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java使用鎖解決銀行取錢問題實(shí)例分析

    Java使用鎖解決銀行取錢問題實(shí)例分析

    這篇文章主要介紹了Java使用鎖解決銀行取錢問題,結(jié)合實(shí)例形式分析了java線程同步與鎖機(jī)制相關(guān)原理及操作注意事項(xiàng),需要的朋友可以參考下
    2019-08-08
  • SpringBoot攔截器excludePathPatterns方法不生效的解決方案

    SpringBoot攔截器excludePathPatterns方法不生效的解決方案

    這篇文章主要介紹了SpringBoot攔截器excludePathPatterns方法不生效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 一文解讀Spring Bean的生命周期

    一文解讀Spring Bean的生命周期

    這篇文章主要給大家詳細(xì)解讀Spring Bean的生命周期,文中有詳細(xì)的代碼示例,對我們學(xué)習(xí)Spring Bean的生命周期有一定的幫助,感興趣的同學(xué)跟著小編一起來學(xué)習(xí)吧
    2023-07-07
  • SpringBoot打Jar包在命令行運(yùn)行流程詳解

    SpringBoot打Jar包在命令行運(yùn)行流程詳解

    這篇文章主要介紹了SpringBoot打Jar包在命令行運(yùn)行流程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java圖片轉(zhuǎn)字符圖片的生成方法

    Java圖片轉(zhuǎn)字符圖片的生成方法

    本文主要實(shí)現(xiàn)了將一張圖片轉(zhuǎn)成字符圖片,同樣可以遍歷每個(gè)像素點(diǎn),然后將像素點(diǎn)由具體的字符來替換,從而實(shí)現(xiàn)字符化處理,感興趣的可以了解一下
    2021-11-11
  • Java動(dòng)態(tài)代理四種實(shí)現(xiàn)方式詳解

    Java動(dòng)態(tài)代理四種實(shí)現(xiàn)方式詳解

    這篇文章主要介紹了Java四種動(dòng)態(tài)代理實(shí)現(xiàn)方式,對于開始學(xué)習(xí)java動(dòng)態(tài)代理或者要復(fù)習(xí)java動(dòng)態(tài)代理的朋友來講很有參考價(jià)值,有感興趣的朋友可以參考一下
    2021-04-04
  • SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法

    SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法

    下面小編就為大家分享一篇SpringMVC 向jsp頁面?zhèn)鬟f數(shù)據(jù)庫讀取到的值方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • java實(shí)現(xiàn)可視化界面肯德基(KFC)點(diǎn)餐系統(tǒng)代碼實(shí)例

    java實(shí)現(xiàn)可視化界面肯德基(KFC)點(diǎn)餐系統(tǒng)代碼實(shí)例

    這篇文章主要介紹了java肯德基點(diǎn)餐系統(tǒng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評論