PHP网页执行bash,bash脚本执行从php和瞬时输出回到网页

我有一个bash和Perl脚本的集合

>开发在linux机器上部署所需的目录结构

>(可选)从svn导出代码

>从此源构建一个包

这在终端上运行良好.现在我的客户端请求此过程的Web界面.

例如,某个页面上的“Create New Package”按钮将逐个调用上述步骤,并将输出作为脚本回声返回给用户,而不是在整个脚本执行时.

是否有可能将bash脚本的瞬时输出发送到网页或php脚本,该脚本通过程序执行功能(system,exec,passthru ……或任何其他适合此流程的东西)调用它?

什么是优雅为什么这样做?

在做这样的事情时我应该采取哪些安全预防措施(如果可能的话)?

编辑

经过一些搜索,我找到了解决方案的一部分,但仍然没有工作:

$cmd = 'cat ./password.txt|sudo -S ./setup.sh ';

$descriptorspec = array(

0 => array("pipe", "r"), // stdin is a pipe that the child will read from

1 => array("pipe", "w"), // stdout is a pipe that the child will write to

2 => array("pipe", "w") // stderr is a pipe that the child will read from

);

flush();

$process = proc_open($cmd, $descriptorspec, $pipes, './', array());

echo "

";

if (is_resource($process)) {

while ($s = fgets($pipes[1])) {

print "Message:".$s;

flush();

}

while ($s = fgets($pipes[2])) {

print "Error:".$s;

flush();

}

}

echo "

";

输出:(网页)

Error:WARNING: Improper use of the sudo command could lead to data loss

Error:or the deletion of important system files. Please double-check your

Error:typing when using sudo. Type "man sudo" for more information.

Error:

Error:To proceed, enter your password, or type Ctrl-C to abort.

Error:

Error:Password:

Error:Sorry, try again.

Error:Password:

Error:Sorry, try again.

Error:Password:

Error:Sorry, try again.

Error:sudo: 3 incorrect password attempts**

我现在要做的第一个问题是通过sudo passoword

请帮忙 !

解决方法:

我会使用一种主/从设计.奴隶将是你的perl / bash脚本,只是做一份工作(打包,编译,导出代码等),并提供一个日志条目.

主人将是你的PHP进程.因此原理如下:主设备和从设备共享通信信道,并从该信道异步通信.

您可以想象一个数据库,如:

create table tasks ( id INT primary key, status INT, argument VARCHAR(100));

你的php页面应该切换用户选择,并过滤输入:

switch ($_GET['action']) {

case 'export':

$argument = sanitize($_GET['arg']);

add_task('export', $argument);

break;

case '...':

// ...

}

add_task函数可能是这样的:

function add_task($action, $arg)

{

return $db->add('tasks', array($action, NEW_TASK, $arg);

}

奴隶可以通过cron job运行,并查询数据库,提供任务的进展.

专业人士是:

>独立系统,缓解进化.

>如果客户端断开连接,则作业永远不会丢失

>更容易保护

缺点是:

>一开始有点复杂.

>反应性较低,因为奴隶的轮询时间在运行(例如,如果他们每5分钟运行一次)

>输出少于命令的直接输出

请注意,您可以实现xml-rpc之类的触发器来运行从属服务器,而不是使用消息传递系统.

标签:php,bash,perl,nonblocking

来源: https://codeday.me/bug/20190530/1182913.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值