The "pty" parameter is not documented.
You should pass a pty emulation name ("vt102", "ansi", etc...) if you want to emulate a pty, or NULL if you don't.
Passing false will convert false to a string, and will allocate a "" terminal.
ssh2_exec
(PECL ssh2 >= 0.9.0)
ssh2_exec — Exécute une commande sur un serveur distant
Description
resource ssh2_exec
( resource $session
, string $command
[, string $pty
[, array $env
[, int $width= 80
[, int $height= 25
[, int $width_height_type= SSH2_TERM_UNIT_CHARS
]]]]] )
Exécute une commande sur un serveur distant.
Liste de paramètres
- session
-
Un identifiant de connexion SSH, obtenu depuis la fonction ssh2_connect().
- command
-
- pty
-
- env
-
env peut être passé sous la forme d'un tableau associatif de paires nom/valeur, à définir dans l'environnement cible.
- width
-
Largeur du terminal virtuel.
- height
-
Hauteur du terminal virtuel.
- width_height_type
-
width_height_type peut valoir SSH2_TERM_UNIT_CHARS ou SSH2_TERM_UNIT_PIXELS.
Valeurs de retour
Retourne un flux en cas de succès, ou FALSE si une erreur survient.
Exemples
Exemple #1 Exécution d'une commande
<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, '/usr/local/bin/php -i');
?>
Voir aussi
- ssh2_connect() - Connexion à un serveur SSH
- ssh2_shell() - Demande un shell interactif
- ssh2_tunnel() - Ouvre un tunnel à travers un serveur distant
ssh2_exec
magicaltux at php dot net
21-Dec-2008 12:41
21-Dec-2008 12:41
jaimie at seoegghead dot com
19-Dec-2008 04:07
19-Dec-2008 04:07
I believe most of the problem that people are having here is that there is a misconception about what blocking *really* means.
Blocking means a read from the *stream* will wait until there is data. Not necessarily all the data from the application -- but *some*. So it won't help you at all if you're executing a command that doesn't write to stdout, or writes a whole lot of data.
So there are 2 problems:
1. If you need to know that a silent program is done via ssh2_exec, you'll need to signal it to yourself. ssh2_exec will *not* block execution until the command is done executing. And 2 consecutive ssh2_execs may execute asynchronously. You could also log into a shell via ssh2_shell and parse up to the next prompt -- but that's overkill. You can also do this by adding on some sort of sentinel at the end of your command, such as echo "@," and then block on reads until you see a "@." Ensure "@" won't appear in the output, or escape the output via some encoding mechanism if you can't do that.
2. If the program takes awhile, you have the same problem. You need to read until you're done. So you need a sentinel value like the above.
3. Sleep() is just a bad idea here. Commands rarely take the same amount of time to execute a command twice. It may be OK if you're doing *one* thing and can just wait 5 seconds. But that's not cool if it's something you're doing in a loop.
I wrote a wrapper here --
http://www.seoegghead.com/software/ssh2-php-wrappers.seo
Use it -- or get ideas from it. I hope this saves someone a bad day.
Hope this helps!
lko at netuse dot de
06-Dec-2007 01:41
06-Dec-2007 01:41
if you are using exec function, and have problems with a output > 1000 lines
you should use
stream_set_blocking($stream, true);
while($line = fgets($stream)) {
flush();
echo $line."<br />";
}
except
stream_set_blocking($stream, true);
echo stream_get_contents($stream);
Betsy Gamrat
16-Apr-2007 02:52
16-Apr-2007 02:52
It is also good to use register_shutdown_function to shred the keys after this runs.
Jon-Eirik Pettersen
11-Mar-2007 12:33
11-Mar-2007 12:33
The command may not finish before the function return if the stream is not set to blocking mode. You may have to set the stream to blocking mode in order to get any output from the command.
<?php
// [Open connection]
// ...
$stream = ssh2_exec($connection, 'ps ax');
stream_set_blocking($stream, true);
// The command may not finish properly if the stream is not read to end
$output = stream_get_contents($stream);
// ...
// [Close stream and connection]
?>
Betsy Gamrat
18-Feb-2007 08:00
18-Feb-2007 08:00
If the ssh2_exec takes awhile to run, and you need a handshake, you can use a file. In this case, $flag names a handshake file that is written when the ssh script finishes.
$stream = ssh2_exec($connection, $command . " $public_key $private_key;");
$i=0;
while (!file_exists ($flag) && ($i < MAX_TIME))
{
sleep(1);
$i++;
}
$ret_val=($stream!=FALSE);
This is an extract out of the bash script that is running. Be sure to allow the webserver permission to read the file that the script writes.
echo 0 > $OUTFILE
chmod 666 $OUTFILE
One other note: you can put more than one command in by separating them with ';' (semicolons).
col at pobox dot com
15-Oct-2006 10:55
15-Oct-2006 10:55
as of 0.9 and above, if not allocating a pty, add FALSE to the call...
old way:
ssh2_exec($connection, $command);
new way:
ssh2_exec($connection, $command, FALSE);
tabber dot watts at gmail dot com
02-Dec-2005 11:21
02-Dec-2005 11:21
This is the best way I found to automatically run multiple commands or commands that might take longer then expected. NOTE: this assumes that no where in the output is there the text '[end]' otherwise the function will end prematurely. Hope this helps people.
<?php
$ip = 'ip_address';
$user = 'username';
$pass = 'password';
$connection = ssh2_connection($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;
}
}
}
}
?>
gakksimian at yahoo dot com
07-Sep-2005 05:15
07-Sep-2005 05:15
Executing remote Windows commands...
After some hair pulling, I thought I'd suggest a couple things that might help others:
1. Use 'ssh2_fetch_stream' to open a stderr stream according to the manual.
2. Windows shell commands require 'cmd /C [command]' to execute. (I had forgotten this)
<?php
// code fragment
$stdout_stream = ssh2_exec($connection, 'cmd /C dir');
$stderr_stream = ssh2_fetch_stream($stdout_stream, SSH2_STREAM_STDERR);
?>
I didn't realize the need for the 'cmd /C' until I saw the stderr response 'dir: not found'.
yairl at savion dot huji dot ac dot il
09-Jun-2005 12:03
09-Jun-2005 12:03
If you want to run many exec orders throught ssh2 in the same process using the same variable $stream for exemple and to read the output for each order, you must close the stream after each order else you will not be able to read the next order output.
$stream=ssh2_exec($conn_id,"/usr/bin/ls .");
stream_set_blocking( $stream, true );
$cmd=fread($stream,4096);
fclose($stream);
if(ereg("public_html",$cmd)){
...........................................
}
$stream=ssh2_exec($conn_id,"/usr/bin/ls public_html");
stream_set_blocking( $stream, true );
$cmd=fread($stream,4096);
fclose($stream);
if(ereg("images",$cmd)){
..........................................
}
$stream=ssh2_exec($conn_id,"/usr/bin/ls public_html/images");
stream_set_blocking( $stream, true );
$cmd=fread($stream,4096);
fclose($stream);
if(ereg("blabla",$cmd)){
..........................................
}
