I'm attempting to write some Java code that connects to a linux workstation, executes a command with sudo and then blocks until the command is complete.
session.getOutputStream().write("sudo -s \n".getBytes());
session.getOutputStream().write(command.getBytes());
session.getOutputStream().write("\n exit\n".getBytes());
IOStreamConnector output = new IOStreamConnector();
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
output.connect(session.getInputStream(), bos);
String theOutput = bos.toString();
doesn't seem to wait for the program to complete or return back the output.
session.executeCommand(command);
IOStreamConnector output = new IOStreamConnector();
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
output.connect(session.getInputStream(), bos);
session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
theOutput = bos.toString();
Works well but not with sudo. I've tried setting the command:
"su -c \"touch /tmp/whatever\"" (touch is not the command I'm running, just using it to test.) This doesn't work.
"sudo touch /tmp/whatever" This doesn't work.
What is the magic formula for doing this???? Thanks in advance.