Executing a shell script that requires sudo from a

2019-07-29 12:00发布

Given that the web application doesn't have su privileges, I'd like to execute a shell script that requires sudo. I'd also like to avoid having the user input the password. What are my options? This is basically what I'm trying to do.

Wicket Application (Form for changing the IP)

@Override
protected void onSubmit() {
    System.out.println("Submitted");            
    try {
        Shell.updateIp("eth0", "192.168.217.129");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Shell.java

public static void updateIp(String ethernetInterface, String ip) throws IOException {
    ProcessBuilder builder = new ProcessBuilder("/home/keeboi/Desktop/iptool.sh", ethernetInterface, ip);
    Process child = builder.start();
    Network.readOutput(child);
    child.destroy();
}

iptool.sh executes a runnable jar.

iptool.sh

#!/bin/sh
sudo java -jar changeip.jar $1 $2

And I'm getting:

sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts

Again, I'd like to emphasize that the web app isn't given any su privileges and I'd like to avoid asking the user for password.

Edit:

I've already tried adding keeboi ALL = NOPASSWD: /home/keeboi/Desktop/iptool.sh to my /etc/sudoers but it still requires a password.

Update

Added keeboi ALL = NOPASSWD: /home/keeboi/Desktop/changeip.jar too, no dice.

1条回答
唯我独甜
2楼-- · 2019-07-29 12:46

just add NOPASSWD in /etc/sudoers:

user     ALL = NOPASSWD: /home/keeboi/Desktop/iptool.sh

That switches password check off.

查看更多
登录 后发表回答