【问题标题】:Running Linux commands on Java through JSch通过 JSch 在 Java 上运行 Linux 命令
【发布时间】:2012-07-16 23:42:27
【问题描述】:

我正在通过 Java 上的 JSch 建立 SSH 连接,一切似乎都运行良好,直到我尝试运行这个 .sh 文件。 shell脚本的名字是repoUpdate.sh,很简单:

echo  ' ****Repository update****'
echo  ' Location: /home/cissys/repo/'
echo -e ' Command: svn update /home/cissys/repo/2.3.0'

svn update /home/cissys/repo/2.3.0

这是我直接在 linux 控制台上得到的输出,带有正确的命令响应:

[cissys@dsatelnx5 ~]$ repoUpdate.sh
 ****Repository update****
 Location: /home/cissys/repo/
 Command: svn update /home/cissys/repo/2.3.0

At revision 9432.

现在,这是我尝试调用同一个文件的 SSH 连接方法的 Java 代码

public void cmremove()
{
    try
    {
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        UserInfo ui = new SUserInfo(pass, null);
        session.setUserInfo(ui);
        session.setPassword(pass);
        session.connect();
        
        ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
        
        InputStream in = channelExec.getInputStream();
        
        channelExec.setCommand("./repoUpdate.sh");
        channelExec.connect();
        
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        int index = 0;
        
        while ((line = reader.readLine()) != null)
        {
            System.out.println(++index + " : " + line);
        }
        
        channelExec.disconnect();
        session.disconnect();
        
        System.out.println("Done!");
    }
    catch(Exception e)
    {
        System.err.println("Error: " + e);
    }
}

我得到的回应如下:

run:
1 :  ****Repository update****
2 :  Location: /home/cissys/repo/
3 :  Command: svn update /home/cissys/repo/2.3.0
Done!
BUILD SUCCESSFUL (total time: 2 seconds)

svn 命令 (At revision 9432) 没有任何输出或执行迹象。

我认为它可能会在某个时候关闭会话,而不是让命令正确执行。如果updateRepo.sh`` file would've had something like cp test.txt test_2.txt`,它会毫无问题地做到这一点。但我只对这个和其他一些特定的 .sh 文件有这个问题。

任何帮助将不胜感激。

【问题讨论】:

  • 另见How to read JSch command output? - 它展示了如何正确同时读取标准和错误输出,以允许命令完成并收集包括错误在内的所有输出。跨度>

标签: java linux ssh command jsch


【解决方案1】:

我怀疑你的 shell 命令由于某种原因出错了——也许svn 不在你的路径上,也许还有其他奇怪的环境影响——但是你没有得到错误输出,因为你没有看为了它。通常,错误是在您从channelExec.getErrStream 获得的流上发送的,但在您的代码中,您只能从getOutputStream 流中读取。

要对此进行诊断,您需要获取这些错误消息。让 linux 对常规输出和错误消息使用一个流可能比让你的 java 程序同时从两个流中提取更容易,所以我将此行添加为repoUpdate.sh 的顶行:

exec 2>&1

这将导致脚本的其余部分使用您正在读取的一个流作为输出和错误。

另外,在您调用chanelExec.disconnect 之前,您应该在您的java 程序中记录退出状态,然后更改您的“完成!”消息基于它是什么:

    int exitStatus = channelExec.getExitStatus();
    channelExec.disconnect();
    session.disconnect();

    if (exitStatus < 0) {
        System.out.println("Done, but exit status not set!");
    } else if (exitStatus > 0) {
        System.out.println("Done, but with error!");
    } else {
        System.out.println("Done!");
    }

如果您这样做,您应该会收到错误消息,告诉您为什么您的命令没有按预期工作。

【讨论】:

    【解决方案2】:

    这就是我所做的。
    我在 repoUpdate.sh 文件的顶部添加了exec 2&gt;&amp;1,并按照@DanielMartin 的建议添加了这段代码来读取输出错误,结果如下:

    public void cmremove()
    {
        try
        {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            UserInfo ui = new SUserInfo(pass, null);
            session.setUserInfo(ui);
            session.setPassword(pass);
            session.connect();
    
            ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
    
            InputStream in = channelExec.getInputStream();
    
            channelExec.setCommand("./repoUpdate.sh");
            channelExec.connect();
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            int index = 0;
    
            while ((line = reader.readLine()) != null)
            {
                System.out.println(++index + " : " + line);
            }
    
            int exitStatus = channelExec.getExitStatus();
            channelExec.disconnect();
            session.disconnect();
            if(exitStatus < 0){
                System.out.println("Done, but exit status not set!");
            }
            else if(exitStatus > 0){
                System.out.println("Done, but with error!");
            }
            else{
                System.out.println("Done!");
            }
        }
        catch(Exception e)
        {
            System.err.println("Error: " + e);
        }
    }
    

    所以这实际上有很大帮助。它确认该命令实际上并没有像我想象的那样正确执行。我在我的 java 输出中得到了这个:

    run:
    1 :  ****Repository update****
    2 :  Location: /home/cissys/repo/
    3 :  Command: svn update /home/cissys/repo/2.3.0
    4 : ./repoUpdate.sh[6]: svn: not found [No such file or directory]
    Done!
    BUILD SUCCESSFUL (total time: 2 seconds)
    

    然后我尝试修改 repoUpdate.sh 文件,添加 svn 命令的绝对路径(谢谢@ymnk)

    exec 2>&1
    echo  ' ****Repository update****'
    echo  ' Location: /home/cissys/repo/'
    echo -e ' Command: svn update /home/cissys/repo/2.3.0'
    
    /opt/subversion/bin/svn update /home/cissys/repo/2.3.0
    

    对于我的 java,我得到了我正在寻找的东西:

    run:
    1 :  ****Repository update****
    2 :  Location: /home/cissys/repo/
    3 :  Command: svn update /home/cissys/repo/2.3.0
    4 : At revision 9443.
    Done!
    BUILD SUCCESSFUL (total time: 3 seconds)
    

    我发现我通过 java 从会话中获得的$PATH 与我直接在 linux 控制台上获得的不同。 所以添加 svn 路径实际上起到了作用。非常感谢您的帮助!

    【讨论】: