4

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 but this require that i put some password...like:

sudo /usr/bin/some_process
please enter your password:  

I used to implement remote cmd execution in this way:

Session sess = conn.openSession();

sess.execCommand("uname -a && date && uptime && who");

System.out.println("Here is some information about the remote host:");

InputStream stdout = new StreamGobbler(sess.getStdout());

BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

while (true)
{
  String line = br.readLine();
  if (line == null)
  break;
  System.out.println(line);
}


System.out.println("ExitCode: " + sess.getExitStatus());

sess.close();

conn.close();

I'm afraid that it's not possible to execute commands that require a password with this library.

Someone could give me a solution or alternative to allow this ???

Thx!

1
  • The title of this post is a little misleading – it sounds like you're having password problems with ssh, but you're actually having password problems inside the ssh session. Consider adding something like 'remote sudo password entry' in the topic and/or tags. Commented Mar 2, 2011 at 14:29

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.