Shell命令未在java中使用jsch和expcetit库运行

时间:2015-04-21 16:25:56

标签: java shell ssh expect

我按照以下步骤在ssh服务器上运行命令。

  1. 连接到ssh服务器
  2. 再次使用devpush命令登录到服务器(使用expectit库进行用户输入提示)。
  3. 最后使用jsch库运行远程命令。
  4. 我面临的问题是,我的代码进入无限循环,它能够登录到ssh服务器但无法运行命令。 任何人都可以帮我这个,因为我第一次使用这两个库。

    package com.dmotorworks.cdk;
    
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import net.sf.expectit.*;
    import net.sf.expectit.matcher.Matchers;
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
    
    public class ShellClient {
    
        public void loginToPabloServer() throws IOException, JSchException{
            String hostname="pablo.dmotorworks.com";
            String username="gaikwasu";
            String password="Nexus@80900";
            final String  endLineStr=" # ";
    
            JSch jsch = new JSch();     
            Session session = jsch.getSession(username, hostname, 22);
            session.setPassword(password);
            jsch.setKnownHosts("C://Users//gaikwasu//.ssh//known_hosts");
    
            session.connect();
            System.out.println("Connected");
    
    
            Channel channel=session.openChannel("shell");
            channel.connect();
    
            Expect expect=new ExpectBuilder()
                    .withOutput(channel.getOutputStream())
                    .withInputs(channel.getInputStream(), channel.getExtInputStream())
                    .withEchoOutput(System.out)
                    .withEchoInput(System.err)
                    .withExceptionOnFailure()
                    .build();
    
            expect.expect(Matchers.contains("-bash-4.1$"));    
            expect.send("devpush\n");
            expect.expect(Matchers.contains("[sudo] password for"));
            expect.send(password+"\n");
    
    
            DataInputStream dataIn = new DataInputStream(channel.getInputStream());
            DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());
            BufferedReader bufferReader= new BufferedReader(new InputStreamReader(dataIn));
    
            dataOut.writeBytes("cd custom\n");
            dataOut.writeBytes("ls -lrt\n");
            dataOut.flush();
    
            String line=bufferReader.readLine();
            while(!line.endsWith(endLineStr)) {
                   System.out.println(line);
              }
    
            channel.disconnect();
            session.disconnect();
            expect.close();
    
    
        }
    
        public static void main(String[] args) {
            ShellClient shellClient=new ShellClient();
            try {
                shellClient.loginToPabloServer();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSchException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

很可能是不定式循环的原因是因为当你在主线程中尝试相同的数据时,Expect实例同时在一个单独的线程中读取输入数据。

尝试在读取数据之前关闭Expect实例。这可能有所帮助。

但是,我强烈建议在发送密码后继续使用Expect实例。 ExpectIt可以帮助从远程命令输出中提取信息。例如:

    expect.sendLine(password);
    expect.expect(Matchers.contains("-bash-4.1$"));
    expect.sendLine("cd custom");
    expect.expect(Matchers.contains("-bash-4.1$"));
    expect.sendLine("ls -lrt");
    // the lsResult variable should contain the result of the 'ls -l' command
    String lsResult = expect.expect(Matchers.contains("-bash-4.1$")).getBefore();
    System.out.println(lsResult);

请查看this示例。