2

I'm trying to execute below code to execute sudo commands but I do not know how execute commands after sudo login

String[] commands = {"sudo su - myname;","id"};
JSch jsch = new JSch();
String username = "myuser";
com.jcraft.jsch.Session session = 
        jsch.getSession(username,"hostname", 22);
session.setPassword("my@123");
session.connect();
Channel channel=session.openChannel("exec");
for(int a=0;a<=commands.length;a++){
    ((ChannelExec)channel).setCommand("sudo su - myname;");
    ((ChannelExec)channel).setErrStream(System.err);
    ((ChannelExec) channel).setPty(true);
    channel.connect();
    System.out.println("id *******");
    OutputStream out=channel.getOutputStream();
    out.write(("my@123\n").getBytes());
    out.flush();
    InputStream in=channel.getInputStream();
    byte[] tmp=new byte[1024];
    while(true){
        while(in.available()>0){
            int i=in.read(tmp, 0, 1024);
            if(i<0)break;
            System.out.print(new String(tmp, 0, i));
        }
        if(channel.isClosed()){
            System.out.println("exit-status: "+channel.getExitStatus());
            break;
        }
    }
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Srinivas
  • 21
  • 2

2 Answers2

1

The sudo su executes a new shell.

To provide a command to the shell you either:

In general, I recommend the first approach as it uses a better defined API (command-line argument).

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
-1
public static void testCh() throws JSchException, IOException, InterruptedException {

    int port=22;
    String name ="username";
    String ip ="ipHost";
    String password ="******";

    JSch jsch = new JSch();
    Session session = jsch.getSession(name, ip, port);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
    System.out.println("Establishing Connection...");
    session.connect();
    System.out.println("Connection established.");

    ChannelExec channelExec = (ChannelExec)session.openChannel("exec");

    InputStream in = channelExec.getInputStream();

    channelExec.setCommand("sudo su - myroot");
    channelExec.connect();
    OutputStream out = channelExec.getOutputStream();
    out.write(("cd /my/sub/directory/\n").getBytes());
    out.write(("ls -lrt *file_pattern* | tail -1\n").getBytes());
    out.flush();
  
    
    byte[] bt = new byte[1024];
    while (true) {
        while (in.available() > 0) {
            int i = in.read(bt, 0, 1024);
            if (i < 0) {
                break;
            }
            String str = new String(bt, 0, i);
            //displays the output of the command executed.
            System.out.print(str);

        }
        if (channelExec.isClosed()) {
            break;
        }
sadati boina
  • 625
  • 7
  • 10
  • This has numerous issues. 1) `StrictHostKeyChecking=on` is a security flaw. 2) The code can deadlock. See [How to read JSch command output?](https://stackoverflow.com/q/6902386/850848). 3) You are missing `\n` after the `cd` command. 4) `Thread.sleep` should not be needed and it's definitely a hackish approach. – Martin Prikryl Feb 09 '22 at 13:06