- 论坛徽章:
- 0
|
查了一些资料最终用如下方法解决了,跟大家分享一下:
- <?php
- function user_exec($shell, $cmd, $max_time)
- {
- $InputCmd = "echo \"[START]\";".$cmd."echo \"[END]\";";
- fwrite($shell,$InputCmd."\n");
- $output = "";
- $start = false;
- $start_time = time();
- while(((time()-$start_time) < $max_time))
- {
- $line = fgets($shell);
- if(!strstr($line,$InputCmd) && !preg_match('/echo/',$line) && !preg_match('/\"\[START\]\"/',$line) && !preg_match('/\"\[END\]\"/',$line))
- {
- if(preg_match('/\[START\]/',$line))
- {
- //echo $line."==1<br>";
- $start = true;
- }
- elseif(preg_match('/\[END\]/',$line))
- {
- //echo $line."==2<br>";
- return $output;
- }
- elseif($start)
- {
- $output[] = $line;
- //$output .= $line;
- }
- }
- }
- }
- $SSH_Connect = ssh2_connect($MyHost,22);
- if ( !$SSH_Connect )
- {
- echo "连接 [".$MyHost."] 失败!<br>";
- exit(1);
- }
- $Ret=ssh2_auth_password($SSH_Connect,user,pass);
- if(!$Ret)
- {
- echo "认证失败[".$MyHost."]!<br>";
- exit(1);
- }
- if(!($shell = ssh2_shell($SSH_Connect, 'bash')))
- {
- echo "fail: unable to establish shell<br>";
- exit(1);
- }
- else
- {
- $ReturnValue = user_exec($shell, $CMD, 5);
- if($ReturnValue == "")
- {
- $ResultString="无信息";
- }
- else
- {
- $Num = sizeof($ReturnValue);
- for($i=0; $i<$Num; $i++)
- {
- $ResultString .= $ReturnValue[$i];
- }
- }
- echo $ResultString."<br>";
- }
- ?>
复制代码
[ 本帖最后由 chinaunixzcx 于 2009-9-8 13:48 编辑 ] |
|