title: Library JSch, Sudo script does not work for Centos
body: <p>I'm using the JSсh library. I need to shutdown the computers Linux OS (Ubuntu, Fedorо, Centos). There are no problems with Ubuntu and Fedoro. But with Centos problems.
  I can shutdown only under the user "root". But if I use the user "john" with the rights of the superuser, the script hangs on entering the password and nothing happens. I will be very grateful for any help.
  Below I present the code</p>
  
  <pre><code>public class Sudo {
  private static final int timeout = 10000;
  public static String sudo_pass = "john123";
  
  public static void main(String[] arg) {
          try {
              JSch jsch = new JSch();
              String user = "john";
              String host = "192.168.56.103";
              Session session = jsch.getSession(user, host, 22);
              UserInfo ui = new MyUserInfo();
              session.setUserInfo(ui);
              session.connect(timeout);
  
              String command = "sudo poweroff";
  
              ChannelExec channel = (ChannelExec) session.openChannel("exec");
  
              // 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.
  
              channel.setCommand("sudo -S -p '' " + command);
              channel.setPty(true);
  
              InputStream in = channel.getInputStream();
              OutputStream out = channel.getOutputStream();
  
              channel.setErrStream(System.err);
  
              channel.connect();
  
              out.write((sudo_pass + "\n").getBytes());
              out.flush();
  
              byte[] tmp = new byte[1024];
              while (true) {
                  while (in.available() &gt; 0) {
                      int i = in.read(tmp, 0, 1024);
                      if (i &lt; 0) break;
                      System.out.print(new String(tmp, 0, i));
                  }
                  if (channel.isClosed()) {
                      System.out.println("exit-status: " + channel.getExitStatus());
                      break;
                  }
                  try {
                      Thread.sleep(1000);
                  } catch (Exception ee) {
                  }
              }
              channel.disconnect();
              session.disconnect();
          } catch (Exception e) {
              System.out.println(e);
          }
  
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/UKV7G.png" rel="nofollow noreferrer">enter image description here</a></p>