JAVA执行shell脚本并实时获取输出

个人博客原文地址:http://www.ltang.me/2016/10/23/java-call-shell/

之前做文档站点的时候有一个需求,做一个在线部署页面,能够通过页面上点点点就自动部署远程服务器上的服务,并且看到部署日志。简单的思路就是,页面通过websocket连接到java后台,java代码调用shell脚本执行发布操作,获取输出,并通过websocket将输出内容返回页面。

Runtime runtime = Runtime.getRuntime();
Process process;
BufferedReader br = null;
BufferedWriter wr = null;
try {
    process = runtime.exec("//要执行的命令");

    br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    wr = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
    
    String inline;
    while ((inline = br.readLine()) != null) {
        if (!inline.equals("")) {
            inline = inline.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
            session.getBasicRemote().sendText(inline);  //返回给页面
            if (inline.endsWith("发布到服务器?[Y/N]:")) {
                wr.write("y");                          //自动输入y
                wr.newLine();
                wr.flush();
                session.getBasicRemote().sendText("y");
            }
        } else {
            session.getBasicRemote().sendText("\n");    //换行
        }
    }
    br.close();
    br = new BufferedReader(new InputStreamReader(process.getErrorStream()));    //错误信息
    while ((inline = br.readLine()) != null) {
        if (!inline.equals(""))
            session.getBasicRemote().sendText("<font color='red'>" + inline + "</font>");
        else
            session.getBasicRemote().sendText("\n");
    }
} catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
    session.getBasicRemote().sendText("<font color='red'>" + e.getMessage() + "</font>");
} finally {
    if (br != null)
        br.close();
    if (wr != null)
        wr.close();
    session.getBasicRemote().sendText("End.");
}
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Java可以通过使用Runtime类或ProcessBuilder类来远程调用shell脚本获取输出信息。 使用Runtime类的示例代码如下: ```java public class RemoteShellDemo { public static void main(String[] args) { String command = "ls -l"; try { Process process = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } process.waitFor(); reader.close(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 使用ProcessBuilder类的示例代码如下: ```java public class RemoteShellDemo { public static void main(String[] args) { String command = "ls -l"; try { ProcessBuilder processBuilder = new ProcessBuilder(command.split(" ")); Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } process.waitFor(); reader.close(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 以上两种方式都可以通过执行shell命令来远程调用shell脚本,并使用BufferedReader来读取输出信息并打印。请注意,读取输出是阻塞的,需要等待命令执行完毕。 当然,在实际应用中,为了远程调用shell脚本,你需要配置好网络连接和权限等,确保能够正常访问并执行远程服务器上的shell脚本
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值