JSCH sudo su命令"tty"错误

d-m*_*man 10 sudo jsch su tty

Java - Jsch sudo命令.

我正在使用Jsch,我的任务是登录服务器并运行命令,如下所示

sudo su - bumboo
Run Code Online (Sandbox Code Playgroud)

使用以下代码我成功连接,但当我尝试运行命令时,它给了我错误 sudo: sorry, you must have a tty to run sudo

以下是我的代码

public static Channel sudoBamboo(Session session, String sudo_pass) throws Exception {

        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        //SUDO to bamboo user
        String command = "sudo su - bumboo";
        channel.setCommand(command);

        //InputStream in = channel.getInputStream();
        channel.setInputStream(null, true);

        OutputStream out = channel.getOutputStream();
        //channel.setErrStream(System.err);
        channel.setOutputStream(System.out, true);
        channel.setExtOutputStream(System.err, true);
        //Test change
        //channel.setPty(false);
        channel.connect();

        out.write((sudo_pass + "\n").getBytes());
        out.flush();

        return channel;
    }
Run Code Online (Sandbox Code Playgroud)

他们建议使用sudo.java中的jsch

// man sudo
      //   -S  The -S (stdin) option causes sudo to read the password from the
      //       standard input instead of the terminal device.
      //   -p  The -p (prompt) option allows you to override the default
      //       password prompt and use a custom one.
      ((ChannelExec)channel).setCommand("sudo -S -p '' "+command);
Run Code Online (Sandbox Code Playgroud)

但当我运行命令"sudo -S -p - su bamboo"仍然给我同样的错误

任何帮助赞赏.

d-m*_*man 11

它对我有用

String command = "sudo su - bumboo";
channel.setPty(true);
Run Code Online (Sandbox Code Playgroud)