无法使用PHP和bash脚本删除Linux用户

时间:2023-02-14 15:43:06

I'm able to delete linux users directly from the shell using the command ./del_user.sh

我可以使用命令./del_user.sh直接从shell中删除linux用户

del_user.sh is as below

del_user.sh如下

#!/bin/sh
userdel username

When I run this script from a PHP page, it doesn't work.

当我从PHP页面运行此脚本时,它不起作用。

echo shell_exec('./del_user.sh');

Both the files are in the same directory.

两个文件都在同一目录中。

What could be the problem?

可能是什么问题呢?

1 个解决方案

#1


1  

  1. del_user.sh does not take an argument so you're running the script on whatever hardcoded user is in the script. This won't work unless you modify the script to allow a command line argument.
  2. del_user.sh不接受参数,因此您在脚本中的任何硬编码用户上运行脚本。除非您修改脚本以允许命令行参数,否则这将不起作用。

  3. While this task can be accomplished by PHP, this is certainly not a primary function and should probably be delegated to a more suitable application.
  4. 虽然这个任务可以通过PHP完成,但这当然不是主要功能,应该可以委托给更合适的应用程序。

As an aside, you haven't stated what your goal is or what this script is supposed to do. Is this part of some user management application or is it simply a one-off for some small task?

顺便说一句,你没有说明你的目标是什么,或者这个脚本应该做什么。这是一些用户管理应用程序的一部分还是仅仅是一次性的小任务?

Answer: To enable this, you'd have to give Apache the ability to sudo so it can gain temporarily raised priveleges. You may have to do some googling on how to do this depending on your OS but this link provides some direction on how to do it in Ubuntu: Ubuntu Forums

答:为了实现这一点,你必须让Apache具备sudo的能力,这样才能获得暂时提升的权限。您可能需要在Google上搜索有关如何执行此操作的内容,具体取决于您的操作系统,但此链接提供了有关如何在Ubuntu中执行此操作的一些方向:Ubuntu论坛

I would also recommend against using a bash script as its not really necessary to do this. You could use a PHP script that accepts a command line argument. So in your main script you could have something like this: shell_exec('php /path/to/del_user.php username');

我还建议不要使用bash脚本,因为它不是必需的。您可以使用接受命令行参数的PHP脚本。所以在你的主脚本中你可以有这样的东西:shell_exec('php /path/to/del_user.php username');

Then in del_user.php you'd have something like shell_exec('userdel '.$argv[1]);. More info on commandline arguments can be found here: $argv

然后在del_user.php中你会有类似shell_exec('userdel'。$ argv [1]);.有关命令行参数的更多信息,请访问:$ argv

Lastly, you could put it directly into your main script instead of using shell_exec twice. In other words, just use shell_exec('userdel '.$username); instead of calling a script. Since Apache will be able to sudo, it should be able to execute this directly.

最后,您可以将其直接放入主脚本中,而不是两次使用shell_exec。换句话说,只需使用shell_exec('userdel'。$ username);而不是调用脚本。由于Apache能够sudo,它应该能够直接执行它。

#1


1  

  1. del_user.sh does not take an argument so you're running the script on whatever hardcoded user is in the script. This won't work unless you modify the script to allow a command line argument.
  2. del_user.sh不接受参数,因此您在脚本中的任何硬编码用户上运行脚本。除非您修改脚本以允许命令行参数,否则这将不起作用。

  3. While this task can be accomplished by PHP, this is certainly not a primary function and should probably be delegated to a more suitable application.
  4. 虽然这个任务可以通过PHP完成,但这当然不是主要功能,应该可以委托给更合适的应用程序。

As an aside, you haven't stated what your goal is or what this script is supposed to do. Is this part of some user management application or is it simply a one-off for some small task?

顺便说一句,你没有说明你的目标是什么,或者这个脚本应该做什么。这是一些用户管理应用程序的一部分还是仅仅是一次性的小任务?

Answer: To enable this, you'd have to give Apache the ability to sudo so it can gain temporarily raised priveleges. You may have to do some googling on how to do this depending on your OS but this link provides some direction on how to do it in Ubuntu: Ubuntu Forums

答:为了实现这一点,你必须让Apache具备sudo的能力,这样才能获得暂时提升的权限。您可能需要在Google上搜索有关如何执行此操作的内容,具体取决于您的操作系统,但此链接提供了有关如何在Ubuntu中执行此操作的一些方向:Ubuntu论坛

I would also recommend against using a bash script as its not really necessary to do this. You could use a PHP script that accepts a command line argument. So in your main script you could have something like this: shell_exec('php /path/to/del_user.php username');

我还建议不要使用bash脚本,因为它不是必需的。您可以使用接受命令行参数的PHP脚本。所以在你的主脚本中你可以有这样的东西:shell_exec('php /path/to/del_user.php username');

Then in del_user.php you'd have something like shell_exec('userdel '.$argv[1]);. More info on commandline arguments can be found here: $argv

然后在del_user.php中你会有类似shell_exec('userdel'。$ argv [1]);.有关命令行参数的更多信息,请访问:$ argv

Lastly, you could put it directly into your main script instead of using shell_exec twice. In other words, just use shell_exec('userdel '.$username); instead of calling a script. Since Apache will be able to sudo, it should be able to execute this directly.

最后,您可以将其直接放入主脚本中,而不是两次使用shell_exec。换句话说,只需使用shell_exec('userdel'。$ username);而不是调用脚本。由于Apache能够sudo,它应该能够直接执行它。