簡體   English   中英

PHP ssh2_exec通道退出狀態?

[英]PHP ssh2_exec channel exit status?

好的,所以pecl ssh2應該是libssh2的包裝器。 libssh2有libssh2_channel_get_exit_status。 有沒有辦法獲取這些信息?

我需要:
-stdout
-STDERR
-EXIT狀態

我得到了所有但退出狀態。 當ssh出現時,很多人都會在phplibsec周圍投擲,但是我認為沒有辦法讓stderr或者頻道退出狀態:/有沒有人能夠得到這三個?

所以,首先要做的是:
不,他們沒有實現libssh2_channel_get_exit_status。 為什么? 超越我。

這是id做了什么:

$command .= ';echo -e "\n$?"'

我填補了換行符和$的回聲? 在每個命令的結尾我執行。 瘦長? 是。 但似乎效果相當不錯。 然后我將其關閉到$ returnValue並在stdout結束時刪除所有換行符。 也許有一天會得到頻道的退出狀態得到支持,幾年之后它就會出現在發行版中。 就目前而言,這已經足夠了。 當您運行30多個遠程命令來填充復雜的遠程資源時,這比為每個命令設置和拆除ssh會話要好得多。

我試圖改進Rapzid的答案。 為了我的目的,我在php對象中包裝了ssh2並實現了這兩個函數。 它允許我使用理智的異常捕獲來處理ssh錯誤。

function exec( $command )
{
    $result = $this->rawExec( $command.';echo -en "\n$?"' );
    if( ! preg_match( "/^(.*)\n(0|-?[1-9][0-9]*)$/s", $result[0], $matches ) ) {
        throw new RuntimeException( "output didn't contain return status" );
    }
    if( $matches[2] !== "0" ) {
        throw new RuntimeException( $result[1], (int)$matches[2] );
    }
    return $matches[1];
}

function rawExec( $command )
{
    $stream = ssh2_exec( $this->_ssh2, $command );
    $error_stream = ssh2_fetch_stream( $stream, SSH2_STREAM_STDERR );
    stream_set_blocking( $stream, TRUE );
    stream_set_blocking( $error_stream, TRUE );
    $output = stream_get_contents( $stream );
    $error_output = stream_get_contents( $error_stream );
    fclose( $stream );
    fclose( $error_stream );
    return array( $output, $error_output );
}

暫無
暫無

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

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