JSCH sudo su command “tty” error

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

Java - Jsch sudo command.

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

sudo su - bumboo 

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

Following is my code

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;     } 

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); 

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

any help appreciated.

回答1:

It worked for me

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!