通过 JSch 在 Java 上运行 Linux 命令

2022-01-23 00:00:00 ssh command linux java jsch

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

I'm establishing an SSH connection through JSch on Java and everything seemed to be working fine, until I tried to run this .sh file. The shell script's name is repoUpdate.sh and it's very simple:

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 控制台上得到的输出,带有正确的命令响应:

This is the output I get directly on the linux console with the proper response of the command:

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

At revision 9432.

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

Now, here's the Java code of my method with the SSH connection that tries to call this same file

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)上没有任何输出或执行迹象.

with no output or signs of execution on the svn command (At revision 9432) whatsoever.

我认为它可能会在某个时候关闭会话,而不是让命令正确执行.如果 updateRepo.sh`` 文件有类似 cp test.txt test_2.txt` 的内容,它会毫无问题地完成.但我只有这个和其他一些特定的 .sh 文件有这个问题.

I'm thinking it may be closing the session at some point, not letting the command execute properly. If the updateRepo.sh`` file would've had something like cp test.txt test_2.txt`, it would do it with no problem. But i only have this problem with this and some other specific .sh files.

任何帮助将不胜感激.

推荐答案

这就是我所做的.
我在 repoUpdate.sh 文件的顶部添加了 exec 2>&1 并添加了那段代码以读取@DanielMartin 建议的输出错误,导致这个:

So here's what I did.
I added the exec 2>&1 at the top of the repoUpdate.sh file and added that piece of code to read the output error as @DanielMartin suggested, resulting on this:

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 输出中得到了这个:

So that actually helped a lot. It confirmed that the command was, in fact, not executing correctly as I thought. I was getting this on my java output:

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)

Then I tried modifying the repoUpdate.sh file, adding the absolute path for the svn command (Thank you, @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,我得到了我正在寻找的东西:

For my java, I got just what I was looking for:

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 路径实际上起到了作用.非常感谢您的帮助!

I found out that the $PATH I get from the session through java is not the same that I get directly on the linux console. So adding the svn path actually did the trick. Thank you very much for your help!

相关文章