phptelnetphp-socket

Making Telnet program in php stuck at this : Escape character is '^]'


I'm trying to create something like this :

telnet towel.blinkenlights.nl 666

which you have to write in terminal & it send you some response and user can also send input if server asks.

I've searched alot for it but didn't get enough. Just this little code :

<?php

$IP_ADDR='127.0.0.1';
$PORT='80';
echo "Welcome to LOCALHOST...";
$connection = fsockopen($IP_ADDR, $PORT);

if(!$connection){

echo "No Connection";

}else{

echo "Hello, what's your name?";

}

?>

But the output shows something like this :

telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

So after connected with localhost it's not showing the output, I've to type any char then press enter then it shows this output :

g
Welcome to LOCALHOST...Hello, what's your name?Connection closed by foreign host.

So how can I avoid this everytime inserting char for output?

Please help me. Thanks

Edited Code :

My Code :

$IP_ADDR='127.0.0.1';
$PORT='80';
$connection = fsockopen($IP_ADDR, $PORT);

if(!$connection){
echo "No Connection";
}else{
$filename = "sampledata.txt";
$somecontent = "Weekend is about to come.";
if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file";
         exit;
    }  
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file";
        exit;
    }

    echo "Success";
    fclose($handle);
} else {
    echo "The file $filename is not writable";
}
}

Output : 
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
d
Success, wrote (Weekend is about to come.) to file (sampledata.txt)Connection closed by foreign host.

Solution

  • You use the socket functions to create a server inside your PHP script. Check the page Example #1 Socket example: Simple TCP/IP server provided by the PHP documentation how to create a server in PHP.

    #!/usr/local/bin/php -q
    <?php
    error_reporting(E_ALL);
    
    /* Allow the script to hang around waiting for connections. */
    set_time_limit(0);
    
    /* Turn on implicit output flushing so we see what we're getting
     * as it comes in. */
    ob_implicit_flush();
    
    $address = '192.168.1.53';
    $port = 10000;
    
    if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
        echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
    }
    
    if (socket_bind($sock, $address, $port) === false) {
        echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    }
    
    if (socket_listen($sock, 5) === false) {
        echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    }
    
    do {
        if (($msgsock = socket_accept($sock)) === false) {
            echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        /* Send instructions. */
        $msg = "\nWelcome to the PHP Test Server. \n" .
            "To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
        socket_write($msgsock, $msg, strlen($msg));
    
        do {
            if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
                echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
                break 2;
            }
            if (!$buf = trim($buf)) {
                continue;
            }
            if ($buf == 'quit') {
                break;
            }
            if ($buf == 'shutdown') {
                socket_close($msgsock);
                break 2;
            }
            $talkback = "PHP: You said '$buf'.\n";
            socket_write($msgsock, $talkback, strlen($talkback));
            echo "$buf\n";
        } while (true);
        socket_close($msgsock);
    } while (true);
    
    socket_close($sock);
    ?>