2012-06-15 272 views
0

我試圖用JSch連接到服務器,然後執行交互式su命令。JSch:我做錯了什麼,用JSCH tty代碼來執行交互式命令?

你能告訴我我失蹤了嗎?這個東西每次都會掛起,我沒有看到這種行爲的原因。 尤其是我在發送密碼之前專門等待密碼字符串(實際上assword沒有:)。

這就是我在做什麼:

  1. 日誌以USER1/PASS1
  2. 然後執行su - user2 -c commandline
  3. 在通道輸入流中傳遞密碼。

無論如何,這裏是主要的執行功能,它需要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; 
} 
+0

人之間,這裏是錯誤消息: ___________ '異常線程 「main」 顯示java.lang.NullPointerException 在com.jcraft。 jsch.Buffer.putString(Buffer.java:59) at com.jcraft.jsch.UserAuthKeyboardInteractive.start(UserAuthKeyboardInteractive.java:183) at com.jcraft.jsch.Session.connect(Session.java:442) at com.jcraft.jsch.Session.connect(Session.java:162) at susessioncution.SUExec ute.execute(SUExecute.java:53) at Tester。 (Tester.java:12) at Tester.main(Tester.java:17)' _________________ –

+0

您可以顯示您的UInfo類的源代碼嗎?它看起來像錯誤在那裏。 –

+1

請不要在評論中放置代碼,它在那裏不可讀。改爲編輯你的問題。爲什麼你要創建一個length.length長度的數組,並且只填充它的第一個元素? –

回答

0

看來你的

UIKeyboardInteractive#promptKeyboardInteractive() 

實現返回字符串數組,其中包含'nu二'。

至於sudo的,我建議參考 http://www.jcraft.com/jsch/examples/Sudo.java.html

+0

謝謝,我會再次查看promptKeyboardInteractive黑客。從代碼中可以看出,使其非交互是一種令人不快的破解。 –

+0

在幾乎所有情況下,會話。setPassword(pstr)將適用於「鍵盤交互式」auth方法。你試過了嗎? – ymnk

+0

@paulo,嘿非常感謝您按順序重新排列問題。看起來更清晰。 –