PHP SSH2

function exec($cfg)
{
$conn = ssh2_connect($cfg[‘fip’], C(‘SSH_PORT’));
if (!$conn) return “cannot connect to server {$cfg}:”.C(‘SSH_PORT’);

$auth = ssh2_auth_password($conn, $cfg[‘flogin’], $cfg[‘floginpwd’]);
if (!$auth) return “auth failed”;

$stdout_stream = ssh2_exec($conn, C(‘REALTIME_CMD’));
//$stdio_stream = ssh2_shell($conn);

$stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);

stream_set_blocking($stderr_stream, true);
stream_set_blocking($stdout_stream, true);

$out = “”;
while($line = fgets($stdout_stream)) { flush(); $out .= $line;}

$err = “”;
while($line = fgets($err_stream)) { flush(); $err .= $line; }

fclose($stdout_stream);
fclose($stderr_stream);

return array($out, $err);
}
php_ssh2.dll