Use JSch sudo example and Channel.setPty for running sudo command on remote host

后端 未结 3 1995
再見小時候
再見小時候 2020-12-01 17:54

I have used JSch Sudo example under following link:

http://www.jcraft.com/jsch/examples/Sudo.java.html

And changed it a bit and get rid of all the dialogs as

3条回答
  •  天命终不由人
    2020-12-01 18:12

    By default, SUDO is configured to require a TTY. That is, SUDO is expected to be run from a login shell.

    That's probably because your /etc/sudoers file (or any file it includes) has:

    Defaults requiretty
    

    But the option -S in sudo will make it read from Standard input.

    -S    The -S (stdin) option causes sudo to read the password from
          the standard input instead of the terminal device.  The pass-
          word must be followed by a newline character.
    

    Jsch exploits the same and tries to send the password. If you want to Jsch to work without prompting the password , you need to disable the requiretty from the sudoers file...using visudo command as follows

    Defaults !requiretty
    

    or

    #Defaults requiretty
    

提交回复
热议问题