简体   繁体   English

使用sudo从php运行shell命令

[英]running shell command from php with sudo

I want to run this code from php 我想从php运行此代码

echo <password> | sudo -S /usr/sbin/asterisk -rx "dongle show devices"

but it's not working. 但它不起作用。 Can anyone help? 有人可以帮忙吗?

You can just use the 'backtick' character (`) around your shell string like: 您可以在shell字符串周围使用'backtick'字符(`),例如:

<?php
    $output = `command_goes_here`;
    echo $output;
?>

Keep in mind this will only work if the shell_exec() function would work on that server, which could also be used in a similar way. 请记住,这仅在shell_exec()函数在该服务器上可用时才有效,这也可以类似的方式使用。

Use php function shell_exec or exec to execute shell commands 使用php函数shell_exec或exec执行shell命令

For more details 更多细节

http://www.php.net/shell_exec http://www.php.net/shell_exec

http://php.net/manual/en/function.exec.php http://php.net/manual/zh/function.exec.php

u can try by this if u want to run shell script command in php file 如果要在php文件中运行shell脚本命令,可以尝试此操作

$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";

u can also try other way by create .sh file for shell script and run that .sh file by php function 您还可以尝试通过其他方法为外壳脚本创建.sh文件,并通过php函数运行该.sh文件

$output = shell_exec('./deploy.sh');
echo "<pre>".$output."</pre>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM