title: Telnet to cisco switch using php
body: <p>I need to telnet to cisco switch using php and execute <code>show interface status</code> command and get results. I tried some php classes I found on internet but none of them could connect to device. So I tried to write the script myself, but I have the same problem, I cant connect to device.</p>
  
  <p>The host sends me banner message and then new line with <code>username:</code>.
  I send my username with <code>\r\n</code>, wait some time and tries to read data, but it looks to me like host is just ignoring my new line characters. This is response I got (explode('\n') on response):</p>
  
  <pre><code>Array
  (
      [0] =&gt; % 
      [1] =&gt; User Access Verification
      [2] =&gt; Username:  timeout expired!
  )
  </code></pre>
  
  <p>Why didn't I get prompt on password? I tried it with sending telnet headers, and without, no change. Can anyone please help me?</p>
  
  <p>Here is my code</p>
  
  <pre><code>&lt;?
  $host = "switchName";
  $name = "name";
  $pass = "pass";
  $port = 23;
  $timeOut = 15;
  $connected = false;
  $skipNullLines = true;
  $timeout = 125000;
  
  
  $header1=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(0x41).chr(0x4E).chr(0x53).chr(0x49).chr(0xFF).chr(0xF0);
  $header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
  
  
  function read_string()
  {
      global $fw,$host,$skipNullLines; 
      $string = "";
  
      while( !feof($fw) )
      {
          $read = fgets($fw);
  
          $string .= $read;
  
          // Probably prompt, stop reading
          if( strpos($read, ':') !== FALSE || strpos($read, '&gt; (enable)') !== FALSE || strpos($read, $host.'#') !== FALSE)
          { break; }
      }
  
      $string =  explode("\n", $string);
  
      // Get rid of null lines
      $ret = array();
      for($i = 0; $i&lt;count($string); $i++)
      {
          if( trim($string[$i]) == '' &amp;&amp; $skipNullLines ) continue;
  
          $ret[] = $string[$i];
      }
  
      return $ret;
  }
  
  function send_string($string, $force=false)
  {
      GLOBAL $timeout,$fw;
      $string = trim($string);
  
      // execute only strings that are preceded by "show" (if not forced)
      if(!$force &amp;&amp; strpos($string, 'show ') !== 0) 
      {
          return 1;
      }
  
      fputs($fw, $string."\r\n");
      echo("SEND:".$string."\r\n");
      usleep($timeout);
  }
  
  
  $fw = fsockopen($host, $port, $errno, $errorstr, $timeOut);    
  if($fw == false)
  {
      echo("Cant connect");
  }
  else
  {
      echo("Connected&lt;br&gt;");
      $connected = true;
  
      stream_set_timeout($fw, $timeout);
  
  //  fputs($fw, $header1);
  //  usleep($timeout);
  //  fputs($fw, $header2);
  //  usleep($timeout);
      print_r(read_string());
      send_string("test", true);
      print_r(read_string());
  }
  
  fclose($fw);
  ?&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong>
  If I send username at first, and then I read, I get password prompt. I dont understand it, why cant I firstly read messages from host and then send my response. The way it works to me now (send response and then read for prompt) is no-sense! (and I still got "% Authentication failed." message event with right password/name).</p>
  
  <pre><code>...
  $connected = true;
  stream_set_timeout($fw, $timeout);
  send_string("name", true);
  send_string("password", true);
  print_r(read_string());
  ...
  </code></pre>