Bonjour,
Je dois, pour mon stage, créer une interface web permettant la gestion des switchs de la société.
Cependant, je ne vois pas comment faire pour récupérer les données des switchs.
Utiliser snmp ? Ou Telnet ? Ou socket ?
Merci.
Donc tu peux faire :Le code source de la page s'affiche comme en ligne de commande, sous forme de tableau. Seulement, à l'affichage de la page, il me met tout à la suite.
...
$tab = explode("\n",$resultat);
Tu auras ainsi un tableau des lignes de résultat.Code : Tout sélectionner
function DoCommand($c)
{
if ($this->fp)
{
fputs($this->fp,"$c\n");
$this->GetResponseUntilPrompt($r);
$r=preg_replace("/\r/", "", $r);
$r=preg_replace("/".$c."/", "", $r);
$tab = explode("\n", $r);
$this->buffer = array_slice($tab, 1, count($tab) - 2);
return 1;
}
return 0;
}
function enable($pwd)
{
fputs($this->fp, "enable\n");
fputs($this->fp, $pwd . "\n");
$this->endPrompt="#";
$this->GetResponseUntilPrompt($tmp);
}
/*
* Fonction permettant de passer en mode configuration terminal
*/
function conft()
{
fputs($this->fp, "conf t\n");
$this->endPrompt="(config)#";
$this->GetResponseUntilPrompt($tmp);
}
function exitConft()
{
fputs($this->fp, "end\n");
$this->endPrompt="#";
$this->GetResponseUntilPrompt($tmp);
}
function interfaceEthernet($numInterface)
{
fputs($this->fp, "interface fastEthernet 0/".$numInterface."\n");
$this->endPrompt="(config-if)#";
$this->GetResponseUntilPrompt($tmp);
}
function confVlanData($numInterface,$vlan)
{
$this->conft();
$this->interfaceEthernet($numInterface);
fputs($this->fp, "switchport mode access\n");
fputs($this->fp, "switchport access vlan ".$vlan."\n");
$this->exitConft();
/*
fputs($this->fp, "interface fastEthernet 0/".$numInterface ."\n");
$this->endPrompt="(config-if)#";
$this->GetResponseUntilPrompt($tmp);
fputs($this->fp, "switchport mode access\n");
fputs($this->fp, "switchport access vlan ".$vlan."\n");
fputs($this->fp, "exit\n");
$this->endPrompt="(config)#";
$this->GetResponseUntilPrompt($tmp);
fputs($this->fp, "exit\n");
$this->endPrompt="#";
$this->GetResponseUntilPrompt($tmp); */
}
function GetResponse(&$r)
{
$r='';
do {
$r.=fread($this->fp,1000);
$s=socket_get_status($this->fp);
} while ($s['unread_bytes']) ;
if ($this->dump)
print $r."\n";
}
function GetResponseUntilPrompt(&$r)
{
$r='';
do
{
$r.=fread($this->fp,10000);
$s=socket_get_status($this->fp);
if (preg_match("/ --More-- /", $r))
{
$r = preg_replace("/ --More-- /", "MORE", $r);
fputs($this->fp, " ");
}
} while (! preg_match("/".$this->endPrompt."$/", $r));
$r=preg_replace("/".chr(8)."/", "", $r);
$r=preg_replace("/MORE /", "", $r);
if ($this->dump)
print $r."\n";
}
Code : Tout sélectionner
if (isset($user)) {
$telnet = new PHPCiscoTelnet();
$result = $telnet->Connect($Ipswitch,$user, $mdp_recu);
switch ($result)
{
case 0:
$telnet->enable($mdp_recu);
$telnet->confVlanData($interface,$vlanData);
$telnet->Disconnect();
break;
case 1:
echo '[PHP Telnet] Connect failed: Unable to open network connection';
break;
case 2:
echo '[PHP Telnet] Connect failed: Unknown host';
break;
case 3:
echo '[PHP Telnet] Connect failed: Login failed';
break;
case 4:
echo '[PHP Telnet] Connect failed: Your PHP version does not support PHP Telnet';
break;
}
}