JSCH sudo su命令"tty"错误 [英] JSCH sudo su command "tty" error

查看:170
本文介绍了JSCH sudo su命令"tty"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java-Jsch sudo命令.

Java - Jsch sudo command.

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

I am using Jsch and my task is to login to server and run command as following

sudo su - bumboo

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

Using following code i am successfully able to connect but when i try to run command it gives me error 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可用示例

jsch in sudo.java available example they advised to use

// 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"的命令时,仍然会给我同样的错误

but when i run command like "sudo -S -p - su bamboo" still gives me the same error

任何帮助表示赞赏.

推荐答案

它对我有用

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

这篇关于JSCH sudo su命令"tty"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆