note 59324 added to function.ssh2-exec

From: Date: Fri, 02 Dec 2005 23:21:53 +0000
Subject: note 59324 added to function.ssh2-exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-99503@lists.php.net to get a copy of this message
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; } } } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.ssh2-exec.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59324 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59324&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59324&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59324&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59324&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59324&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59324&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59324&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59324&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99503) next »