1

I know there are a lot of links on this topic but none of them exactly answered my problem.

From my Java program I need to exec the following command:

sudo nohup hcidump --raw|myProgram arg1 arg2

My approach:

 String[] cmd = {"/bin/sh", "-c", "sudo nohup hcidump --raw | myProgram arg1 arg2"};
Process p = Runtime.getRuntime().exec(cmd);

However, I get a usage error for sudo.

Any suggestions on how I can pipe this?

Ninja
  • 5,082
  • 6
  • 37
  • 59
  • Isn't it meant to be `./bin/sh`? Also, add a output stream to the process, record the output and report back. Is it straight away throwing an error in Java? If so, put inside a `try {} catch {}` statement and use `e.printStackTrace()`. – nyxaria Feb 20 '15 at 21:30
  • what's the error? do you know that sudo is just not granting the permission? – Jeffrey Blattman Feb 20 '15 at 21:35
  • possible duplicate of [How to make pipes work with Runtime.exec()?](http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec) – Jared Burrows Feb 20 '15 at 21:40
  • I don't think it's s a dupe - the problem here seems to be specific to the use of the `sudo` commmand, rather than in the pipe. – Jeen Broekstra Feb 20 '15 at 23:21

1 Answers1

2

Since I don't know the error you're encountering, I can only guess what the problem is. My guess is you are hitting a permissions issue.

Two options I can think of are to get the rights for hcidump lowered so your user can run them, or get your user rights to run sudo for that program. Since it is run in java you would probably want to have passwordless access to run sudo for that command. see this askubuntu post

Another possible option is to run your program as sudo and remove the sudo from the command. The problem with this option is if your program is exploitable, the malicious user now has sudo rights on your machine.

 String[] cmd = {"/bin/sh", "-c", "nohup hcidump --raw | myProgram arg1 arg2"};
Process p = Runtime.getRuntime().exec(cmd);
Community
  • 1
  • 1