为什么ssh2_exec的命令不会结束? [英] Why commands with ssh2_exec do not end?

查看:1145
本文介绍了为什么ssh2_exec的命令不会结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ssh2_exec远程运行命令时遇到问题。



当我使用wget或unzip时,命令应该执行,一些文件。



我需要知道的是如何确保我的ssh2_exec脚本在继续执行剩下的PHP代码之前完全执行。

  $ stream = ssh2_exec($ connection,'cd / home; wget http://domain.com/myfile.zip; unzip myfile。 zip; rm myfile.zip'); 

提前感谢!



我找到脚本,如何获得命令的结果?

 <?php 
$ ip ='ip_address';
$ user ='username';
$ pass ='password';

$ connection = ssh2_connect($ ip);
ssh2_auth_password($ connection,$ user,$ pass);
$ shell = ssh2_shell($ connection,bash);

// Trick是在开始和结束echos可以在* nix和Windows系统中执行。
//如果在Windows系统上,请将cmd / C添加到$ cmd的开头。
$ cmd =echo'[start]';您的命令在这里; echo'[end]';
$ output = user_exec($ shell,$ cmd);

fclose($ shell);

function user_exec($ shell,$ cmd){
fwrite($ shell,$ cmd。\\\
);
$ output =;
$ start = false;
$ start_time = time();
$ max_time = 2; // time in seconds
while(((time() - $ start_time)< $ max_time)){
$ line = fgets($ shell);
if(!strstr($ line,$ cmd)){
if(preg_match('/ \ [start\] /',$ line)){
$ start = true ;
} elseif(preg_match('/ \ [end \] /',$ line)){
return $ output;
} elseif($ start){
$ output [] = $ line;
}
}
}
}

?> Debian可能会认为软件包是好的,因为它是非常好的软件包,因为它是一个非常好的软件包。稳定(根据官方网站,自2012-10-15以来没有更改)。但我会说这不好。我将使用以下方法:

  $ command =ls -l 
$ user =user;
$ host =host;

if(!my_ssh_exec($ command,$ user,$ host)){
fprintf(STDERR,my_ssh_exec failed\\\
);
}


函数my_ssh_exec($ cmd,$ host,$ user){
$ result = true;

$ desc = [
1 => ['pipe','w'],
2 => ['pipe','w'],
];

// -tt强制TTY分配
$ ssh_cmd =ssh -tt $ user @ $ host - $ cmd;

$ proc = proc_open($ cmd,$ desc,$ pipes);

if(!is_resource($ proc)){
return false;
}

if($ error = stream_get_contents($ pipes [2])){
fprintf(STDERR,Error:%s\\\
,$ error);
$ result = false;
}
fclose($ pipes [2]);

if($ output = stream_get_contents($ pipes [1])){
printf(Output:%s\\\
,$ output);
}
fclose($ pipes [1]);

if($ exit_status = proc_close($ proc)){
fprintf(STDERR,命令以非零状态退出%d:%s\\\

$ exit_status,$ cmd);
$ result = false;
}

return $ result;
}

脚本应该在命令行界面中运行。


I have a problem running a command on my server remotely with ssh2_exec.

When I use wget or unzip, the command should execute but I get no result or only a few files.

What I need to know is how I can be sure that my ssh2_exec script will execute fully before proceeding to the rest of my PHP code.

$stream =ssh2_exec($connection, 'cd /home; wget http://domain.com/myfile.zip; unzip myfile.zip; rm myfile.zip');

Thanks in advance!

EDIT : I have found script, how get result of commands ?

<?php 
$ip = 'ip_address'; 
$user = 'username'; 
$pass = 'password'; 

$connection = ssh2_connect($ip); 
ssh2_auth_password($connection,$user,$pass); 
$shell = ssh2_shell($connection,"bash"); 

//Trick is in the start and end echos which can be executed in both *nix and windows systems. 
//Do add 'cmd /C' to the start of $cmd if on a windows system. 
$cmd = "echo '[start]';your commands here;echo '[end]'"; 
$output = user_exec($shell,$cmd); 

fclose($shell); 

function user_exec($shell,$cmd) { 
  fwrite($shell,$cmd . "\n"); 
  $output = ""; 
  $start = false; 
  $start_time = time(); 
  $max_time = 2; //time in seconds 
  while(((time()-$start_time) < $max_time)) { 
    $line = fgets($shell); 
    if(!strstr($line,$cmd)) { 
      if(preg_match('/\[start\]/',$line)) { 
        $start = true; 
      }elseif(preg_match('/\[end\]/',$line)) { 
        return $output; 
      }elseif($start){ 
        $output[] = $line; 
      } 
    } 
  } 
} 

?>

解决方案

Debian might think the package is good, because it's very stable(according to the official site there were no changes since 2012-10-15). But I'll say it's not good. I'd use the following approach:

$command = "ls -l";
$user = "user";
$host = "host";

if (! my_ssh_exec($command, $user, $host)) {
  fprintf(STDERR, "my_ssh_exec failed\n");
}


function my_ssh_exec($cmd, $host, $user) {
  $result = true;

  $desc = [
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w'],
  ];

  // -tt forces TTY allocation
  $ssh_cmd = "ssh -tt $user@$host -- $cmd";

  $proc = proc_open($cmd, $desc, $pipes);

  if (! is_resource($proc)) {
    return false;
  }

  if ($error = stream_get_contents($pipes[2])) {
    fprintf(STDERR, "Error: %s\n", $error);
    $result = false;
  }
  fclose($pipes[2]);

  if ($output = stream_get_contents($pipes[1])) {
    printf("Output: %s\n", $output);
  }
  fclose($pipes[1]);

  if ($exit_status = proc_close($proc)) {
    fprintf(STDERR, "Command exited with non-zero status %d: %s\n",
      $exit_status, $cmd);
    $result = false;
  }

  return $result;
}

The script is supposed to run in command line interface.

这篇关于为什么ssh2_exec的命令不会结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆