使用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.*;/** * 遠程執行linux的shell script * @author Ickes * @since V0.1 */@Data@NoArgsConstructor@AllArgsConstructor@Slf4j@Componentpublic 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 * 遠程執行shll腳本或者命令 * @param cmd * 即將執行的命令 * @return * 命令執行完后返回的結果值 * @since V0.1 */ public String execute(String cmd){ String result=''; try { if(login()){Session session= conn.openSession();//打開一個會話session.execCommand(cmd);//執行命令result=processStdout(session.getStdout(),DEFAULTCHART);//如果為得到標準輸出為空,說明腳本執行出錯了if(StringUtils.isBlank(result)){ result=processStdout(session.getStderr(),DEFAULTCHART);}conn.close();session.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } /** * @author Ickes * 遠程執行shll腳本或者命令 * @param cmd * 即將執行的命令 * @return * 命令執行成功后返回的結果值,如果命令執行失敗,返回空字符串,不是null * @since V0.1 */ public String executeSuccess(String cmd){ String result=''; try { if(login()){Session session= conn.openSession();//打開一個會話session.execCommand(cmd);//執行命令result=processStdout(session.getStdout(),DEFAULTCHART);conn.close();session.close(); } } catch (IOException e) { e.printStackTrace(); } return result; } /** * 解析腳本執行返回的結果集 * @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連接無法自動獲取環境變量的值,得再執行前面加入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操控內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: