Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 402191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:06:09+00:00 2026-05-12T17:06:09+00:00

I’m running PHP5 on Windows XP Professional. I’m trying to write a telnet php

  • 0

I’m running PHP5 on Windows XP Professional. I’m trying to write a telnet php script which simply connects, sends a text string and grabs the response buffer and outputs it. I’m using the telnet class file from here:

http://cvs.adfinis.ch/cvs.php/phpStreamcast/telnet.class.php

which i found in another thread.

<?php 
error_reporting(255);
ini_set('display_errors', true);

echo "1<br>";
require_once("telnet_class.php");

$telnet = new Telnet(); 

$telnet->set_host("10.10.5.7"); 
$telnet->set_port("2002");
$telnet->connect();
//$telnet->wait_prompt();
$telnet->write('SNRD   1%0d');
echo "3<br>";
$result = $telnet->get_buffer();
        echo $result;
        print_r($result);
//        flush_now();
echo "4<br>";

$telnet->disconnect();

?>

I’m not receiving any kind of errors or response. If I send an invalid string, I should get an ‘ERR’ response in the least however I don’t even get that. Any ideas what i could be doing wrong? If I do the same thing from the command prompt, I receive the string output I need. Could this is because the write function is sending

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:06:10+00:00Added an answer on May 12, 2026 at 5:06 pm

    After some reading in the source code and on the original (french) site referred to in the header….

    <?php 
    error_reporting(255);
    ini_set('display_errors', true);
    
    echo "1<br>";
    require_once("telnet_class.php");
    
    $telnet = new Telnet(); 
    
    $telnet->set_host("10.10.5.7"); 
    $telnet->set_port("2002");
    if ($telnet->connect() != TELNET_OK) {
         printf("Telnet error on connect, %s\n",$telnet->get_last_error());
    }
    //$telnet->wait_prompt();
    if ($telnet->write('SNRD   1' . "\xd") != TELNET_OK) {
         printf("Telnet error on write, %s\n",$telnet->get_last_error());
    }
    
    // read to \n or whatever terminates the string you need to read
    if ($telnet->read_to("\n") != TELNET_OK) {  
         printf("Telnet error on read_to, %s\n",$telnet->get_last_error());
    }
    echo "3<br>";
    
    
    $result = $telnet->get_buffer();
            echo $result;
            print_r($result);
    //        flush_now();
    echo "4<br>";
    
    $telnet->disconnect();
    
    ?>
    

    Okay, explanation: get_buffer() does just that, read what’s in the buffer. To get something in the buffer you have to execute read_to($match) who will read into buffer up to $match. After that, get_buffer should give you the desired string.

    EDIT:
    if you cannot find some string that follows the string you are interested in read_to will end in an error due to this part of the read_to method (translation of original french comment is mine):

        if ($c === false){
         // plus de caracteres a lire sur la socket
         // --> no more characters to read on the socket
            if ($this->contientErreur($buf)){
                return TELNET_ERROR;
            }
    
            $this->error = " Couldn't find the requested : '" . $chaine . "', it was not in the data returned from server : '" . $buf . "'" ;
            $this->logger($this->error);
            return TELNET_ERROR;
        } 
    

    Meaning that when the socket is closed without a match of the requested string, TELNET_ERROR will be returned. However, the string you’re looking for should at that point be in the buffer…. What did you put in read_to’s argument? “\n” like what I did or just “” ?

    EDIT2 :
    there’s also a problem with get_buffer. IMO this class is not really a timesaver 😉

    //------------------------------------------------------------------------
    function get_buffer(){
        $buf = $this->buffer;
    
        // cut last line (is always prompt)
        $buf = explode("\n", $buf);
        unset($buf[count($buf)-1]);
        $buf = join("\n",$buf);
        return trim($buf);
    } 
    

    It will throw away the last line of the response, in your case the one that contains the
    answer.
    I suggest to add a “light” version of get_buffer to the class, like this

    //------------------------------------------------------------------------
    function get_raw_buffer(){
        return $this->buffer;
    

    }

    and do the necessary trimming/searching in the result yourself.

    You might also want to add the following constant

    define ("TELNET_EOF", 3);
    

    and change read_to like this

    ...
    if ($c === false){
        // plus de caracteres a lire sur la socket
        if ($this->contientErreur($buf)){
            return TELNET_EOF;
        }
    
        $this->error = " Couldn't find the requested : '" . $chaine . "', it was not in the data returned from server : '" . $buf . "'" ;
        $this->logger($this->error);
        return TELNET_EOF;
    } 
    ...
    

    in order to treat that special case yourself (a result code TELNET_EOF doesn’t have to be treated as an error in your case). So finally your code should look more or less like this:

    // read to \n or whatever terminates the string you need to read 
    if ($telnet->read_to("\n") == TELNET_ERROR) {  
        printf("Telnet error on read_to, %s\n",$telnet->get_last_error()); } echo "3<br>";
    } else {
        $result = $telnet->get_raw_buffer();
        echo $result;
        print_r($result);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.