JSch - How to issue commands as the user I have switched to
<p>I am using Jsch to remotely connect to a Linux server from Windows 7.</p>

<p>I have to </p>

<pre><code>(i) login to Linux server with my credentials 
(ii) switch to root using a pmrun command [this is the procedure in our environment], and
(iii) execute su command to switch as "someuser"
(iv) execute further commands as that "someuser"
</code></pre>

<p>I completed the steps (i) and (ii).</p>

<p>After completing step (ii), the InputStream from the channel shows the command-prompt as that of the root user.  So I assume that I have become the root user now.</p>

<p>Now, do I need to get the root user's session to further execute an su command, or is there a way to execute further commands as the root user?</p>

<p>Someone please help. Thanks.</p>

<p><strong>Update:</strong> Source code</p>

<pre><code>public class Pmrun {

    public static void main(String[] arg){
        try{
            JSch jsch=new JSch();

            String host=null;
            if(arg.length&gt;0){
                host=arg[0];
            }
            else{
                host="myself@linuxserver.com";
            }
            String user=host.substring(0, host.indexOf('@'));
            host=host.substring(host.indexOf('@')+1);

            Session session=jsch.getSession(user, host, 22);

            UserInfo ui=new MyUserInfo();
            session.setUserInfo(ui);
            session.connect();

            String command=  "pmrun su -";

            Channel channel=session.openChannel("exec");

            ((ChannelExec)channel).setCommand(command);

            InputStream in = channel.getInputStream();
            OutputStream outStream = channel.getOutputStream();
            ((ChannelExec)channel).setPty(true);
            ((ChannelExec)channel).setErrStream(System.err);

            channel.connect();
            TimeUnit.SECONDS.sleep(10);
            outStream.write("xxxxxxx\n".getBytes());
            outStream.flush();

            ((ChannelExec)channel).setCommand("su - someuser");


            byte[] tmp=new byte[1024];
            while(true){
                while(in.available()&gt;0){
                    int i=in.read(tmp, 0, 1024);
                    if(i&lt;0)break;
                    System.out.print(new String(tmp, 0, i));
                }
                if(channel.isClosed()){
                    System.out.println("exit-status: "+channel.getExitStatus());
                    break;
                }
                try{Thread.sleep(1000);}catch(Exception ee){}
            }
            channel.disconnect();
            session.disconnect();
        }
        catch(Exception e){
            System.out.println(e);
        }
    }

    public static class MyUserInfo implements UserInfo{
        public String getPassword(){ return passwd; }
        public boolean promptYesNo(String str){
            str = "Yes";
            return true;}

        String passwd;

        public String getPassphrase(){ return null; }
        public boolean promptPassphrase(String message){ return true; }
        public boolean promptPassword(String message){
            passwd="zzzzzzzz";
            return true;
        }
        public void showMessage(String message){

        }

    }


}
</code></pre>