使用springboot對(duì)linux進(jìn)行操控的方法示例
1,在pom中導(dǎo)入
<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.*;
/**
* 遠(yuǎn)程執(zhí)行l(wèi)inux的shell script
* @author Ickes
* @since V0.1
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
@Component
public class RemoteExecuteCommandutil {
//字符編碼默認(rèn)是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;
/**
* 遠(yuǎn)程登錄linux的主機(jī)
* @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);//認(rèn)證
} catch (IOException e) {
e.printStackTrace();
}
return flg;
}
/**
* @author Ickes
* 遠(yuǎn)程執(zhí)行shll腳本或者命令
* @param cmd
* 即將執(zhí)行的命令
* @return
* 命令執(zhí)行完后返回的結(jié)果值
* @since V0.1
*/
public String execute(String cmd){
String result="";
try {
if(login()){
Session session= conn.openSession();//打開一個(gè)會(huì)話
session.execCommand(cmd);//執(zhí)行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
//如果為得到標(biāo)準(zhǔn)輸出為空,說明腳本執(zhí)行出錯(cuò)了
if(StringUtils.isBlank(result)){
result=processStdout(session.getStderr(),DEFAULTCHART);
}
conn.close();
session.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* @author Ickes
* 遠(yuǎn)程執(zhí)行shll腳本或者命令
* @param cmd
* 即將執(zhí)行的命令
* @return
* 命令執(zhí)行成功后返回的結(jié)果值,如果命令執(zhí)行失敗,返回空字符串,不是null
* @since V0.1
*/
public String executeSuccess(String cmd){
String result="";
try {
if(login()){
Session session= conn.openSession();//打開一個(gè)會(huì)話
session.execCommand(cmd);//執(zhí)行命令
result=processStdout(session.getStdout(),DEFAULTCHART);
conn.close();
session.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 解析腳本執(zhí)行返回的結(jié)果集
* @author Ickes
* @param in 輸入流對(duì)象
* @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連接無法自動(dòng)獲取環(huán)境變量的值,得再執(zhí)行前面加入source /etc/profile;來手動(dòng)識(shí)別,如果還是不行可以在/etc/profile末尾加入export PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
到此這篇關(guān)于使用springboot對(duì)linux進(jìn)行操控的方法示例的文章就介紹到這了,更多相關(guān)springboot linux操控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Linux下部署springboot項(xiàng)目的方法步驟
- Linux編輯啟動(dòng)、停止與重啟springboot jar包腳本實(shí)例
- Linux啟動(dòng)與停止spring boot工程的腳本示例
- Linux 啟動(dòng)停止SpringBoot jar 程序部署Shell 腳本的方法
- 使用linux部署Spring Boot程序
- Springboot jar文件如何打包zip在linux環(huán)境運(yùn)行
- spring boot linux啟動(dòng)方式詳解
- Linux+Docker+SpringBoot+IDEA一鍵自動(dòng)化部署的詳細(xì)步驟
相關(guān)文章
MyBatis-Plus條件構(gòu)造器Wrapper應(yīng)用實(shí)例
QueryWrapper是用于查詢的Wrapper條件構(gòu)造器,可以通過它來構(gòu)建SELECT語句中的WHERE條件,這篇文章主要介紹了MyBatis-Plus數(shù)據(jù)表操作條件構(gòu)造器Wrapper,需要的朋友可以參考下2023-09-09
spring boot如何使用spring AOP實(shí)現(xiàn)攔截器
本篇文章主要介紹了spring boot如何使用spring AOP實(shí)現(xiàn)攔截器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
解決mybatis-plus-boot-starter與mybatis-spring-boot-starter的錯(cuò)誤問題
本文主要講述了在使用MyBatis和MyBatis-Plus時(shí)遇到的綁定異常問題,通過排查和總結(jié),作者發(fā)現(xiàn)使用MyBatis-Plus?Boot?Starter可以解決這個(gè)問題,文章詳細(xì)對(duì)比了MyBatis-Plus?Boot?Starter和MyBatis?Spring?Boot?Starter的功能和使用場(chǎng)景2025-01-01
Java非靜態(tài)成員變量之死循環(huán)(詳解)
下面小編就為大家?guī)硪黄狫ava非靜態(tài)成員變量之死循環(huán)(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09

