【问题标题】:JSCH sudo su command "tty" errorJSCH sudo su 命令“tty”错误
【发布时间】:2012-12-22 15:13:33
【问题描述】:

Java - Jsch sudo 命令。

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

sudo su - bumboo

使用以下代码我可以成功连接,但是当我尝试运行命令时,它给了我错误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;
    }

他们建议使用的 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);

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

任何帮助表示赞赏。

【问题讨论】:

    标签: sudo jsch tty su


    【解决方案1】:

    它对我有用

    String command = "sudo su - bumboo";
    channel.setPty(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 2019-12-22
      相关资源
      最近更新 更多