1

I've looked in to this a bit but still don't really understand if its possible.

I have a small webserver that sits on my private network. I want to create a php script that will connect to a telnet server on the private network and send it text every 30 seconds.

Obviously the easy part is the text and timing but connection to a TCP Port 23 and sending a string of text seems harder for some reason. What's the best way to do this?

2
  • The php manual has an example for socket . Commented Apr 18, 2017 at 15:09
  • And I tried it. I can connect and it shows the connection but I can't seem to send a string of text to the device. Commented Apr 19, 2017 at 16:16

1 Answer 1

5

Use PHP sockets :

<?php
while(true){
    sleep 30;
    $fp = fsockopen("www.example.com", 23, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        fwrite($fp, "The string you want to send");
        while (fgets($fp, 128)) {
            echo fgets($fp, 128); // If you expect an answer
        }
        fclose($fp); // To close the connection
    }
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

That means until the end of file :)
That's what you would think, but it's wrong. It does an extra fgets() after the end of file. Read the linked question. You should use while (fgets($fp, 128))
I think the while (fgets($fp, 128)) edit is wrong. The while will read data then the echo will read more data, not the same data again. You should be reading into a buffer and echoing that, borrowing from the linked question... while (($buffer = fgets($fp, 128)) !== false) { echo $buffer; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.