Précédent   Forum des professionnels en informatique > PHP > Langage > Fichiers
Fichiers Forum d'entraide sur les fichiers avec PHP. Avant de poster -> FAQ fichiers et Sources fichiers
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 21/08/2011, 13h58   #1
Invité régulier
 
Homme Christophe Buffet
Développeur Web
Inscription : novembre 2010
Messages : 11
Détails du profil
Informations personnelles :
Nom : Homme Christophe Buffet
Localisation : Belgique

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : novembre 2010
Messages : 11
Points : 9
Points : 9
Envoyer un message via Skype™ à coccoweb
Par défaut SSH en PHP, Comment ça marche

Bonjour, voici un certain temps déjà que j'essaye, en y arrivant progressivement de faire une class SSH pour gérer un serveur dédié.

Comme je sais que vous êtes, comme moi des dévoreurs de code, voici le miens.

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
 
<?php 
class SSH {
private $conn; // Connection 
private $log=array(); // Event log
private $command=NULL;
private $stream;
private $debug=false;
public $data;
public $fingerprint;
 
public $console=array();
 
 
public function __construct($user, $pass, $host, $port=22) {
 
if ($this->connect($host,$port))
{
 
	if (@$this->auth_pwd($user,$pass)){
	return true;
	} else {
	return false;
	}
 
} else {
return false;
}
 
}
 
 
/*** Connection SSH ***/
public function connect($host,$port=22) {
 
if ($this->conn = ssh2_connect($host, $port))
{
$this->fingerprint=@ssh2_fingerprint($connection,SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);
 
$this->log[] = 'Connection succes to '.$host.':'.$port;
return true;
} else {
$this->log[] = 'Connection faild to '.$host.':'.$port;
return false;
}
 
}
 
 
/*** Login SSH ***/
public function auth_pwd($user,$pass) {
 
if (ssh2_auth_password($this->conn, $user, $pass)==true)
{
$this->log[] = 'Login succces by '.$user;
return true;
} else {
$this->log[] = 'Login faild by '.$user;
return false;
}
 
}
 
/*** Open shell ***/
public function shell($term_type=SSH2_DEFAULT_TERMINAL, $env=NULL, $width=SSH2_DEFAULT_TERM_WIDTH, $height=SSH2_DEFAULT_TERM_HEIGHT) {
 
if ($this->stream = @ssh2_shell($this->conn, $term_type, $env, $width, $height, SSH2_DEFAULT_TERM_UNIT))
{
$this->log[] = 'Open shell with PARAM terminal = ' . $term_type . ' env = ' .$env . ' width =' .$width . ' height = ' .$height;
 
 
	if ($this->stream!=false) {
 
	return true;
	}
	else
	{
	$this->log[] = 'Open shell not run. Exit';
	$this->__destruct();
	return false;
	}
 
}
else
{
return false;
}
 
}
 
 
/*** Run a command ***/
public function cmd($cmd, $toudou=false) {
 
if ($toudou == true)
{
	if (!empty($this->stream))
	{
	$this->log[] = 'Run cmd : ' . $cmd;
	//fwrite($this->stream, $cmd . PHP_EOL);
	$stream = ssh2_exec($this->conn, $cmd . PHP_EOL);
	sleep ( 1 );
 
 
	stream_set_blocking($stream, true);
 
	  // The command may not finish properly if the stream is not read to end
	$this->console[] = stream_get_contents($stream);
 
	}
	else
	{
	$this->log[] = 'Shell not run : exit';
	$this->__destruct();
	}
}
else
{
$this->log[] = 'add command to list : '. $cmd;
$this->command.=$cmd . ' ; ';
}
 
}
 
public function shell_cmd($cmd)
{
fwrite($this->stream, $cmd . PHP_EOL);
}
 
/*** Return log debuging ***/
function log($print=false) {
 
if ($print == false)
{
return $this->log;
}	
else
{
echo '<pre>';
 
	foreach ($this->log as $key => $value)
	{
	echo $key . ' ' . $value . PHP_EOL;
	}
 
echo '</pre>';
}
 
}
 
 
/*** Print terminal return ***/
public function get() {
 
 
while($line = fgets($this->stream, 8192)) { flush(); echo $line . PHP_EOL;}
 
}
 
public function console(){
$this->log[] = 'Send command list : ' .$this->command;
$this->cmd($this->command, true);
$this->disconnect();
return $this->console;
}
 
 
/*** Deconnexion ***/
public function disconnect() {
 
	// if disconnect function is available call it..
	if ( function_exists('ssh2_disconnect') ) {
	$this->log[] = 'Disconnect by funct ssh2_disconnect';
		ssh2_disconnect($this->conn);
	} else { // if no disconnect func is available, close conn, unset var
	$this->log[] = 'Disconnect by fclose';
		@fclose($this->conn);
		unset($this->conn);
	}
	if ($this->debug == true){$this->log(true);}
	// return null always 
	return NULL;
}
 
 
/*** Destruction de class ***/
public function __destruct() {
// fermeture de la connection, bool envois direct
$this->cmd('echo "Exit" ; exit', true); 
$this->disconnect(); 
}
 
}
?>
Certain variable et fonction ne sont pas utilisé, elle cause des erreurs d'affichage.


Pour info :

En lançant cette commande
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
<?php
if ($ssh = new SSH('userSSH', 'passwordSSH', '127.0.0.1'))
{
 
if ($ssh->shell()==true)
{
// Stockage de la command
$ssh->cmd('cd public_html');
$ssh->cmd('cd framework');
$ssh->cmd('ps -x', true); // Envois de la requete direct
$ssh->cmd('ls');
 
echo '<pre>';
	foreach($ssh->console() as $data)
	{
	echo $data . PHP_EOL;
	}
echo '</pre>';
 
$ssh->log(true);
}
 
}
else
{
echo 'Login faild';
}
 
?>
Vous obtenez cette réponse

PID TTY STAT TIME COMMAND
28165 ? S 0:00 sshd: hurricane@pts/0
28166 pts/0 Rs+ 0:00 -bash
28167 ? Rs 0:00 ps -x

controller_base.class.php
laucher.class.php
mvc.class.php
router.class.php
template.class.php

0 Connection succes to 127.0.0.1:22
1 Login succces by hurricane
2 Open shell with PARAM terminal = vanilla env = width =80 height = 25
3 add command to list : cd public_html
4 add command to list : cd framework
5 Run cmd : ps -x
6 add command to list : ls
7 Send command list : cd public_html ; cd framework ; ls ;
8 Run cmd : cd public_html ; cd framework ; ls ;
9 Disconnect by fclose


Le log est très utile pour moi, sachant que je ne connais que très peu le SSH

Vous aurez surement constaté que j'ouvre un Shell (qui n'est pas utilisé )
J'avais essayé de le faire fonctionner, mais ça coince, voir ligne 100 commenter

Comment compléter, arranger voir amélioré cette class, pour ouvrir un véritable shell interactif ?
coccoweb est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 11h44.


 
 
 
 
Partenaires

Hébergement Web