0

When I try to execute a set of commands through Runtime.exec() in Java, some of them don't work. Nevertheless, when I manually execute the same commands in the terminal, they all work fine. I made sure to copy the exact same commands and they only work when executed manually. What can be causing the problem?

Here's my code:

rt = Runtime.getRuntime();
rt.exec("sudo wpa_cli -i " + ifName + " remove_network 0"); //Always removes network 0, in case it already exists
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " add_network 0");
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " set_network 0 ssid '\"" + SSID + "\"'");//This command only works when executed manually
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " set_network 0 psk '\"" + Pass + "\"'");//This command only works when executed manually
Thread.sleep(250);
rt.exec("sudo wpa_cli -i " + ifName + " select_network 0");

The commands where I input the SSID and Password are the ones causing trouble. ifName is the name of the NIC I am using.

Note that I added several Thread.sleep() because I wasn't sure if the commands had time to finish executing before I called the next one (as this is configuring a network and connecting to it, I really didn't know). Maybe they are completely useless, but I added them just in case.

EDIT: I know this is not related to quote duplication because I already tried removing the quotes and the code still doesn't work. I keep getting the same FAIL message from the console.

blastervla
  • 539
  • 6
  • 19
  • What is the command in linux? You seem to use both `'` and `"` in your command. Is this intended? – user140547 Oct 02 '16 at 21:44
  • Actually, only `"` should be used. Nevertheless, when executing it with only `"`, it always fails. I've read online that the solution is to execute it with both `'` and `"`, tested it and works like a charm (only when executed manually, of course). – blastervla Oct 02 '16 at 21:47
  • Possible duplicate of [Using Quotes within getRuntime().exec](http://stackoverflow.com/questions/161859/using-quotes-within-getruntime-exec) – user140547 Oct 02 '16 at 21:53
  • well then i guess it is a duplicate of the question linked. those quotes are only needed for the shell – user140547 Oct 02 '16 at 21:54
  • So this means I should be typing `rt.exec("sudo wpa_cli -i " + ifName + " set_network 0 ssid '" + SSID + "'");`? – blastervla Oct 02 '16 at 21:56
  • 1
    I didn't try that, actually I don't know that command, but maybe even `Runtime.getRuntime().exec(new String[] {"sudo", "wpa_cli", "-i", ifName, "set_network","0","ssid",SSID});` – user140547 Oct 02 '16 at 22:09
  • Unfortunately, it's still not working, so I guess the problem had nothing to with quote duplication. @user140547, I did try your suggestion above but it didn't work. Do you have any other ideas as to what could be happening? – blastervla Oct 02 '16 at 22:17
  • 1
    well did you try every combination? actually i am not an expert in the shell, but if you manually need to do `'"SSID"'`, the command maybe actually expects double-quotes and you could try `"\""+SSID+"\""` – user140547 Oct 02 '16 at 22:34
  • Thank you @user140547 for all your help, your last response actually did the trick! – blastervla Oct 02 '16 at 22:44

1 Answers1

0

Thanks @user140547 for all your help!

It ended up working by executing rt.exec(new String[] {"sudo", "wpa_cli", "-i", ifName, "set_network", "0", "psk", "\""+Pass+"\""});. It looks like when executing the command manually you have to put the password in between simple and double quotes, (e.g: '"ActualPassword"') but when using rt.exec() absolutely no simple quotes should be used and you should, actually, write down the double quotes.

So I guess the question didn't have to do with quote duplication after all, as I ended up having to write the quotes down. I'm still not sure though why this happens.

blastervla
  • 539
  • 6
  • 19
  • 1
    I think the problem is that "normal" commands usually don't expect arguments with quotes. Therefore the linked question has the usually correct answer. BUT, it seems that this specific command actually wants "SSID" as an argument. But if you write `"SSID"` in the shell, those quotes get removed and you end up with `SSID`. So if you write `'"SSID"'`, it works. But when using Java, you don't need the single quotes because you are not in the shell and you can write `"\""+SSID+"\""` – user140547 Oct 02 '16 at 22:48