栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用springboot对linux进行操控的方法示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用springboot对linux进行操控的方法示例

1,在pom中导入

 
  ch.ethz.ganymed
  ganymed-ssh2
  build210
 

2,编写工具类

package org.jeecg.modules.system.util;




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.*;



@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;


  
  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;
  }
  
  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;
  }


  
  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;
  }

  
  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操控内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号