Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java程序中运行长shell脚本代码?_Java_Shell - Fatal编程技术网

如何在java程序中运行长shell脚本代码?

如何在java程序中运行长shell脚本代码?,java,shell,Java,Shell,我正在编写一个Java程序,它应该ssh到IP并运行一个shell脚本代码 问题是脚本太长,应该由java代码携带 我使用了sshxcute库 我有两个解决方案: 将所有脚本作为字符串一行一行地放到JAVA代码中并立即执行。(如何做?)或者将脚本代码作为字符串分成80行,然后在JAVA中逐个运行。(我认为这不是一种有效的方法!但我知道的唯一方法是) 将shell脚本代码放在JAVA文件中,并由JAVA执行(我不知道如何执行)。我应该把它放在哪里(主软件包?)和谁联系 最简单的方法是在远程计算机上

我正在编写一个Java程序,它应该ssh到IP并运行一个shell脚本代码

问题是脚本太长,应该由java代码携带

我使用了sshxcute库

我有两个解决方案:

  • 将所有脚本作为字符串一行一行地放到JAVA代码中并立即执行。(如何做?)或者将脚本代码作为字符串分成80行,然后在JAVA中逐个运行。(我认为这不是一种有效的方法!但我知道的唯一方法是)

  • 将shell脚本代码放在JAVA文件中,并由JAVA执行(我不知道如何执行)。我应该把它放在哪里(主软件包?)和谁联系


  • 最简单的方法是在远程计算机上启动ssh会话,然后通过管道向其发送命令:

    // start ssh session to machine alan
    Process p = new ProcessBuilder("ssh", "alan").start();
    // get a means of reading its output
    final Scanner reader = new Scanner(p.getInputStream());
    // start a new thread going to read its output
    Thread readThread = new Thread(new Runnable() {
        public void run() {
            while (reader.hasNextLine())
                System.out.println(reader.nextLine());
            reader.close();             
        }
    });
    readThread.start();
    // get a means of sending it commands
    PrintWriter writer = new PrintWriter(p.getOutputStream());
    // send some commands
    writer.println("touch /tmp/somefile");
    writer.println("hostname");
    writer.println("ls -l /home");
    writer.println("uptime");
    writer.println("exit");
    writer.close();
    // wait for reading thread to finish
    readThread.join();
    
    这将启动进程并登录到远程计算机。然后,它启动一个新线程来读取进程的输出(就您的程序而言,这是一个输入流),并创建一个
    PrintWriter
    ,您可以使用它来发送命令(这是进程的输入,但就您的程序而言是一个输出流)

    当我在笔记本电脑上运行时,我会

    alan
    total 20
    drwx------ 65 james james  4096 Oct 16 21:50 james
    drwx------  2 root  root  16384 Apr  1  2014 lost+found
     23:12:13 up 6 days,  8:46,  5 users,  load average: 0.19, 0.19, 0.20
    

    如您所见,这是我发送的命令的输出。

    这是在Linux机器上运行的代码吗?如果我想在另一台通过特定Ip和密码连接到Linux机器的机器上运行这些bash脚本呢?@HoKa它通过ssh连接到一台机器,然后发送一些命令。我在代码中给出的命令只是示例。Java不是特定于平台的,但是如果您通过ssh向一台机器发送命令,那么您发送的命令必须适合该机器,无论它在运行什么。但决定发送何种命令的是ssh服务器的操作系统,而不是运行Java应用程序的机器。