首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用不起作用的php ssh2_exec命令运行2个命令

用不起作用的php ssh2_exec命令运行2个命令
EN

Stack Overflow用户
提问于 2015-07-16 02:12:12
回答 1查看 767关注 0票数 1

我正在使用php执行linux命令。我的php函数或执行命令是:

代码语言:javascript
复制
    $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命令是执行命令并返回流的函数。Execute_command函数如下所示:-

代码语言:javascript
复制
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执行两个命令呢?

感谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2015-07-16 02:22:53

拼写错误:

代码语言:javascript
复制
        $dur_command = 'ffmpeg -i '.$status. '2>&1 | grep "Duration"';
                                              ^^---no space

你在制作

代码语言:javascript
复制
 ffmpeg -i foo2>&1 | grep "Duration"

,它被执行为

代码语言:javascript
复制
 ffmpeg -i foo2 >&1 | grep "Duration"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31438008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档