capture ncurses shell output in PHP

╄→尐↘猪︶ㄣ 提交于 2019-12-11 23:35:58

问题


okay so i dont know if this is possible and to be honest im leaning more towards the not possible end of the scale but i thought it might be worth asking.

Basically what i am wanting to know is if it is possible to capture shell output rendered with ncurses in php for use with tools such as htop.

i have noticed that php has a whole bunch of experimental ncurses functions but they all seem to be aimed at creating content not reading it. Ideally id like something where i could end up with something like

$output = ncurses_exec("htop --no-loop");

NOTE: im aware that htop doesnt have an option for --no-loop but i added it to make the program exit after the first rendering (rendering can be cleared or kept) just for testing purposes

Thanks in advance


回答1:


There is a solution:

Use Gnu Screen

Send commands to screen running in detached mode. Here is a quick-and-dirty example just to get you started:

<?php

// Start screen in detached mode, running htop
`screen  -d -m -S htop_session htop`;

 // let screen and htop start
sleep(1);

// Tell screen to save a screenshot in file 'hardcopy.0'
`screen -p 0 -S htop_session -X hardcopy`;

// Tell screen to quit
`screen -p 0 -S htop_session -X quit`;
?>

<pre>
    <?php print file_get_contents('hardcopy.0'); ?>
</pre>

Things to try

  • Experiment how to set a larger screen window size


来源:https://stackoverflow.com/questions/21191061/capture-ncurses-shell-output-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!