JSch:我做错了什么,使用JSCH tty代码执行交互式命令?

时间:2012-06-15 16:58:34

标签: java ssh jsch

我正在尝试将JSch连接到服务器,然后执行交互式su命令。

你可以告诉我我错过了什么吗?这个东西每次都挂起,我认为没有理由这样做。 特别是我在发送密码之前专门等待密码字符串(实际上assword没有:)。

这就是我正在做的事情:

  1. 以user1 / pass1
  2. 身份登录
  3. 然后执行su - user2 -c commandline
  4. 在频道输入流中传递密码。
  5. 无论如何,这是主要的执行函数,它需要user1 / pass1,user2 / pass2和commandLine。

    public String execute (String host, String nuser, String npass,
                           String puser, String ppass, String commandLine)
        {
          try{
            synchronized(this) 
            {
              session = jsch.getSession(nuser,host,22);
              session.setConfig("StrictHostKeyChecking", "no");
              pstr=npass;
              //session.setPassword(npass);
              String authmethods=  session.getConfig("PreferredAuthentications");
              System.out.println(authmethods);
              UserInfo ui=new SUSessionExecution.UInfo();
              Thread.sleep(150);
              if(authmethods.contains("keyboard-interactive"))
              { System.out.print("keyboard-interactive"); session.setUserInfo(ui); } 
              else if ( authmethods.contains("password") )
              { System.out.print("password"); session.setPassword(pstr);  }
    
              session.connect();
              channel = session.openChannel("exec");
              nuser=null;
              npass=null;
    
    
              ((ChannelExec)channel).setPty(true);
              ((ChannelExec)channel).setPtyType("vt100");
              String command= "su - " + puser + " -c " + commandLine  + "\n";
              //((ChannelExec)channel).setCommand(commandLine);
    
              byte[] cmdBuffer=command.getBytes();
              ByteArrayInputStream bi = new ByteArrayInputStream(cmdBuffer);
              channel.setInputStream(bi);
    
              ByteArrayOutputStream bo = new ByteArrayOutputStream();
              ((ChannelExec)channel).setErrStream(bo);
    
              sessionOutput = channel.getInputStream();
              //sessionError = channel.getExtInputStream();
    
    
              channel.connect();
    
              session_open=true;
              // it is only here our session is fully functional.
    
              boolean sustatus;//=establishSU(commandIO, channel,puser,ppass);
              // NEEDS REPLACE
    
              //commandIO.write(command.getBytes());
              //commandIO.flush();
    
    
    
              String standardOutBuffer="";
              String standardErrBuffer="";
    
              int counter;
              byte[] byteBuffer = new byte[1024];
    
    
              while(sessionOutput.available() > 0)
                { counter=0; //byteBuffer=null;
                  counter=sessionOutput.read(byteBuffer, 0, byteBuffer.length);
                  if(counter < 0) { throw new java.io.IOException(); }
                  standardOutBuffer += new String(byteBuffer,0,counter);
                  if(standardOutBuffer.contains("assword")){break;}
                }
    
                /*if(sessionError.available() > 0)
                { counter=0; //byteBuffer=null;
                  counter=sessionError.read(byteBuffer, 0, byteBuffer.length);
                  if(counter < 0) { throw new java.io.IOException(); }
                  standardErrBuffer += new String(byteBuffer,0,counter);
                  if(standardErrBuffer.contains("assword")){break;}}*/
    
    
            commandIO = new PipedOutputStream();
            sessionInput = new PipedInputStream(commandIO);
            channel.setInputStream(sessionInput);
            commandIO.write(new String(ppass+"\n").getBytes());
            commandIO.flush();
    
            counter=0; standardOutBuffer="";
            while((counter = sessionOutput.read(byteBuffer,0,byteBuffer.length)) != -1)
            { standardOutBuffer += new String(byteBuffer,0,counter); }
    
            closeComponents();
            return standardOutBuffer;
            }
          }
          catch(com.jcraft.jsch.JSchException jse) 
          { session_open=false;su_space_open=false;jse.printStackTrace();
            closeComponents(); return null; }
          catch(java.io.IOException ioe)
          { session_open=false;su_space_open=false;ioe.printStackTrace();
            closeComponents(); return null; }
          catch(InterruptedException ie)
          { ie.printStackTrace(); }
    
            return null;
        }
    

    这是我的错误消息:

    Exception in thread "main" java.lang.NullPointerException
      at com.jcraft.jsch.Buffer.putString(Buffer.java:59)
      at com.jcraft.jsch.UserAuthKeyboardInteractive.start(UserAuthKeyboardInteractive.ja‌va:183)
      at com.jcraft.jsch.Session.connect(Session.java:442)
      at com.jcraft.jsch.Session.connect(Session.java:162)
      at susessionexecution.SUExecute.execute(SUExecute.java:53)
      at Tester.<init>(Tester.java:12)
      at Tester.main(Tester.java:17) 
    

    ,这是promptKeyboardInteractive方法

    public String[] promptKeyboardInteractive (String destination, String name,
                                               String instruction, String[] prompt,
                                               boolean[] echo)
    {
        System.out.println("\n"+prompt.length+"\n\n");
        String[] response=new String[prompt.length];
        response[0] = passwd;
        return response;
    }
    

1 个答案:

答案 0 :(得分:0)

似乎是你的

的实现
  UIKeyboardInteractive#promptKeyboardInteractive()

返回一个String数组,其中包含'null'。

至于sudo,我建议参考   http://www.jcraft.com/jsch/examples/Sudo.java.html