Execute a linux shell command with “sudo” using java, without entering the required password

后端 未结 3 1533
野性不改
野性不改 2020-12-06 08:20

Hi I\'m using ganymed-ssh2 java library to execute remote commands in linux workstation with success.

But now there is a situation where I need to execute a command

相关标签:
3条回答
  • 2020-12-06 08:50

    assuming there is a sess.getStdin(), you should be able to pipe in a password to the sudo command.

    0 讨论(0)
  • 2020-12-06 08:59

    The easiest solution would be to give the user that Java program logs in as the ability to run that sudo command without entering the password. You can do that by running visudo on the host and entering NOPASSWD after the given command. See man 8 visudo for more details.

    0 讨论(0)
  • 2020-12-06 09:03

    The easiest thing to do is probably to use public key authentication. That way you don't need a password.

    Edit: I was mislead that this question was about SSH, when in fact it's about sudo. In that case kojiro's solution is the correct one: edit the /etc/sudoers file, use the NOPASSWD flag.

    0 讨论(0)
提交回复
热议问题