2011-07-21 5 views
2
****UPDATE**** 
# The issue was within the wanpipemon script and the way that it interacts 
# with ioctl basically unless you want to edit the source for wanpipemon 
# and recompile it, it will not work. I have tried -S with root among 
# others and unless I am within console it throws a not authorized error 
# in the ioctl function. 

を印刷していない私は、コマンドを実行し、FXOライン電圧を出力し、簡単なシェルスクリプトを持って、通常は一度に1行を取得する必要がありますこれを1つの読み取り可能な出力に結合します。それは本当に完璧な素晴らしい作品PHPのもしくはshell_exec() - 動的な出力を印刷する、唯一の静的なエコーテキストに[OK]を

#!/bin/bash 

LINE1=$(wanpipemon -i w1g1 -c astats -m 1 | grep -m1 VOLT | cut -d ":" -f 2-2) 
LINE2=$(wanpipemon -i w1g1 -c astats -m 2 | grep -m1 VOLT | cut -d ":" -f 2-2) 
LINE3=$(wanpipemon -i w1g1 -c astats -m 3 | grep -m1 VOLT | cut -d ":" -f 2-2) 
LINE4=$(wanpipemon -i w1g1 -c astats -m 4 | grep -m1 VOLT | cut -d ":" -f 2-2) 
LINE5=$(wanpipemon -i w1g1 -c astats -m 5 | grep -m1 VOLT | cut -d ":" -f 2-2) 
LINE6=$(wanpipemon -i w1g1 -c astats -m 6 | grep -m1 VOLT | cut -d ":" -f 2-2) 

echo "Line 1 Voltage: $LINE1" 
echo "Line 2 Voltage: $LINE2" 
echo "Line 3 Voltage: $LINE3" 
echo "Line 4 Voltage: $LINE4" 
echo "Line 5 Voltage: $LINE5" 
echo "Line 6 Voltage: $LINE6" 

端子を介してマシン上でスクリプトを実行する:(この記事に関係のないが、私は誰かがこのスクリプトが何をするか尋ねるかもしれないと思った)

これは、シェルスクリプトです。私のPHPスクリプトでは、私は単純にやる、この(PHPスクリプトはshのファイルと同じサーバー上にある):

<?php 
    $output = shell_exec('/usr/sbin/linesta.sh'); 
    echo "<pre>$output</pre>"; 
?> 

私は、スクリプトを実行しているブラウザで受け取る出力は次のようになります。今

Line 1 Voltage: 
Line 2 Voltage: 
Line 3 Voltage: 
Line 4 Voltage: 
Line 5 Voltage: 
Line 6 Voltage: 

私はアクセス許可が正しいことを確認しました。それは、50%の方法で実行されていることを既に知っているはずです。スクリプトがマシン上で動作することがわかっているようです。

静的なテキストを出力しますが、動的なテキストを出力しないという事実は本当に奇妙です。 'wanpipemon -i w1g1 -c astats -m *'コマンドは、PBX上のアナログカードのドライバアプリケーションの一部であり、実行するためにrootを必要としません(どのユーザーでもコマンドを実行できます)。続行中です。

これまでのところ、私はコンソールスクリプト(linesta.shがあります)上のスクリプトを実行しているシェルスクリプトに対して二重チェックのアクセス権を試してきましたが、私は確信していませんそれ以外はテストする。私はexec()やsystem()のような他のPHPコマンドも全く同じ結果を試してみました。

+0

私は何をやってしまったことは、スクリプトを実行するcronジョブを割り当てたを取得するには、次の機能を使用しshell_exec、cronジョブは、スクリプトを実行し、出力を取り、中のファイルに書き込みますphpはreadfile経由で情報を引き出します。最高の解決策ではありませんが、それは私が推測するには十分です。ちょうど私がここで行ったことを追加して、あなたのアナログ回線を監視したい誰かが簡単にそうする方法を持つことができるように思った。 – BrandonS

答えて

2

wanpipemonは、PHPがスクリプトの実行に使用しているシェルのパスにありますか?

$output = shell_exec('/usr/sbin/linesta.sh 2>&1'); 
              ^^^^--- redirect stderr to stdout. 
+0

Ioctl ::オペレーションは許可されていません。phpスクリプトを実行するユーザレベルにwanpipemonを実行するのに十分な権限がないと思います。少なくとも、出力から取得しているものです。私はそれが私が見つけて見てみるアナログカードのドライバの一部であると言ったように、スクリプトが正直であることは本当に分からない。より高いレベルでPHPスクリプトを実行する上の任意のアドバイス? – BrandonS

+0

suPHPを使用すると、別のユーザーとしてスクリプトを実行したり、shell_exec呼び出しなどでsudoを使用したりすることができます。 –

+0

あなたは正しく、権限がありました。ルートを使用して-Sスイッチを使用しても(私はそれがちょうどテストでは安全ではないことは分かっています)、うまく動作しませんでしたwanpipemonがプログラムされた方法でしたが、あなたの解決策は私がさらに診断し、ありがとう。私はコンソールでそれを使用しなければならないと思う。 – BrandonS

0

特定のものの多くはである:型エラーはstderrに書かれたスクリプトで、あなたの文字列の建物に捕捉されていない、とあなたはstderrのリダイレクトをした場合を除きまた、PHPによって捕捉されないことになる「が見つかりませんファイル」私はすべての出力

/** 
* Executes the shell command and gets stdout and stderr streams 
* 
* @see http://www.php.net/manual/en/function.proc-open.php 
* @param string $cmd - command to be executed 
* @param string $cwd - folder 
* @param array $env - options 
* @return array() 
*/ 
function shexec($cmd, $cwd = './', $env = array()) 
{ 
    $return_value = array(
     "exit" => 1,  // exit 0 on ok 
     "stdout" => "",  // output of the command 
     "stderr" => "",  // errors during execution 
    ); 

    $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 
    ); 

    $process = proc_open(escapeshellcmd($cmd), $descriptorspec, $pipes, $cwd, $env); 
    // $pipes now looks like this: 
    // 0 => writeable handle connected to child stdin 
    // 1 => readable handle connected to child stdout 
    // 2 => readable handle connected to child stderr 

    if (false === is_resource($process)) 
    { 
     //echo("Sys::shexec() Error on proc_open, shell command \"$cmd\""); 
    } 
    else 
    { 
     $return_value['stdout'] = stream_get_contents($pipes[1]); 
     $return_value['stderr'] = stream_get_contents($pipes[2]); 

     fclose($pipes[0]); 
     fclose($pipes[1]); 
     fclose($pipes[2]); 

     // It is important that you close any pipes before calling 
     // proc_close in order to avoid a deadlock 
     $return_value['exit'] = proc_close($process); 
    } 

    if(trim($return_value['stderr']) !== "") { 
     //echo("Sys::shexec() \n\"$cmd\"\nERROR:\n" . $return_value['stderr']); 
    } 

    if(trim($return_value['stdout']) !== "") { 
     //echo("Sys::shexec() \n\"$cmd\"\nOUTPUT:\n" . $return_value['stdout']); 
    } 

    return $return_value; 

} // END FUNCTION shexec() 
関連する問題