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
| function connect( $ip, $port, $user=NULL, $pass=NULL, $cmd, $allow_return=0){
//$allow_return 1 => sorti classique, 2 => sortie boolean
// Connect to the server
$ERR = false;
$log = new Logs();
if(!function_exists('ssh2_connect')){
showLog('The ssh2 lib is not installed');
$log->addLog('SSH', "ERROR The ssh2 lib is not installed" );
return false;
}
try{
$ssh2 = ssh2_connect($ip,$port);
if($user != NULL AND $pass!=NULL){
$auth = ssh2_auth_password($ssh2, $user, $pass);
$data = '';
if(!$auth){
$log->addLog('SSH', "ERROR Connection $ip:$port => Erreur d'identification ( Login => $user AND pass => $pass )" );
return false;
}
}
}catch(Exception $e){
$log->addLog('SSH', "ERROR Connection $ip:$port => Erreur de connexion ( Login => $user AND pass => $pass )" );
$ERR = true;
return false;
}
flush();
if (!$stream = ssh2_exec($ssh2, $cmd)) {
$log->addLog('SSH', "ERROR Connection $ip:$port => Erreur d'exécution de la commande shell $cmd" );
$ERR = true;
//echo "<br />erreur d'exécution commande shell";
}
error_reporting(E_ALL);
// Sortie de l'erreur quand il y en a une
$stderr = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($stderr, true);
$output = '';
while($ligne = fgets($stderr)) {
$output = $output . $ligne . '<br />';
}
if($output != ''){
$log->addLog('SSH', "ERROR Connection $ip:$port => commande:$cmd =>Erreurs : $output" );
$ERR = true;
}
fclose($stderr);
fclose($stream);
flush();
if($ERR == true) return false;
$log->addLog('SSH', "Connection $ip:$port => commande:$cmd $data" );
if($allow_return == 1)
return $data;
return true;
// Close the stream
//fclose($stream);
} |
Partager