0

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.
  • It is a little bit unclear what you want to do with the `fsockopen()` call there. You are not doing anything with the `$connection` variable. Do you want to start a server with your PHP script, which you can connect to via "telnet" or do you want to connect to an existing telnet server with your PHP script as a client? What is the server and what is the client in your scenario? – Progman Sep 13 '19 at 20:06
  • @Progman Let me clear myself here. I want to achieve this using php. Something like this. if someone types `telnet subdomain.mydomain.com` It should connect to it without showing `Escape character is '^]'.` & show a menu to do operations like 1. Login 2. Register 3. Exit something like this. The way we use this `telnet telehack.com` So I'll have my own linux based shared server and MySQL database to store data. Or if i'm wrong then please guide me how i can achieve this. Thank you so much – Curious Developer Sep 14 '19 at 06:16

2 Answers2

0

echo won't work here because echo puts output to STDIO. You need to write to the socket, with the fwrite function, like you would do to a normal system file.

Check it out:

https://www.php.net/manual/en/function.fsockopen.php

https://www.php.net/manual/en/function.fwrite.php

taiar
  • 552
  • 6
  • 22
0

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);
?>
Progman
  • 16,827
  • 6
  • 33
  • 48
  • Yes very first I visited this site only & got this example. But the error was permission denied & also `socket_bind(): unable to bind address [98]: Address already in use` . So i changed my Apache2 port from 80 to 2323 so that above 1024 we don't need permission and the port would be available. So please guide me where I'm doing wrong? I just changed these lines from the code you provided above : `$address = '192.168.1.203';` `$port = 2323;` – Curious Developer Sep 14 '19 at 07:00
  • @AndroidDeveloper You can't have two applications/services/servers running on the same port. When you have your apache running on port 80 and a PHP script try to create a server on the same port you will get this error message. You have to pick a free port for your PHP script, you don't use the port which is already being used (by your apache server). You can keep your apache server running on port 80 and let your PHP script run a server on any other free port you have (like port 10000 as in the example). – Progman Sep 14 '19 at 07:06
  • Thank you so much Sir, the script is running properly now. But how can I run this on actual server and instead of port number 10000 How can run like this `telnet subdomain.mydomain.com` so it take port 23 as default one. Just like this example `telnet towel.blinkenlights.nl` – Curious Developer Sep 14 '19 at 07:25
  • @AndroidDeveloper You do not add another port to the apache configuration, keep it on port 80. Use the port 10000 inside your PHP script and run it. As long as the PHP script is running your can connect to it via `telnet ip 10000`. Btw.: It is not required that your PHP script is executed through apache. If possible you can run it from the CLI like "php yourfile.php". This way you don't need the apache server at all to have your server from your PHP script. – Progman Sep 14 '19 at 07:25
  • Yes sir, I exactly did that php index.php & then I telnet it. – Curious Developer Sep 14 '19 at 07:28
  • @AndroidDeveloper You can use any port you want, including port 23. But for the lower ports (<1024) you need admin/root permissions to use them. So you need to run your PHP script with the root user (which is not recommended security wise). – Progman Sep 14 '19 at 07:39
  • Ok sir. So I shouldn't use port numbers below 1024 right? Thank you so much sir for your guidance. :) – Curious Developer Sep 14 '19 at 09:09
  • @AndroidDeveloper You can use them, but then your need more permissions (admin/root) to use them (see https://stackoverflow.com/questions/10182798/why-are-ports-below-1024-privileged). – Progman Sep 14 '19 at 09:12
  • Yes Sir. I just read the whole discussion and the accepted answer. So I need a root permissions to listen ports below 1024. Which is not available in shared hosting services. So what is the best way to run own php server script securely. So users shouldn't be worried about the security issues. Thanks – Curious Developer Sep 14 '19 at 09:19
  • @AndroidDeveloper You pick any port above 1024 to run your PHP script server so you don't need root permissions to use it. Using `10000` is totally fine. – Progman Sep 14 '19 at 09:24
  • Yeah but in which cases I should use port 23 or below 1000? If i use above 1000 is there any security issues that can happen with my users or to my server? – Curious Developer Sep 14 '19 at 09:38
  • Sir can you help me with one more thing in this! I want this code to ask user for their input until user types exit. But right now if user gives some input then after the connection get automatically closed. Thank you – Curious Developer Sep 14 '19 at 17:29
  • @AndroidDeveloper The example code already shows you how to use a loop (a `do...while` loop) to keep reading data from the user. You can use this technique for your goal to read user input until the user enters "exit". In fact, the example code already has a check if the user enters "quit" which will disconnect the user. You can try to build a system around the socket functions to read/write data you want. If you have a question ask a new question as this one seems answered/solved. – Progman Sep 14 '19 at 17:36
  • Ohk sure sir. I'll first try this example as you've said then I'll ask another question. But i dont know if i can tag you in that :p – Curious Developer Sep 14 '19 at 17:38