Note that there are some explanatory texts on larger screens.

plurals
  1. POJSch: what am i doing wrong, with JSCH tty code for executing interactive commands?
    primarykey
    data
    text
    <p>I'm trying to connect with JSch to a server and then execute an interactive <code>su</code> command.</p> <p>Could you please tell me what I am missing? This stuff hangs everytime, and I see no reason for that behavior. Especially I am specifically waiting for the password string ( actually <code>assword</code> without <code>:</code> ) before sending in the password.</p> <p>That is what I'm doing:</p> <ol> <li>logs in as user1/pass1</li> <li>then executes <code>su - user2 -c commandline</code>.</li> <li>pass the password in the channel input stream.</li> </ol> <p>Anyways, here's the main execute function, it takes user1/pass1, user2/pass2 and commandLine.</p> <pre><code>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() &gt; 0) { counter=0; //byteBuffer=null; counter=sessionOutput.read(byteBuffer, 0, byteBuffer.length); if(counter &lt; 0) { throw new java.io.IOException(); } standardOutBuffer += new String(byteBuffer,0,counter); if(standardOutBuffer.contains("assword")){break;} } /*if(sessionError.available() &gt; 0) { counter=0; //byteBuffer=null; counter=sessionError.read(byteBuffer, 0, byteBuffer.length); if(counter &lt; 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; } </code></pre> <p>Here is my error message:</p> <pre><code>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.&lt;init&gt;(Tester.java:12) at Tester.main(Tester.java:17) </code></pre> <p>and Here is the promptKeyboardInteractive method</p> <pre><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload