簡體   English   中英

使用php ssh2_exec命令運行2個命令不起作用

[英]Run 2 Commands with php ssh2_exec command not working

我正在使用php執行linux命令。 但是當我嘗試一一執行2條命令時,第二條命令總是返回空流。 我的PHP函數或執行命令是:

    $file_name = $_GET['name'];
            $fielss = explode(".", $file_name);
            $year = substr($file_name, 0, 4);

            if(sizeof($fielss) == 1)
            {
                $command ='find /mnt/mediamanager/'.$year.' -name '.$file_name.'.'.'mxf';
            }
            else if(sizeof($fielss) == 2)
            {
                $ext = end(explode(".", $file_name));
                $command ='find /mnt/mediamanager/'.$year.' -name '.$fielss[0].'.'.$ext;
            }

// Command first
            $connection1 = $this->ssh->Create_connection('192.168.1.102', 'root', 'admin123');
            $status = $this->ssh->execute_command($connection1, $command);
            ssh2_exec($connection1, 'logout');
            ssh2_exec($connection1, 'exit');
            unset($connection1);

// Command second       
            $connection = $this->ssh->Create_connection('192.168.1.102', 'root', 'admin123');
            $dur_command = 'ffmpeg -i '.$status. '2>&1 | grep "Duration"';
            $duration = $this->ssh->execute_command($connection, $dur_command);

在這段代碼中,$ command和$ dur_command是我執行的兩個命令。 execute command是執行命令並返回流的函數。 Execute_command函數看起來像:-

function execute_command($connection, $command)
    {
        if (!($stream = ssh2_exec($connection, $command))) 
        {
            return FALSE;
        } 
        else 
        {
            $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
            stream_set_blocking($errorStream, true);
            stream_set_blocking($stream, true);
            // search file into the varibales.
            $file = stream_get_contents($stream);
            return $file;
        }
    }

第一個命令運行正常,但第二個命令execute_command函數始終返回null。 所以我怎么能用ssh2_exec執行2條命令。

感謝幫助

錯別字:

        $dur_command = 'ffmpeg -i '.$status. '2>&1 | grep "Duration"';
                                              ^^---no space

你在生產

 ffmpeg -i foo2>&1 | grep "Duration"

執行為

 ffmpeg -i foo2 >&1 | grep "Duration"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM