IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Exécuter commande Cisco - PHP


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Inscrit en
    Janvier 2014
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2014
    Messages : 15
    Par défaut Exécuter commande Cisco - PHP
    Bonjour,

    Excusez-moi, je ne savais pas trop où poster mon message, je le fais donc ici. Je dois développer une interface web pour gérer des switchs. Je communique donc en telnet via php avec mon switch. J'ai réussi à procéder à l'affichage de données. Mais, lorsque je veux effectuer une commande me permettant de modifier la configuration du switch (ici, les vlans), problème : la page tourne en boucle et n'affiche rien. Je viens donc demander de l'aide, en espérant avoir été clair..


    Le code de la fonction :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
     
    <?php
     
     
    class PHPCiscoTelnet
    {
            var $fp=NULL;
            var $loginprompt;
     
            var $buffer = array();
            var $dump = 0;
            var $endPrompt = ">";
     
     
     
            /*
            0 = success
            1 = couldn't open network connection
            2 = unknown host
            3 = login failed
            4 = PHP version too low
            */
            function Connect($server,$user,$pass)
          {
                    $rv=0;
                    //$vers=explode('.',PHP_VERSION);
                    $needvers=array(4,3,0);
                    //$j=count($vers);
                    //$k=count($needvers);
                    //if ($k<$j) $j=$k;
                    //for ($i=0;$i<$j;$i++)
                //{
                     //       if (($vers[$i]+0)>$needvers[$i]) break;
                      //      if (($vers[$i]+0)<$needvers[$i]) return 4;
                    //}
     
                   // $this->Disconnect();
     
                    if (strlen($server))
                {
                            if (preg_match('/[^0-9.]/',$server))
                      {
                                    $ip=gethostbyname($server);
                                    if ($ip==$server)
                            {
                                            $ip='';
                                            $rv=2;
                                    }
                            } else $ip=$server;
                    } else $ip='127.0.0.1';
     
                    if (strlen($ip))
                {
                            if ($this->fp=fsockopen($ip,23))
                      {
                                    fputs($this->fp,chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
                                    chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
                                    chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
                                    chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
                                    chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
                                    chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
                                    chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
                                    chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
                                    chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
                                    chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
                                    chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
                                    chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0));
     
                                    fputs($this->fp,chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
                                    chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21));
                                    $this->GetResponse($r);
                                    $r=explode("\n",$r);
                                    $this->loginprompt=$r[count($r)-1];
     
                                    fputs($this->fp,"$user\r");
     
                                    fputs($this->fp,"$pass\r");
                                    $this->GetResponse($r);
                                    $r=explode("\n",$r);
                                   /* if (($r[count($r)-1]=='')||($this->loginprompt==$r[count($r)-1])) {
                                            $rv=3;
                                            $this->Disconnect();
                                    }*/
                            } else $rv=1;
                    }
     
                   $this->GetResponseUntilPrompt($tmp);
                    return $rv;
            }
     
            function Disconnect($exit=1)
          {
                    if ($this->fp) {
                            if ($exit) fputs($this->fp, "\nexit");
                            fclose($this->fp);
                            $this->fp=NULL;
                    }
            }
     
            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, "(config)#interface fastEthernet 0/".$numInterface."\n");
                    $this->endPrompt="(config-if)#"; 
                    $this->GetResponseUntilPrompt($tmp);
          }
     
          function confVlanData($numInterface,$vlan)
          {
            $this->conft();
            $this->interfaceEthernet($numInterface);
            fputs($this->fp, "(config-if)#switchport mode access\n");
             $this->GetResponseUntilPrompt($tmp);
            fputs($this->fp, "(config-if)#switchport access vlan ".$vlan."\n");
             $this->GetResponseUntilPrompt($tmp);
            $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";
          }   
     
            function display()
          {   
             $out='';
                 foreach ($this->buffer as $line)
             {
                  $out.= $line."\n\n";                
                }
             return $out;
          }
    }
    ?>







    Code d'appel :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    <?php
    require("entete.php");       
    require_once ("PHPCiscoTelnet.php");
     
    //Récupère les sessions définis dans le fichier login.php     
     $Ipswitch=$_SESSION['ipswitch'];
     $user=$_SESSION['login'];
     $mdp_recu= $_SESSION['mdp'];  
     
     $interface = $_POST['nomInterface'];
     $description = $_POST['txtDesc'];
     $vlanData = $_POST['txtVlanData'];
     $vlanVoice = $_POST['txtVlanVoice'];
     
     
    //Si session active, se connecter au switch, sinon, retour au formulaire     
    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;
          }
      }




    Merci d'avance !

  2. #2
    Membre confirmé Avatar de alejandro
    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2004
    Messages
    167
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2004
    Messages : 167
    Par défaut
    Bonsoir,

    Pas évident comme ça mais je dirais en consultant la doc de fsockopen que tu devrais mettre un timeout lors de ton appel de fonction :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    if ($this->fp = fsockopen($ip, 23, $errno, $errstr, 30))
    {
    ...
    Va voir les exemples d'utilisation sur la doc officielle :

    http://www.php.net/manual/fr/function.fsockopen.php

Discussions similaires

  1. exécuter un url php en ligne de commande
    Par MedNaceur dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 4
    Dernier message: 11/12/2009, 22h32
  2. Exécuter un script PHP en ligne de commande
    Par sami_c dans le forum Unix
    Réponses: 4
    Dernier message: 09/05/2009, 15h28
  3. [Système] Exécution commande dans une page PHP
    Par corto31 dans le forum Langage
    Réponses: 7
    Dernier message: 25/11/2008, 15h06
  4. Ligne de commande php pour exécuter un script php basé sur mon serveur web
    Par bdgtat dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 5
    Dernier message: 13/07/2007, 08h53
  5. [MySQL] exécuter un script php en commande linux : problème sur les fonctions mysql
    Par dr_octopus74 dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 16/03/2007, 16h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo