Java中运行shell脚本

简介: Java中运行shell脚本

需求


  忽然想写个东西,然后发现自己的linux中端口开放问题以及端口占用问题很麻烦,因为我经常用8080端口,如果有一个图形化界面看某个端口被占用以及被什么占用就好了。


落地实现


(1)直接执行shell命令(参数为命令)


ShellUtils.exceShell("ls -l /");


package com.example.portinterpretationplugin.utils;
import com.example.portinterpretationplugin.constant.ShellConstant;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
 * @author chaird
 * @create 2020-10-11 15:53
 */
public class ShellUtils {
  /**
   * @param pathOrCommand 脚本路径或者命令
   * @return
   */
  public static List<String> exceShell(String pathOrCommand) {
    List<String> result = new ArrayList<>();
    try {
      // 执行脚本
      Process ps = Runtime.getRuntime().exec(pathOrCommand);
      int exitValue = ps.waitFor();
      if (0 != exitValue) {
        System.out.println("call shell failed. error code is :" + exitValue);
      }
      // 只能接收脚本echo打印的数据,并且是echo打印的最后一次数据
      BufferedInputStream in = new BufferedInputStream(ps.getInputStream());
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String line;
      while ((line = br.readLine()) != null) {
        System.out.println("脚本返回的数据如下: " + line);
        result.add(line);
      }
      in.close();
      br.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
}


日志打印结果如下


1.png


(2)直接执行shell脚本(参数为脚本路径)


参数为脚本路径,脚本内容就不贴了


ShellUtils.exceShell("/opt/project/firewalld_status.sh");


package com.example.portinterpretationplugin.utils;
import com.example.portinterpretationplugin.constant.ShellConstant;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
 * @author chaird
 * @create 2020-10-11 15:53
 */
public class ShellUtils {
  /**
   * @param pathOrCommand 脚本路径或者命令
   * @return
   */
  public static List<String> exceShell(String pathOrCommand) {
    List<String> result = new ArrayList<>();
    try {
      // 执行脚本
      Process ps = Runtime.getRuntime().exec(pathOrCommand);
      int exitValue = ps.waitFor();
      if (0 != exitValue) {
        System.out.println("call shell failed. error code is :" + exitValue);
      }
      // 只能接收脚本echo打印的数据,并且是echo打印的最后一次数据
      BufferedInputStream in = new BufferedInputStream(ps.getInputStream());
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String line;
      while ((line = br.readLine()) != null) {
        System.out.println("脚本返回的数据如下: " + line);
        result.add(line);
      }
      in.close();
      br.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
}


前提是:你的脚本有权限去运行,即在linux上有权限去运行,否则不通;


如果不满足,如果不满足,下下策为执行脚本之前先执行以下赋予权限的命令 ,在执行你的命令


ShellUtils.exceShell("chmod -R 777 /opt/project/firewalld_status.sh");
ShellUtils.exceShell("/opt/project/firewalld_status.sh");


(3)脚本在项目里(在jar包里)


(1)复制sh到操作系统的某个目录下(亲测,可用)

 

(2)用方式二执行脚本

目录
相关文章
|
4天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
28 3
|
1天前
|
监控 Shell 应用服务中间件
第十二章 Shell脚本编写及常见面试题(二)
第十二章 Shell脚本编写及常见面试题(二)
|
1天前
|
监控 关系型数据库 Shell
第十二章 Shell脚本编写及常见面试题(一)
第十二章 Shell脚本编写及常见面试题(一)
|
1天前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
1天前
|
运维 Shell
Shell脚本判断IP是否合法性(多种方法)
Shell脚本判断IP是否合法性(多种方法)
|
1天前
|
Java
网页运行java程序cheerpj
网页运行java程序cheerpj
25 0
|
7天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
31 0
|
8天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
9天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
30 1
|
13天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0