azaonline Posted August 17, 2009 Share Posted August 17, 2009 Hi All, i am trying to get a shell script ro run on a remote server (opensuse 11.1) to copy files from one directory to antother using PHPs ssh2 exec function. soo far i have been able to get the script to run, but after the sleep timer has expired the session finishes and terminates the shell script. is there any way to keep the script going untill it has finished. the script works fine if i execite it from the command prompt on both the web server and the remote server. Any ideas?? Thanks in advanced. Aaron $connection = ssh2_connect($host, $port); if (ssh2_auth_password($connection, $usr, $psw)) { echo "Authentication Successful!\n"; echo "<br>"; } else { die('Authentication Failed...'); } echo "<br>"; $command1 = "/srv/www/htdocs/exec/rebuild.sh\n"; echo "<br><br><br>"; $shell = ssh2_shell($connection,"xterm"); fwrite( $shell, $command1); echo "server Started..."; sleep(4); // stream while($line = fgets($shell)) { flush(); echo $line."<br />"; } Link to comment https://forums.phpfreaks.com/topic/170694-running-a-shell-script-with-php_ssh2/ Share on other sites More sharing options...
rhodesa Posted August 17, 2009 Share Posted August 17, 2009 Here is a class I wrote to help with SSH commands: <?php /************************************************************************************************** * SSH Class * * This class was writen to be used in conjuction with the SSH2 PECL extension. It does a pretty * good job handling bi-directional pipeing in the cmd() function. If you find ways to improve * this class, please notify me so I can spread the word. * * NOTE: PasswordAuthentication must be set to yes in the sshd_config file of the server the * script is connecting to. *************************************************************************************************/ class ssh { protected static $instances = array(); /*************************************************************************** * CONSTANTS * * DEFAULT_TIMEOUT: Default Timeout for commands (20 seconds) **************************************************************************/ const DEFAULT_TIMEOUT = 20; /*************************************************************************** * MEMBERS **************************************************************************/ protected $host; protected $port; protected $user; protected $pass; protected $cnx; protected $stream; protected $timeout = self :: DEFAULT_TIMEOUT; /*************************************************************************** * METHODS **************************************************************************/ public static function getInstance($id = null) { if (empty ($id)) $id = 0; if (isset (self :: $instances[$id])) return self :: $instances[$id]; return false; } public function registerInstance($id = null) { if (empty ($id)) { if (isset (self :: $instances[0])) throw new Exception("Default instance already exists"); self :: $instances[0] = $this; return true; } if (isset (self :: $instances[$id])) throw new Exception("Instance '{$id}' already exists"); self :: $instances[$id] = $this; return true; } #-------------------------------------------------------------------------------------------------- # __construct(string $host,[string $port]) # # HOST - Hostname used for the ssh connection. IP address is also acceptable. # PORT - (Optional) Port to use for the ssh connection. If omitted, the default port (22) will # be used # # Class constructor. Checks the SSH2 extension is loaded then executes the ssh2_connect # command to establish a connection with the specified host at the specified port. #-------------------------------------------------------------------------------------------------- public function __construct($host, $port = '22') { //Check to make sure the SSH2 if (!extension_loaded('ssh2')) throw new Exception("SSH2 Extension must be installed"); //Save host/port $this->host = $host; if (strlen($port)) $this->port = $port; //Establish SSH connection if (!($this->cnx = ssh2_connect($this->host, $this->port))) throw new Exception("Connection failed to host '{$this->host}:{$this->port}'"); } #-------------------------------------------------------------------------------------------------- # boolean getFingerprint() # # Returns the fingerprint of the remote server. #-------------------------------------------------------------------------------------------------- public function getFingerprint() { return @ ssh2_fingerprint($this->cnx); } #-------------------------------------------------------------------------------------------------- # boolean auth(string $user,[string $password]) # # USER - Username used to authenticate the current connection. # PASSWORD - (Optional) Password used to authenticate user at the current connection. If # omitted, no password is used durring authentication. # # Tries to authenticate with the specified user and optional password. If successful, it # opens a shell and stores the stream into a variable. Returns true if sucessful, false if # there is an error. #-------------------------------------------------------------------------------------------------- public function auth($user, $password = null) { if (!@ ssh2_auth_password($this->cnx, $user, $password)) throw new Exception("Authorization for '$user' failed at host '{$this->host}:{$this->port}'"); $this->user = $user; $this->pass = $password; return true; } #-------------------------------------------------------------------------------------------------- # boolean setTimeout([int seconds]) # # SECONDS - (Optional) Number of seconds until a cmd() command times out. # # Sets the timeout variable used in the cmd() function. If seconds is specified, it confirms # the value is an integer then sets the timeout. If ommited, it sets timeout to the default # value. Returns true if successful, otherwise returns false. #-------------------------------------------------------------------------------------------------- public function setTimeout($seconds = self :: DEFAULT_TIMEOUT) { if (is_numeric($seconds) && $seconds > 0) return $this->timeout = $seconds; return false; } #-------------------------------------------------------------------------------------------------- # boolean cmd(string $cmd,[ref $output],[ref $error],[string $user],[string $pass]) # # CMD - Command to run on the remote system. # OUTPUT - (Optional) Reference to a variable in which any output from the remote terminal will # be stored. # RC - (Optional) Reference to a variable in which the command's exit code will be stored. # USER - (Optional) User to run the command as. # PASS - (Optional) Password for the user above. If user is null but a password is specified, # sudo is assumed. # # Runs a command on the remote system and gathers any output from the command. Returns true # if sucessful, false if there is an error. #-------------------------------------------------------------------------------------------------- public function cmd($cmd, & $output = null, & $rc = null, $user = null, $pass = null) { //Confirm we have a stream open if (!$this->stream) { if (!($this->stream = @ ssh2_shell($this->cnx))) throw new Exception("Could not open a shell for '$user' at host '{$this->host}:{$this->port}'"); } //Generate a random string to use as a key we can parse for. $prefix = md5(microtime()); $suffix = md5(microtime()); $fail = md5(microtime()); //Set some variables $output = null; $rc = null; $start = time(); //Generate the command // It wraps the command with echo statements in order to determine the begining // and end of the output from running the command. $command = $cmd; if (strlen($user) && strlen($pass)) //Run as other user $command = sprintf('su %s -c %s', escapeshellarg($user), escapeshellarg($command)); elseif (strlen($pass)) //Sudo $command = sprintf("sudo %s", escapeshellarg($command)); $command = sprintf("echo %s && %s && echo %s || echo %s\n", $prefix, $command, $suffix . ':$?', $fail . ':$?'); fwrite($this->stream, $command); //Start the inifinite loop while (1) { //Get some output from shell $output .= stream_get_contents($this->stream); //Flush the output // Found the prefix key. Strip everything up to and including the prefix key from output // The \r\n is to make sure we get the new line feed after the echo if (preg_match(sprintf('/%s\r?\n(.*)/s', $prefix), $output, $matches)) $output = $matches[1]; //Finished // Found the suffix key so the command is done. Strip the suffix key and everything after from output if (preg_match(sprintf('/(.*)%s:(\d*)\r?\n/s', $suffix), $output, $matches)) { $output = $matches[1]; $rc = $matches[2]; return true; } //Failed // Found the failed suffix key so the command errored out for some reason. // Strip the failed suffix key and everything after from output and return false. if (preg_match(sprintf('/(.*)%s:(\d*)\r?\n/s', $fail), $output, $matches)) { $output = $matches[1]; $rc = $matches[2]; return false; } //Check for password prompt if (strlen($pass) && preg_match('/password:\s*$/i', $output)) { $output = null; fwrite($this->stream, "{$pass}\n"); $pass = null; } //Check for timeout if (time() > $start + $this->timeout) throw new Exception("Command '{$cmd}' timed out"); //Sleep for a micro second to save the processor usleep(1); } //If we get here something weird happened. return false; } #-------------------------------------------------------------------------------------------------- # boolean su_cmd(string $cmd,string $user,string $pass,[ref $output],[ref $error]) # # CMD - Command to run on the remote system. # USER - User to run the command as. # PASS - Password for the user above. # OUTPUT - (Optional) Reference to a variable in which any output from the remote terminal will # be stored. # RC - (Optional) Reference to a variable in which the command's exit code will be stored. # # Runs the cmd() function. #-------------------------------------------------------------------------------------------------- public function su_cmd($cmd, $user, $pass, & $output = null, & $rc = null) { return $this->cmd($cmd, $output, $rc, $user, $pass); } #-------------------------------------------------------------------------------------------------- # boolean sudo_cmd(string $cmd,string $pass,[ref $output],[ref $error]) # # CMD - Command to run on the remote system. # OUTPUT - (Optional) Reference to a variable in which any output from the remote terminal will # be stored. # RC - (Optional) Reference to a variable in which the command's exit code will be stored. # # Runs the cmd() function. #-------------------------------------------------------------------------------------------------- public function sudo_cmd($cmd, & $output = null, & $rc = null) { return $this->cmd($cmd, $output, $rc, null, $this->pass); } #-------------------------------------------------------------------------------------------------- # boolean put(string $local,string $remote,[int $mode]) # # LOCAL - Local path to the file that will be sent. # REMOTE - Remote path where the file being sent will be stored. # MODE - (Optional) Mode to create the file with. If ommited a default of 700 is used. # # Checks to make sure the local file exists then sends it via SCP. Returns true if # sucessful, false if there is an error. #-------------------------------------------------------------------------------------------------- public function put($local, $remote, $mode = 0700) { if (!is_file($local)) throw new Exception("Tried sending '{$local}' but it does not exist"); if (!$remote) $remote = basename($local); if (@ ssh2_scp_send($this->cnx, $local, $remote, $mode)) return true; throw new Exception("Failed to send '{$local}'"); } #-------------------------------------------------------------------------------------------------- # boolean get(string $remote,string $local) # # REMOTE - Remote path to the file being received. # LOCAL - Local path where the file being received will be stored. # # Receives a file via SCP. Returns true if sucessful, false if there is an error. #-------------------------------------------------------------------------------------------------- public function get($remote, $local) { if (@ ssh2_scp_recv($this->cnx, $remote, $local)) return true; throw new Exception("Failed to get '{$remote}'"); } } ?> Usage for your case would be something like: <?php require_once('ssh.class.inc'); $ssh = new ssh($hostname); if (!$ssh->auth($user,$pass)) die("Failed to authenticate to '{$hostname}' as '{$user}'\n"); $cmd1 = "/srv/www/htdocs/exec/rebuild.sh"; $ssh->setTimeout(60); //Timeout after 60 seconds...increase if needed if(!$ssh->cmd($cmd, $output, $rc)) die("Failed to run command {$cmd}"); print "Return Code: {$rc}\n"; print "======= Output =======\n$output\n======================\n"; ?> Link to comment https://forums.phpfreaks.com/topic/170694-running-a-shell-script-with-php_ssh2/#findComment-900280 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.