Chinaunix

标题: 【已解决】使用ssh2_exec执行远程机器命令无法获取执行结果 [打印本页]

作者: chinaunixzcx    时间: 2009-09-06 20:11
标题: 【已解决】使用ssh2_exec执行远程机器命令无法获取执行结果
用ssh2_exec在远程主机上执行命令无法获取结果,但是在apache所在的本机上执行同样的命令可以正常获取结果,有人能指点一下吗?

[ 本帖最后由 chinaunixzcx 于 2009-9-8 13:49 编辑 ]
作者: chinaunixzcx    时间: 2009-09-06 21:15
没有人遇到过吗???
作者: chinaunixzcx    时间: 2009-09-06 22:20
谁能帮忙指点一下啊。。。
作者: chinaunixzcx    时间: 2009-09-07 10:04
有人遇到过这个问题吗???
作者: sunceenjoy    时间: 2009-09-07 10:14
本想试试看,发现没有编译这个模块。

你可以换个思路来处理啊。比如调用远程服务器的一个文件来激活,由该文件代理执行。
作者: chinaunixzcx    时间: 2009-09-07 10:41
标题: 回复 #5 sunceenjoy 的帖子
就是把命令放在远程主机的文件里面执行还是要获取结果,现在的问题就是无法获取结果
作者: chinaunixzcx    时间: 2009-09-07 11:18
刚刚又做了一个测试,我在远程主机是执行 echo 'aaaaaa' > /tmp/a.txt; 命令,远程主机上生成了a.txt 说明命令能正确执行,但是我执行cd /usr; ls ; 却没有结果返回。。。。
作者: HonestQiao    时间: 2009-09-07 18:38
你的整体程序流程是怎么样子的?贴上来看看。
作者: chinaunixzcx    时间: 2009-09-07 22:56
流程很简单
$connect=ssh2_connect("192.168.1.2",22);
ssh2_auth_password($connect,user,pass);
$stream=ssh2_exec($connect, "ls /home");
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
fclose($stream);
echo $output ;
作者: HonestQiao    时间: 2009-09-07 23:11
原帖由 chinaunixzcx 于 2009-9-7 22:56 发表
流程很简单
$connect=ssh2_connect("192.168.1.2",22);
ssh2_auth_password($connect,user,pass);
$stream=ssh2_exec($connect, "ls /home");
stream_set_blocking($stream, true);
$output = stream_get_ ...



<?php
$connect=ssh2_connect("www.superserver.com",22);
ssh2_auth_password($connect,'HonestQiao','123456');
$stream=ssh2_exec($connect, "cd /usr;ls;");
stream_set_blocking($stream, true);
$output = stream_get_contents($stream);
fclose($stream);
echo $output ;
?>



结果如下:
---------- PHP5 代码调试 ----------
bin
compat
db
games
home
include
lib
libdata
libexec
local
obj
ports
sbin
share
src

输出完成 (耗时 3 秒) - 正常终止
作者: HonestQiao    时间: 2009-09-07 23:12
我测试的结果完全正常。
要不你在最开始加上:
ini_set('display_errors', true);
ini_set('error_reporting', E_ALL);
作者: chinaunixzcx    时间: 2009-09-08 08:58
你代码中的www.superserver.com 是局域网内的一台机器还是远程的主机,我在局域网测试也没有问题,但是在广域网的远程主机是就无法获取到结果。
作者: bs    时间: 2009-09-08 09:39
外网 sleep()  ,直到返回

作者: chinaunixzcx    时间: 2009-09-08 13:43
查了一些资料最终用如下方法解决了,跟大家分享一下:

  1. <?php
  2. function user_exec($shell, $cmd, $max_time)
  3. {
  4.         $InputCmd = "echo \"[START]\";".$cmd."echo \"[END]\";";
  5.         fwrite($shell,$InputCmd."\n");
  6.         $output = "";
  7.         $start = false;
  8.         $start_time = time();
  9.         while(((time()-$start_time) < $max_time))
  10.         {
  11.                 $line = fgets($shell);
  12.                 if(!strstr($line,$InputCmd) && !preg_match('/echo/',$line) && !preg_match('/\"\[START\]\"/',$line) && !preg_match('/\"\[END\]\"/',$line))
  13.                 {
  14.                         if(preg_match('/\[START\]/',$line))
  15.                         {
  16.                                 //echo $line."==1<br>";
  17.                                 $start = true;
  18.                         }
  19.                         elseif(preg_match('/\[END\]/',$line))
  20.                         {
  21.                                 //echo $line."==2<br>";
  22.                                 return $output;
  23.                         }
  24.                         elseif($start)
  25.                         {
  26.                                 $output[] = $line;
  27.                                 //$output .= $line;
  28.                         }
  29.                 }
  30.         }
  31. }

  32. $SSH_Connect = ssh2_connect($MyHost,22);
  33. if ( !$SSH_Connect )
  34. {
  35.         echo "连接 [".$MyHost."] 失败!<br>";
  36.         exit(1);
  37. }
  38. $Ret=ssh2_auth_password($SSH_Connect,user,pass);
  39. if(!$Ret)
  40. {
  41.         echo "认证失败[".$MyHost."]!<br>";
  42.         exit(1);
  43. }
  44. if(!($shell = ssh2_shell($SSH_Connect, 'bash')))
  45. {
  46.         echo "fail: unable to establish shell<br>";
  47.         exit(1);
  48. }
  49. else
  50. {
  51.         $ReturnValue = user_exec($shell, $CMD, 5);
  52.         if($ReturnValue == "")
  53.         {
  54.                 $ResultString="无信息";
  55.         }
  56.         else
  57.         {
  58.                 $Num = sizeof($ReturnValue);
  59.                 for($i=0; $i<$Num; $i++)
  60.                 {
  61.                         $ResultString .= $ReturnValue[$i];
  62.                 }
  63.         }
  64.         echo $ResultString."<br>";
  65. }
  66. ?>
复制代码

[ 本帖最后由 chinaunixzcx 于 2009-9-8 13:48 编辑 ]
作者: HonestQiao    时间: 2009-09-08 13:54
我的测试时远程的,freebsd服务器端。
作者: bs    时间: 2009-09-08 15:17
原帖由 chinaunixzcx 于 2009-9-8 13:43 发表
查了一些资料最终用如下方法解决了,跟大家分享一下:





这手册上不就有了吗




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2