本文介绍了从 Web 应用程序执行需要 sudo 的 shell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于 web 应用程序没有 su 权限,我想执行一个需要 sudo 的 shell 脚本.我还想避免让用户输入密码.我有哪些选择?这基本上就是我想要做的.

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.

@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 执行一个可运行的 jar.

iptool.sh executes a runnable jar.

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

我得到:

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

再次强调,Web 应用程序没有被授予任何 su 权限,我想避免向用户询问密码.

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.

我已经尝试将 keeboi ALL = NOPASSWD:/home/keeboi/Desktop/iptool.sh 添加到我的 /etc/sudoers 但它仍然需要密码.

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

更新

也添加了 keeboi ALL = NOPASSWD:/home/keeboi/Desktop/changeip.jar,没有骰子.

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

推荐答案

只需在/etc/sudoers中添加NOPASSWD:

just add NOPASSWD in /etc/sudoers:

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

这会关闭密码检查.

这篇关于从 Web 应用程序执行需要 sudo 的 shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 11:45