0

I wrote a small client/server application that can execute system commands remotely. It works fine with non interactive programs when you only need to read their output. However, is there a reliable way to send user input to the Process started IF it requires it? How do I even find out whether a process prompts for any input?

Ree
  • 6,061
  • 11
  • 48
  • 50
  • Have you looked at using SSH? (via a Java library like jSSH) It looks like what you are trying to do is very similar. Perhaps you can take a few ideas from how it works (or just use it as you don't need Java installed on the server and it works) – Peter Lawrey Dec 15 '11 at 08:34
  • I don't really need SSH, it looks too complicated for the simple system that I have. I'd like to implement this using the standard Java libraries (if that is possible at all). – Ree Dec 15 '11 at 08:44
  • There are Java libraries which support SSH. You need an SSH server which is trivial for Linux and fairly easy for Windows. – Peter Lawrey Dec 15 '11 at 08:50
  • I don't have complete control over environments my system will run in. SSH is out of the question, actually. – Ree Dec 15 '11 at 08:55

1 Answers1

1

Take a look at 'expectj'. This is a port of http://en.wikipedia.org/wiki/Expect

http://expectj.sourceforge.net/

Example:

// Create a new ExpectJ object with a timeout of 5s
ExpectJ expectinator = new ExpectJ(5);

// Fork the process
Spawn shell = expectinator.spawn("/bin/sh");

// Talk to it
shell.send("echo Chunder\n");
shell.expect("Chunder");
shell.send("exit\n");
shell.expectClose();
laher
  • 8,860
  • 3
  • 29
  • 39
  • The problem is the user executing a remote program via my client doesn't know what to "expect". He should be prompted for input whenever a remotely executed process requires it. – Ree Dec 15 '11 at 08:48
  • Oh I think I see now what you've done. OK, you can just read from the output stream of the Process. e.g. System.exec() returns a Process, which gives an output stream as follows: Process.getOutputStream(). Then you'd send the user input to the process's InputStream. Makes sense? – laher Dec 15 '11 at 10:14
  • How do I know I need to send any input? – Ree Dec 15 '11 at 11:38
  • err, you'd forward the data from the output stream back to the user, and they would decide as usual... hmm, I'm starting to wonder if we're on the same page here... – laher Dec 15 '11 at 19:13