ArabSmall 118 Zgłoś post Napisano Luty 22, 2012 (edytowany) Witam, chciałbym sobie zrobić taki mały skrypt PHP który wykorzystuje SSH2. A dokładnie to skrypt tworzy katalog wchodzi do katalog ściąga plik .zip rozpakowuje plik .zip no i skrypt robi wszystko do ściągnięcia pliku .zip ale go nie rozpakowuje. Tak wygląda plik .php <?php include('config.php'); session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl"> <head> <title>podstrona</title> </head> <body background="images/bg.jpg" text="white"> <?php if($_SESSION["zalogowany"]==0){echo "<center>Nie masz dostępu do tej części witryny. <a href='index.php'>Zaloguj się</a></body></html>"; exit();} if($_SESSION["zalogowany"]==1){echo "<center>Test skryptu</body></html>"; $polecenie = 'cd /home && mkdir katalog && wget http://pliki.arabsmall.pl/steam.zip && mv steam.zip katalog && cd katalog '; $polecenie2 = 'cd /home/katalog && unzip steam.zip'; $polaczenie = ssh2_connect($host, $port); if( !@ssh2_auth_password($polaczenie, $user, $haslo) ) { echo 'Nie mozna polaczyc sie z serwerem!'; exit;} $stream = ssh2_exec($polaczenie, $polecenie); $stream = ssh2_exec($polaczenie, $polecenie2); if($stream) {echo "<br />Operacja została wykonana!";} echo '<br /><br /><FORM ACTION="index.php" METHOD="post"> <INPUT TYPE="submit" VALUE="Wróć do panelu" NAME="button"></FORM>'; ////To ma być na końcu! exit();} ?> I tak jak pisałem wcześniej skrypt robi to co ma tworzy katalog przechodzi do katalogu ściąga plik przenosi plik do katalogu wchodzi do katalogu lecz go nie rozpakowuje. Myślałem że może nie wchodzi do katalogu i zostaje w /home lecz gdy dodałem jeszcze przez unzip mkdir test to tworzy katalog test w /home/folder/ Z góry dziękuje za pomoc. Edytowano Luty 22, 2012 przez ArabSmall (zobacz historię edycji) Udostępnij ten post Link to postu Udostępnij na innych stronach
Misiek08 285 Zgłoś post Napisano Luty 22, 2012 Jak wejdziesz na tych danych przez putty to jesteś w stanie wykonać wszystkie te operacje? Udostępnij ten post Link to postu Udostępnij na innych stronach
ArabSmall 118 Zgłoś post Napisano Luty 22, 2012 Jak wejdziesz na tych danych przez putty to jesteś w stanie wykonać wszystkie te operacje? Tak, wszystko wtedy działa (ściąga, rozpakowuje) Udostępnij ten post Link to postu Udostępnij na innych stronach
Misiek08 285 Zgłoś post Napisano Luty 22, 2012 Zrób to w 1 linii, bo z tego co pamietam to po 1 wywołaniu ssh2_exec trzeba odczytywać wynik, żeby 100% działało następne. Udostępnij ten post Link to postu Udostępnij na innych stronach
Gość Kamikadze Zgłoś post Napisano Luty 22, 2012 Spróbuj tak: if($_SESSION["zalogowany"]==0){echo "<center>Nie masz dostępu do tej części witryny. <a href='index.php'>Zaloguj się</a></body></html>"; exit();} if($_SESSION["zalogowany"]==1){echo "<center>Test skryptu</body></html>"; $polecenie = 'cd /home && mkdir katalog && cd katalog && wget "http://pliki.arabsmall.pl/steam.zip" && unzip steam.zip'; $polaczenie = ssh2_connect($host, $port); if( !@ssh2_auth_password($polaczenie, $user, $haslo) ) { echo 'Nie mozna polaczyc sie z serwerem!'; exit;} $stream = ssh2_exec($polaczenie, $polecenie); if($stream) {echo "<br />Operacja została wykonana!";} echo '<br /><br /><FORM ACTION="index.php" METHOD="post"> <INPUT TYPE="submit" VALUE="Wróć do panelu" NAME="button"></FORM>'; ////To ma być na końcu! exit();} Udostępnij ten post Link to postu Udostępnij na innych stronach
ArabSmall 118 Zgłoś post Napisano Luty 22, 2012 Dziękuje za pomoc lecz nadal nie działa unzip nawet spróbowałem poprzez plik .sh, stworzyłem plik unzip.sh który rozpakowuje steam.zip gdy go w konsoli odpalę plik się rozpakowuje, a gdy poprzez plik .php już nie. Potem sprawdziłem czy mogę uruchomić plik .sh przez .php więc utworzyłem skrypt który tworzy folder testowy i go utworzył. Czyli jak na razie wszystko działa oprócz komendy unzip nawet gdy ona jest umieszczona w pliku .sh Udostępnij ten post Link to postu Udostępnij na innych stronach
xorg 693 Zgłoś post Napisano Luty 22, 2012 (edytowany) Może podaj pełną ścieżkę, tj /usr/bin/unzip ? Poza tym polecam Ci użyć jakiejś sensownej klasy do obsługi SSH2, np. <?php /************************************************************************************************** * SSH Class * * @author Aaron Rhodes (aaron@vectorloft.com) * @version 1.0 * * 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 neException("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}'"); } } ?> Edytowano Luty 22, 2012 przez xorg (zobacz historię edycji) Udostępnij ten post Link to postu Udostępnij na innych stronach
ArabSmall 118 Zgłoś post Napisano Luty 22, 2012 Może podaj pełną ścieżkę, tj /usr/bin/unzip ? Poza tym polecam Ci użyć jakiejś sensownej klasy do obsługi SSH2, np. <?php /************************************************************************************************** * SSH Class * * @author Aaron Rhodes (aaron@vectorloft.com) * @version 1.0 * * 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 neException("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}'"); } } ?> To tak po dodaniu tego /usr/bin/unzip nadal nie rozpakowuje. A co do tej funkcji SSH2 to jest ona dla mnie nie jasna, a to dlatego że na PHP się nie znam, a jedynie znalazłem na WHT jak wykonać polecenia przez SSH2 i dlatego ten skrypt wygląda tak, a nie inaczej. Udostępnij ten post Link to postu Udostępnij na innych stronach
ArabSmall 118 Zgłoś post Napisano Luty 23, 2012 (edytowany) Wiem że nie można pisać posta pod postem, ale czy ktoś ma/miał podobny problem? Ponieważ przez .php nie mogę żadnego pliku rozpakować .tar.gz/.rar/.zip. Nawet gdy całość jest wpisana w skrypt .sh, a przez .php odpalam skrypt .sh to i tak nie rozpakowuje pliku. Edit: Upsss... Przepraszam bardzo pliki .tar.gz rozpakowuje normalnie tak wiec zostaje mi pakowanie plików w format .tar.gz ponieważ .rar/.zip nie rozpakowuje. Edytowano Luty 23, 2012 przez ArabSmall (zobacz historię edycji) Udostępnij ten post Link to postu Udostępnij na innych stronach