par
synergie » 13 janv. 2014, 15:56
En effet, je dois développer cette application en php mais merci quand même !
Je sèche complètement sur ce problème :
Je dois communiquer avec le switch afin de pouvoir modifier des vlan à distance, via l'application web mais, ce que j'ai fait ne fait que tourner en boucle, et ne s'arrête jamais. Je ne vois pas mon/mes erreurs.
Merci d'avance !
Le code de traitement :
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";
}
Le code d'appel :
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;
}
}
En effet, je dois développer cette application en php mais merci quand même !
Je sèche complètement sur ce problème :
Je dois communiquer avec le switch afin de pouvoir modifier des vlan à distance, via l'application web mais, ce que j'ai fait ne fait que tourner en boucle, et ne s'arrête jamais. Je ne vois pas mon/mes erreurs.
Merci d'avance !
Le code de traitement :
[code]
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]
Le code d'appel :
[code]
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;
}
}
[/code]