jaywax
Messages postés3Date d'inscriptionvendredi 25 juillet 2003StatutMembreDernière intervention18 janvier 2005
-
17 janv. 2005 à 12:16
jaywax
Messages postés3Date d'inscriptionvendredi 25 juillet 2003StatutMembreDernière intervention18 janvier 2005
-
18 janv. 2005 à 08:08
Bonjour à tous :)
J'utilise PHP pour me connecter à des switchs cisco (via telnet) et récupérer le résultat d'une commande que j'exploite ensuite.
Mon problème est que sur certains type de switchs (3550), le telnet marche, mais impossible de valider le mot de passe, alors que sur des 2949G, la communication telnet se passe sans problème :
PS : J'ai chopé cette classe sur internet :
<?php
error_reporting(-1);
class Telnet {
var $sock = NULL;
function telnet($host,$port) {
$this->sock = fsockopen($host,$port);
}
function close() {
if ($this->sock)
fclose($this->sock);
$this->sock = NULL;
}
function write($buffer) {
$buffer = str_replace(chr(255),chr(255).chr(255),$buffer);
fwrite($this->sock,$buffer);
}
function getc() {
return fgetc($this->sock);
}
function read_till($what) {
$buf = '';
while (1) {
$IAC = chr(255);
$DONT = chr(254);
$DO = chr(253);
$WONT = chr(252);
$WILL = chr(251);
$theNULL = chr(0);
$c = $this->getc();
if ($c === false)
return $buf;
if ($c == $theNULL) {
continue;
}
if ($c == "\021") {
continue;
}
if ($c != $IAC) {
$buf .= $c;
if ($what == (substr($buf,strlen($buf)-strlen($what)))) {
return $buf;
} else {
continue;
}
}
$c = $this->getc();
if ($c == $IAC) {
$buf .= $c; } else if (($c $DO) || ($c $DONT)) {
$opt = $this->getc();
fwrite($this->sock,$IAC.$WONT.$opt); } elseif (($c $WILL) || ($c $WONT)) {
$opt = $this->getc();
fwrite($this->sock,$IAC.$DONT.$opt);
} else {
}
}
}
}
$hostname = "mon_host";
$tn = new telnet($hostname,23);
sleep(2);
echo $tn->read_till("word: ");
sleep(2);
//Mot de passe :
$tn->write("***");
sleep(2);
//$tn->write("\r\n");
echo $tn->read_till("> ");
//Pour les 2948G
$tn->write("sh port status\r\n");
//Pour les 3550
$tn->write("sh int status\r\n");
//On met dans $variable le résultat de la commande...
$variable = $tn->read_till("> ");
echo $tn->close();
//On affiche le tout pour l'exemple :
echo $variable;
?>
jaywax
Messages postés3Date d'inscriptionvendredi 25 juillet 2003StatutMembreDernière intervention18 janvier 2005 18 janv. 2005 à 08:08
Ne vous méprennez pas :) --> un telnet simple (sans passer par PHP) marche parfaitement.
Le problème se situe seulement lorsque j'utilise la classe telnet.