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 使用jsch执行多个shell脚本_Java_Shell_Unix_Expect_Jsch - Fatal编程技术网

Java 使用jsch执行多个shell脚本

Java 使用jsch执行多个shell脚本,java,shell,unix,expect,jsch,Java,Shell,Unix,Expect,Jsch,我使用Jsch库在一些linux机器上执行大约1000个不同的shell脚本,并将状态更新到一个表中 我使用了jsch exec channel[ChannelExec],它只对单个脚本有效,如果shell脚本调用另一个脚本,ChannelExec不会给出正确的结果 现在我正在使用jsch的shell channnel。从任何类型的shell脚本中获取输出都很有效 问题是,如果我一次执行多个shell脚本,我将在一个批量中获得所有结果 无法执行一个Shell脚本并接收其结果 如果我想得到单个脚本

我使用Jsch库在一些linux机器上执行大约1000个不同的shell脚本,并将状态更新到一个表中

我使用了jsch exec channel[ChannelExec],它只对单个脚本有效,如果shell脚本调用另一个脚本,ChannelExec不会给出正确的结果

现在我正在使用jsch的shell channnel。从任何类型的shell脚本中获取输出都很有效

问题是,如果我一次执行多个shell脚本,我将在一个批量中获得所有结果

无法执行一个Shell脚本并接收其结果

如果我想得到单个脚本的执行结果,我需要登录到机器上执行每个脚本,这需要很长时间

有人可以发布一个解决方案,建议如何进行,登录到机器一次,执行多个脚本,并收到每个脚本的结果分别

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;

public class JschShellExample {

    public static void main(String[] args) {

        try {
            JSch jsch = new JSch();

            Session session = jsch.getSession("user", "10.32.248.158", 22);
            session.setPassword("password");

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            config.put("PreferredAuthentications","publickey,keyboard-interactive,password");
            session.setConfig(config);
            session.connect(100);

            Channel channel = session.openChannel("shell");

            OutputStream inputstream_for_the_channel = channel.getOutputStream();
            PrintStream commander = new PrintStream(inputstream_for_the_channel, true);

            channel.setOutputStream(null);
            channel.connect(100);
            //shell script
            commander.println("cd /user/home/work ; ./checkstaus.sh ; exit");
            commander.flush();

            System.out.println(channel.getExitStatus());

            InputStream outputstream_from_the_channel = channel.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
            String line = null;
            StringBuilder sb = new StringBuilder();
            boolean isloginStringPassed = false ;

            while ((line = br.readLine()) != null) {
                    sb.append(line.trim());
            }
            System.out.println("Result ="+sb.toString());

            channel.disconnect();
            session.disconnect();
            System.out.println("completed .. ");
        } catch (JSchException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

通常,当您第一次连接到远程计算机并且在执行命令后,您将得到一个打印到输出流中的文件。可以将其用作不同shell命令输出之间的标记

可以考虑使用第三方库,它简化了与远程服务的工作并捕获输出。您可以尝试以下一组好的选项:

然而,当我正要解决类似的问题时,我发现这些库非常陈旧,很难在商业软件中使用。因此,我创建了自己的,并将其提供给其他人。它叫。我的图书馆的优点在上面有说明

下面是与公共远程SSH服务交互的示例:

    JSch jSch = new JSch();
    Session session = jSch.getSession("new", "sdf.org");
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    Channel channel = session.openChannel("shell");

    Expect expect = new ExpectBuilder()
            .withOutput(channel.getOutputStream())
            .withInputs(channel.getInputStream(), channel.getExtInputStream())
            .withEchoOutput(adapt(System.out))
    //        .withInputFilters(removeColors(), removeNonPrintable())
            .withErrorOnTimeout(true)
            .build();
    // try-with-resources is omitted for simplicity
    channel.connect();
    expect.expect(contains("[RETURN]"));
    expect.sendLine();
    String ipAddress = expect.expect(regexp("Trying (.*)\\.\\.\\.")).group(1);
    System.out.println("Captured IP: " + ipAddress);
    expect.expect(contains("login:"));
    expect.sendLine("new");
    expect.expect(contains("(Y/N)"));
    expect.send("N");
    expect.expect(regexp(": $"));
    // finally is omitted
    channel.disconnect();
    session.disconnect();
    expect.close();

您还可以看一个与捕获单个命令输出的Karaf shell交互的示例

您好,谢谢您的回复,我将尝试这个并接受您的回答。