Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ssh检测流结束_Php_Ssh_Stream - Fatal编程技术网

Php ssh检测流结束

Php ssh检测流结束,php,ssh,stream,Php,Ssh,Stream,我无法从ssh连接检测流的结尾。这就是我所尝试的: <pre><?php if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); if(!($con = ssh2_connect("localhost", 22))) echo "fail: unable to establish connection\n"; else if(!$ssh = ssh2_au

我无法从ssh连接检测流的结尾。这就是我所尝试的:

<pre><?php
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
if(!($con = ssh2_connect("localhost", 22)))
    echo "fail: unable to establish connection\n";
else
    if(!$ssh = ssh2_auth_password($con, "viassh", "sshpass")) {
        echo "fail: unable to authenticate\n";
    } 

        $sshStream = ssh2_shell($con, 'xterm', null, 80, 24, SSH2_TERM_UNIT_CHARS);
        $strCommand = 'cd /var/www/test';
        fwrite($sshStream, $strCommand . PHP_EOL);
        stream_set_blocking($sshStream, true);

        // infinite loop
        #$stderr_stream = fgets(ssh2_fetch_stream($sshStream, SSH2_STREAM_STDERR), 8192);
        #var_dump($stderr_stream);

        // infinite loop
        #var_dump(stream_get_contents($sshStream));

        $ret = "";
        while ($buf = fgets($sshStream)) {
            $ret .= $buf;

            // always false
            #$meta = stream_get_meta_data($sshStream);
            #var_dump($meta['eof']);

            // works
            if(strpos($buf, '$ cd ') !== false) break;
        }
        $strCommand = 'git status';
        fwrite($sshStream, $strCommand . PHP_EOL);
        stream_set_blocking($sshStream, true);
        while ($buf = fgets($sshStream)) {
            $ret .= $buf;
            if (strpos($buf, '-a') !== false) break;
        }
        fclose($sshStream);
        echo $ret;


?>
但这只是因为我知道小溪的最后一条线。我尝试了feof(),但也失败了。
所以希望有人能帮我。

看来你也可以很容易地做到这一点



也许我不明白你想做什么?

看起来这是已知的行为


你想干什么

我知道这是一个老问题,但我想添加我的输入,因为我在网上找不到可用的代码段。phpseclib并不总是一个合适的替代品——特别是,它缺乏一种按顺序执行命令的可靠方法。我发现PHP扩展更可靠,也更易于使用

我的解决方案如下所示,在我们的环境中似乎非常有效。原理很简单:要求系统在输出前打印开始消息,在输出后打印结束消息(base64用于此,但还有很多其他方法)。函数等待结束,然后返回在标记之间打印到控制台的内容


你试过fread或stream_get_内容吗?它在脚本中被注释掉了。我总是得到一个无限循环。
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-32-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/

20 packages can be updated.
0 updates are security updates.

Last login: Tue Oct 23 16:45:20 2012 from localhost

cd /var/www/test
]0;viassh@rcdev: ~viassh@rcdev:~$ cd /var/www/test
git status
]0;viassh@rcdev: /var/www/testviassh@rcdev:/var/www/test$ git status
# On branch develop
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#   modified:   ssh_1.php
#   modified:   ssh_1.php~
#   modified:   ssh_shell.php
#   modified:   ssh_shell.php~
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#   ssh_stream1.php
#   ssh_stream1.php~
no changes added to commit (use "git add" and/or "git commit -a")
<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('git status');
?>
function executeCommand($connection, $command)
    {
        $shell = ssh2_shell($connection, 'xterm');
        sleep(1);
        stream_set_blocking($shell, true);
        printf("Running command: %s\n\n", $command);
        fwrite($shell, 'echo "LS0tLUJFR0lOLS0tLQo=" | base64 --decode;'.$command.';printf "\n";echo "LS0tLUVORC0tLS0K" | base64 --decode;'.PHP_EOL);

        $output = '';
        $output_start = false;
        while (true) {
            $buffer = fgets($shell);
            flush();
            if(strpos($buffer, "----BEGIN----") !== false) {
                $output_start = true;
                continue;
            }
            if(strpos($buffer, "----END----") !== false) {
                break;
            }

            if($output_start) {
                printf("%s\n", $buffer);
                $output .= $buffer."\n";
            }
        }
        print("Finished executing command\n");
        fclose($shell);
        print("Shell closed\n");
        return trim($output);
    }