0

When I enter the line:

sed -i 's/DNS1="8.8.8.8"/DNS1="8.8.4.4"/' /etc/sysconfig/network-scripts/ifcfg-eth0

I get the desired result (i.e. 8.8.8.8 is replaced with 8.8.4.4), however, I have a jave program that passes the same command as shown here:

public static void swapDns() throws IOException, InterruptedException 
{
    Runtime rt = Runtime.getRuntime();
    Process ps;

    String cmd[] = {"sed","-i","'s/DNS1=\"8.8.8.8\"/DNS1=\"8.8.4.4\"/'","/etc/sysconfig/network-scripts/ifcfg-eth0"};
    ps = rt.exec(cmd);
} 

and it doesn't work. It doesn't change anything. Is there an issue with sed/java, or am I missing something...

0

1 Answer 1

2

Does the Java program perhaps run with a different environment, perhaps one where sed is not in the path? Otherwise perhaps it runs as a user who does not have write permission to the network scripts?

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

5 Comments

Great!, you could make it setUID root, but running it as sudo is probably better practice. Please accept the answer if it works for you.
scratch that, i'm an idiot. it didn't work. I was running the sed command instead of the java program (facepalm)
So running the java program with sudo makes no difference?
Nope. I can always change the file with java completely but I do this fairly often in my current programs and sed would make thing easier.
Why dont you redirect STDERR to a file so you can see if there are errors? You could try running strace -f on your program and search for write calls to the file.

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.