Pioden Posted July 21, 2009 Share Posted July 21, 2009 Hi folks, I'm thinking of writing some code to execute commands on a different server to the one on which LAMPS will be running. For example changing user quotas etc. on a Samba server etc. Can anyone recommend a way of doing this? My first thought was to execute shell scripts on the localhost (with all the ssh stuff in the script) but I'm sure there are other, probably better, ways of doing the same thing. TIA Huw Link to comment https://forums.phpfreaks.com/topic/166784-executing-commandscli-on-a-diferent-server/ Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 Here is an SSH class I wrote a while back: <?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}'"); } } ?> Example usage: <?php header('Content-type:text/plain;'); require_once ('../../autoload.inc'); $hostname = 'www.yourserver.com'; $user = 'someuser'; $pass = 'somepass'; $file = 'test.txt'; $ssh = new ssh($hostname); print "Connected via SSH to '{$hostname}'\n"; if (!$ssh->auth($user,$pass)) die("Failed to authenticate as '{$user}'\n"); print "Authenticated as '{$user}'\n"; print "\nCommand: id\n"; if(!$ssh->cmd('id', $output, $rc)) die("Failed to run command {$cmd}"); print "Return Code: {$rc}\n"; print "======= Output =======\n$output\n======================\n"; print "\nSudo Command: id\n"; if(!$ssh->sudo_cmd('id', $output, $rc)) die("Failed to run command {$cmd}"); print "Return Code: {$rc}\n"; print "======= Output =======\n$output\n======================\n"; print "\nChanging to /tmp\n"; if(!$ssh->cmd('cd /tmp', $output, $rc)) die("Failed to change to /tmp\n"); print "\nCommand: cd /tmp\n"; print "Return Code: {$rc}\n"; print "======= Output =======\n$output\n======================\n"; print "\nSending file ".realpath($file)."\n"; if(!$ssh->put(realpath($file),'/tmp/test')) die("Failed to put file {$file}\n"); print "File sent\n"; if(!$ssh->cmd('ls -la', $output, $rc)) die("Failed to list files\n"); print "\nCommand: ls -la\n"; print "Return Code: {$rc}\n"; print "======= Output =======\n$output\n======================\n"; ?> Link to comment https://forums.phpfreaks.com/topic/166784-executing-commandscli-on-a-diferent-server/#findComment-879496 Share on other sites More sharing options...
Pioden Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks. That helped - I found a bunch of other interesting stuff too as a result of your reply. Cool!! Link to comment https://forums.phpfreaks.com/topic/166784-executing-commandscli-on-a-diferent-server/#findComment-880177 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.