java - 尝试使用java代码依次执行两个shell脚本

标签 java linux ubuntu

这是我使用的代码。

ssh.sh:

#!/bin/sh
ssh root@192.168.1.6

ssh1.sh:

#!/bin/sh
ifconfig

代码

public static void main(String[] args) {
        Process p,p1;
        String s=null,s1=null;
        try {
        p = Runtime.getRuntime().exec("/home/test/Desktop/selenium/ssh.sh");
        Thread.sleep(2500);
        p1 = Runtime.getRuntime().exec("/home/test/Desktop/selenium/ssh1.sh");
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader br1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
        while ((s = br.readLine()) != null){
        System.out.println(s);
        }
        while ((s1 = br1.readLine())!=null)
        {
            System.out.println(s1);
        }
        p.waitFor();
        p1.waitFor();
        System.out.println ("exit: " + p.exitValue());
        System.out.println ("exit: " + p1.exitValue());
        p.destroy();
        p1.destroy();
    } catch (Exception e) {}
}

但它只执行 ssh.sh

最佳答案

如果您想通过 ssh 获取 ifconfig,只需将第一个脚本更改为 ssh root@192.168.1.6 ifconfig

关于java - 尝试使用java代码依次执行两个shell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679922/

相关文章:

java - 读取包含括号的文件中的整数值

java - 如何查询一般情况?

c - 旧 libconfig 版本支持 config_setting_get_string

java - 如何使用 jamod 库在 Java 中实例化 Register 对象?

java - 如何使用JMC连接远程WildFly?

php - 图像未从本地主机服务器显示

json - 将 JSON 文件中的所有时间戳转换为 bash 中的 unix 时间戳(Ubuntu)

ubuntu - 如何在 Ubuntu 中更新 Visual Studio Code?

Apache - 在我的域上输入没有 www 不起作用

mysql - 运行mysql_secure_installation后如何才能成功使用root登录?