最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用springboot對linux進行操控的方法示例

 更新時間:2020年11月24日 11:40:23   作者:kill_clalala  
這篇文章主要介紹了使用springboot對linux進行操控的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1,在pom中導入

 <dependency>
  <groupId>ch.ethz.ganymed</groupId>
  <artifactId>ganymed-ssh2</artifactId>
  <version>build210</version>
 </dependency>

2,編寫工具類

package org.jeecg.modules.system.util;

/**
 * @Description:
 * @Author: LGX
 * @Date: 2020/11/19 10:36
 */


import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


import java.io.*;


/**
 * 遠程執(zhí)行l(wèi)inux的shell script
 * @author Ickes
 * @since V0.1
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
@Component
public class RemoteExecuteCommandutil {
  //字符編碼默認是utf-8
  private static String DEFAULTCHART="UTF-8";
  private Connection conn;

  @Value(value = "${jeecg.linux.ip}")
  public String ip;

  @Value(value = "${jeecg.linux.userName}")
  public String userName;

  @Value(value = "${jeecg.linux.userPwd}")
  public String userPwd;


  /**
   * 遠程登錄linux的主機
   * @author Ickes
   * @since V0.1
   * @return
   *   登錄成功返回true,否則返回false
   */
  public Boolean login(){
    boolean flg=false;
    try {
      conn = new Connection(ip);
      conn.connect();//連接
      flg=conn.authenticateWithPassword(userName, userPwd);//認證
    } catch (IOException e) {
      e.printStackTrace();
    }
    return flg;
  }
  /**
   * @author Ickes
   * 遠程執(zhí)行shll腳本或者命令
   * @param cmd
   *   即將執(zhí)行的命令
   * @return
   *   命令執(zhí)行完后返回的結果值
   * @since V0.1
   */
  public String execute(String cmd){
    String result="";
    try {
      if(login()){
        Session session= conn.openSession();//打開一個會話
        session.execCommand(cmd);//執(zhí)行命令
        result=processStdout(session.getStdout(),DEFAULTCHART);
        //如果為得到標準輸出為空,說明腳本執(zhí)行出錯了
        if(StringUtils.isBlank(result)){
          result=processStdout(session.getStderr(),DEFAULTCHART);
        }
        conn.close();
        session.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return result;
  }


  /**
   * @author Ickes
   * 遠程執(zhí)行shll腳本或者命令
   * @param cmd
   *   即將執(zhí)行的命令
   * @return
   *   命令執(zhí)行成功后返回的結果值,如果命令執(zhí)行失敗,返回空字符串,不是null
   * @since V0.1
   */
  public String executeSuccess(String cmd){
    String result="";
    try {
      if(login()){
        Session session= conn.openSession();//打開一個會話
        session.execCommand(cmd);//執(zhí)行命令
        result=processStdout(session.getStdout(),DEFAULTCHART);
        conn.close();
        session.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return result;
  }

  /**
   * 解析腳本執(zhí)行返回的結果集
   * @author Ickes
   * @param in 輸入流對象
   * @param charset 編碼
   * @since V0.1
   * @return
   *    以純文本的格式返回
   */
  private String processStdout(InputStream in, String charset){
    InputStream  stdout = new StreamGobbler(in);
    StringBuffer buffer = new StringBuffer();;
    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(stdout,charset));
      String line=null;
      while((line=br.readLine()) != null){
        buffer.append(line+"\n");
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return buffer.toString();
  }
}

3,yml里編寫配置信息

jeecg :
  linux:
   ip: 192.168.xxx.xxx
   userName: root
   userPwd: 123456

4,注入工具類,編寫命令

@Autowired
 private RemoteExecuteCommandutil Commandutil;
 @GetMapping(value = "/training")
 public String training(@RequestParam(name="cmd") String cmd){
// String a = "sh /opt/shops/test1.sh 1 3";
 //命令返回的信息
 String cmdInformation =Commandutil.execute("source /etc/profile;"+cmd);
 return cmdInformation;
 }

由于ssh連接無法自動獲取環(huán)境變量的值,得再執(zhí)行前面加入source /etc/profile;來手動識別,如果還是不行可以在/etc/profile末尾加入export PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

到此這篇關于使用springboot對linux進行操控的方法示例的文章就介紹到這了,更多相關springboot linux操控內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java虛擬機GC日志分析

    Java虛擬機GC日志分析

    這篇文章主要介紹了Java虛擬機GC日志分析,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • MyBatis-Plus條件構造器Wrapper應用實例

    MyBatis-Plus條件構造器Wrapper應用實例

    QueryWrapper是用于查詢的Wrapper條件構造器,可以通過它來構建SELECT語句中的WHERE條件,這篇文章主要介紹了MyBatis-Plus數(shù)據(jù)表操作條件構造器Wrapper,需要的朋友可以參考下
    2023-09-09
  • spring boot如何使用spring AOP實現(xiàn)攔截器

    spring boot如何使用spring AOP實現(xiàn)攔截器

    本篇文章主要介紹了spring boot如何使用spring AOP實現(xiàn)攔截器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯誤問題

    解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯誤問題

    本文主要講述了在使用MyBatis和MyBatis-Plus時遇到的綁定異常問題,通過排查和總結,作者發(fā)現(xiàn)使用MyBatis-Plus?Boot?Starter可以解決這個問題,文章詳細對比了MyBatis-Plus?Boot?Starter和MyBatis?Spring?Boot?Starter的功能和使用場景
    2025-01-01
  • SpringBoot解決yml明文密碼問題的方法

    SpringBoot解決yml明文密碼問題的方法

    在現(xiàn)代的軟件開發(fā)中,安全性是一個重要的考量因素,對于使用SpringBoot框架開發(fā)的應用程序而言,敏感信息如數(shù)據(jù)庫密碼、API密鑰等通常存儲在YAML配置文件中,而這些文件往往是明文存儲,存在安全隱患,所以本文介紹了SpringBoot解決yml明文密碼問題的方法
    2024-07-07
  • Java非靜態(tài)成員變量之死循環(huán)(詳解)

    Java非靜態(tài)成員變量之死循環(huán)(詳解)

    下面小編就為大家?guī)硪黄狫ava非靜態(tài)成員變量之死循環(huán)(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java并發(fā)工具類Future使用示例

    Java并發(fā)工具類Future使用示例

    這篇文章主要介紹了Java并發(fā)工具類Future使用示例,本文需要注意future.get()方法是阻塞式的,如果調用該方法的時候任務尚未執(zhí)行完成,則會一直等待下去,直到任務執(zhí)行結束,本文通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2022-06-06
  • Java中的接口回調實例

    Java中的接口回調實例

    今天小編就為大家分享一篇關于Java中的接口回調實例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • JAVA中的SPI思想介紹

    JAVA中的SPI思想介紹

    大家好,本篇文章主要講的是JAVA中的SPI思想介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • SpringBoot和Tomcat的關系解讀

    SpringBoot和Tomcat的關系解讀

    這篇文章主要介紹了SpringBoot和Tomcat的關系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03

最新評論

昌黎县| 武宣县| 长汀县| 沐川县| 永清县| 十堰市| 武夷山市| 芮城县| 尉氏县| 华安县| 邵阳市| 淄博市| 新津县| 平乐县| 瑞金市| 涿鹿县| 岳普湖县| 琼中| 襄垣县| 城口县| 许昌县| 元朗区| 甘泉县| 芦溪县| 衡阳县| 洮南市| 新巴尔虎左旗| 东莞市| 故城县| 株洲县| 连南| 北票市| 盐边县| 广宗县| 桐乡市| 牟定县| 汽车| 中阳县| 东光县| 密山市| 招远市|