0

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; 
      } 
    } 
  } 
} 

?>
SarahG
  • 1
  • 3
  • Can you provide the command you are using? We nee more information to be able to help. – mtrueblood May 06 '16 at 12:20
  • I have edit the post, :) – SarahG May 06 '16 at 12:24
  • I think that your commands are probably running asynchronously and that is why you aren't getting the result you are looking for. – mtrueblood May 06 '16 at 13:01
  • Take a look at this in the php manual to see if it will fix your problem: http://php.net/manual/en/function.ssh2-exec.php#59324 – mtrueblood May 06 '16 at 13:10
  • Thanks mtrueblood, other question with this. How get result of commands? I would like to see result. Per example if server return an error or true result. – SarahG May 06 '16 at 13:47
  • if you are looking to display the result in your script add in an env tweak of `'php -v'`, use `$output = sh2_fetch_stream($stream, SSH2_STREAM_STDIO)` to define an output stream variable, and then echo the output stream after the commands execute. – mtrueblood May 06 '16 at 14:00
  • I get blank page... $output = user_exec($shell,$cmd); $output = ssh2_fetch_stream($output, SSH2_STREAM_STDIO); echo stream_get_contents($output); – SarahG May 06 '16 at 14:08
  • try this: `$stream = ssh2_exec([your connection, env, and commands here]);` `stream_set_blocking($stream, true);` `$outputStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);` `echo stream_get_contents($outputStream);` – mtrueblood May 06 '16 at 14:30
  • I use script by function on the page that you give me... He don't use "ssh2_exec" ! Please looking the script for help me :/ – SarahG May 06 '16 at 14:40
  • Sorry, I forgot that switched, just use the`ssh2_exec` instead. Also look at this reference for using ssh2_exec with multiple commands and env vars: http://php.net/manual/en/function.ssh2-shell.php#76276 – mtrueblood May 06 '16 at 15:00
  • The first script (http://php.net/manual/en/function.ssh2-exec.php#59324) is very good, I must need to get result of commands.. You know how to this? – SarahG May 06 '16 at 15:10
  • You should only have to add the following into your code `stream_set_blocking($stream, true);` To allow blocking `$outputStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);` Using your original stream variable to grab the feedback. `echo stream_get_contents($outputStream);` To write out the feedback – mtrueblood May 06 '16 at 15:23
  • Okay but I don't understand because variable $stream doesn't exist in my script... Where I put your lines ? – SarahG May 06 '16 at 15:31
  • Okay, I get error 500 – SarahG May 06 '16 at 15:41
  • How about creating a flag file if your unzip occurs, then checking for the file to verify that it did unzip? I have to admit I have never tried to capture command output before so you might also want to post this aspect as a separate question about how to capture the output – mtrueblood May 06 '16 at 16:33
  • Other proposal please? :/ – SarahG May 06 '16 at 16:41

1 Answers1

0

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.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60