0

我正在尝试使用 java 代码连接与 SSH 的交互式连接。我使用了 jsch 并期望生成器。但这对我来说是“错误消息java.io.EOFException:输入已关闭”在expectbuilder中。但是我通过腻子连接的同样的东西它工作正常。谁能帮助我哪里出错了?这是我的代码

package src.main.java;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

import javax.swing.JOptionPane;

import net.sf.expectit.Expect;
import net.sf.expectit.ExpectBuilder;
import net.sf.expectit.matcher.Matcher;
import net.sf.expectit.matcher.Matchers;
import net.sf.saxon.functions.Contains;

import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;


public class SSHConnect  {




    //String remoteFile="/home/john/test.txt";

public static void main(String[] args) {
    String user = "user";
    String password = "password";
    String host = "hostname";
    int port=22;

    try
    {  
        JSch jsch = new JSch();
        //       jsch.

        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        System.out.println("Establishing Connection...");
        session.connect();
        Channel channel=session.openChannel("exec");



        channel.connect();
        System.out.println("chanel connect");

         Expect expect = new ExpectBuilder()
         .withInputs(channel.getInputStream())
         .withOutput(channel.getOutputStream())
         .withTimeout(10, TimeUnit.SECONDS)
         .withExceptionOnFailure()

         .build();
         expect.interact();



        Expect p= expect.sendLine("ssh serverName");
        String check1=expect.expect(contains("Could"));
         expect.sendLine("yes");
         String check= expect.expect(contains("password"));
         expect.sendLine("password");
        System.out.println("check"+check1);





    }
    catch(Exception e){System.err.print("error message"+ e);}
}

    private static Matcher contains(String string) {
        Matcher matcher=Matchers.contains(string);
        return matcher;
    }
}
4

1 回答 1

0

尝试将 JSch 通道类型从“exec”更改为“shell”。

请按照这个例子

于 2016-09-27T16:18:24.010 回答