Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try to use <code>sudo -S -p</code>.</p> <p>other way I used <code>JSch</code> class located in <code>jsch-0.1.38.jar</code>.</p> <p>The idea is to redirect <code>sudo</code> input from console to java code. </p> <p><strong>SudoExec class</strong></p> <pre><code>public abstract class SudoExec { private String mHost; private static String passwd; private SSHObserverItf mObserver = null; protected boolean isForceStop = false; protected boolean isAsIs = false; protected Timer mTimer = null; //default constructor public SudoExec(String hostName,String userName,String password){ setHost(userName+"@"+hostName); setPassword(password); } public void init(int timeToWait) { mTimer = new Timer(); new Thread(){ public void run(){ execCMD(); } }.start(); mTimer.doWait(timeToWait); isForceStop = true; } private void execCMD (){ isForceStop = false; try{ JSch jsch=new JSch(); String host=getHost(); String user=host.substring(0, host.indexOf('@')); host=host.substring(host.indexOf('@')+1); Session session=jsch.getSession(user, host, 22); // username and password will be given via UserInfo interface. UserInfo ui=new MyUserInfo(); session.setUserInfo(ui); session.connect(); String command=getCmd(); Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setPty(true); if(isAsIs == true){ ((ChannelExec)channel).setCommand(command); } else{ ((ChannelExec)channel).setCommand("sudo -S -p '' " + command); } InputStream in=channel.getInputStream(); OutputStream out=channel.getOutputStream(); ((ChannelExec)channel).setErrStream(System.err); channel.connect(); out.write((passwd+"\n").getBytes()); out.flush(); byte[] tmp=new byte[1024]; while(true &amp;&amp; isForceStop == false){ while(in.available()&gt;0 ){ int i=in.read(tmp, 0, 1024); if(i&lt;0)break; mObserver.onResponse((new String(tmp, 0, i))); } if(channel.isClosed()){ mObserver.onResponse("exit-status: "+channel.getExitStatus()); mTimer.doNotify(); break; } try{Thread.sleep(100);}catch(Exception ee){} } mObserver.onResponse("close channel ... "); channel.disconnect(); mObserver.onResponse("close session ... "); session.disconnect(); } catch(Exception e){ System.out.println(e); mObserver.onErrorResponse(e.getMessage()); } } public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ public String getPassword(){ return passwd; } public boolean promptYesNo(String str){ return true; } public String getPassphrase(){ return null; } public boolean promptPassphrase(String message){ return true; } public boolean promptPassword(String message){ return true; } public void showMessage(String message){ } @Override public String[] promptKeyboardInteractive(String arg0, String arg1, String arg2, String[] arg3, boolean[] arg4) { return null; } } public void setPassword(String password){ passwd=password; } public void setHost(String hostname){ mHost=hostname; } public String getPassword(){ return passwd; } public String getHost(){ return mHost; } protected abstract String getCmd(); public void setObserver(SSHObserverItf observer) { mObserver = observer; } } </code></pre> <p><strong>SSHObserverItf interface</strong></p> <pre><code>public interface SSHObserverItf { public void onResponse(String line); public void onErrorResponse(String line); } </code></pre> <p><strong>SomeTask</strong></p> <pre><code>public class SomeTask extends SudoExec implements SSHObserverItf{ private static String command = ""; private static String hostname = ""; private static String user = ""; private static String pass = ""; private static Boolean isError=false; private static String wait = "300"; static public void main(String args[]) throws IOException, ParseException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { new SomeTask(hostname,user,pass); if (isError == true){ System.out.println("Test failed"); } else{ System.out.println("\nSucceeded to invoke command : " + command); } } public CopyPeriodMergeToExternal(String hostName, String userName, String password) throws IOException, ParseException { super(hostName, userName, password); SSHObserverItf observer = this; super.setObserver(observer); super.init(Integer.parseInt(wait) * 1000); } @Override protected String getCmd() { isAsIs = true; command="rm -f somescript.sh"; System.out.println("Run followed command : " + command); return command; } @Override public void onResponse(String line) { System.out.println(line); } @Override public void onErrorResponse(String line) { System.out.println(line); System.out.println("Error has occured"); isError = true; } } </code></pre> <p>The main part in <code>SudoExec</code> class is:</p> <pre><code>public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{ public String getPassword(){ // &lt;--- return passwd; } public boolean promptYesNo(String str){ return true; // &lt;--- } </code></pre> <p>Hope it will solve your problem</p>
    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.
    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