Bonjour à tous,
Petit soucis avec SSH2 en PHP
J'ai besoin de fermer ou d'ouvrir une application sur le serveur.
J'ai déjà dégoté une class (ci-joint) qui se charge de la connexion SSH et des commandes.
J'arrive à lister les processus, fermer le processus s'il est ouvert grâce au PID et à la commande
Par contre au moment d'ouvrir un nouveau processus
Et bien le processus ne s'ouvrir pas.
Voici mon script et la class qui va avec...
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
<?php
/* ************************************************************ */
/*
* Copyright (C) 2008 Volkan KIRIK
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* ************************************************************ */
/*
* SSH - SFTP (class.shell2.php)
* v 1.0 2007-08-13
* v 1.1 2008-05-19 -current-
*
* Author : Volkan KIRIK
*
* Changes in v1.1:
*
* * Added Disconnect function
* * Some function names changed:
* auth to auth_pwd
* send to send_file
* get to get_file
* output to get_output
*
* Changes in v1.0:
*
* * First release
*
*/
/* ************************************************************ */
class shell2 {
var $conn;
var $error;
var $stream;
function __destruct(){
$this->disconnect();
}
function login($user, $pass, $host, $port=22) {
if ($this->connect($host,$port)) {
if ($this->auth_pwd($user,$pass)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
function connect($host,$port=22) {
if ($this->conn = ssh2_connect($host, $port)) {
return true;
} else {
$this->error = '[x] Can not connected to '.$host.':'.$port;
return false;
}
}
function auth_pwd($u,$p) {
if (ssh2_auth_password($this->conn, $u, $p)) {
return true;
} else {
$this->error = 'Login Failed';
return false;
}
}
function send_file($localFile,$remoteFile,$permision) {
if (@ssh2_scp_send($this->conn, $localFile, $remoteFile, $permision)) {
return true;
} else {
$this->error = 'Can not transfer file';
return false;
}
}
function get_file($remoteFile,$localFile) {
if (ssh2_scp_recv($this->conn, $remoteFile, $localFile)) {
return true;
} else {
return false;
}
}
function exec_cmd($cmd) {
$this->stream=ssh2_exec($this->conn, $cmd);
stream_set_blocking( $this->stream, true );
}
function get_output() {
$line = '';
while ($get=fgets($this->stream)) {
$line.=$get;
}
return $line;
}
function disconnect() {
// if disconnect function is available call it..
if ( function_exists('ssh2_disconnect') ) {
ssh2_disconnect($this->conn);
} else { // if no disconnect func is available, close conn, unset var
@fclose($this->conn);
unset($this->conn);
}
// return null always
return NULL;
}
}
?> |
Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
<pre>
<?php
$host = "127.0.0.1";
$user = "user";
$pass = "password";
$localFile="/home/test1.txt";
$remoteFile="/home/test2.txt";
$ssh = new shell2; // init class
// connect server
if ( $ssh->login($user,$pass,$host) ) {
echo '<h1>Connection </h1>';
//SSH Command
$ssh->exec_cmd("ls & cd webradio");
echo $ssh->get_output();
$ssh->exec_cmd("cd webradio");
echo $ssh->get_output();
$ssh->exec_cmd("ps -x");
// Enregistrement du processus
$process = $ssh->get_output();
// Suppréssion des espaces
$process=preg_replace('/\s\s+/', ' ', $process);
echo $process;
if (preg_match('#(.*)?(./sc_serv)#Ui', $process, $m)) {
preg_match('#(.*) #Ui', $m[0], $c);
echo "J'ai trouvé ce PID " . $c[1] . "<br />";
$ssh->exec_cmd("kill -9 ".$c[1]);
echo "J'ai tué ce PID " . $c[1] . "<br />";
$ssh->exec_cmd("ps -x");
echo $ssh->get_output();
} else {
echo "Ah !!! Je n'ai pas trouvé.<br /><br />";
echo "Je démarre ./sc_serv<br />";
$ssh->exec_cmd("./sc_serv");
echo $ssh->get_output();
$ssh->exec_cmd("ps -x");
echo $ssh->get_output();
}
} else {
echo '<h1>Erreur</h1>' . $ssh->error;
}
?>
</pre> |
Je tiens à préciser, que personnellement je ne connais pas encore SSH, je fais juste ce script pour un ami.
Toute aide, explication est la bienvenue.
Un grand merci d'avance