Skip to content

Instantly share code, notes, and snippets.

@Rotron
Last active June 29, 2025 13:10
Show Gist options
  • Select an option

  • Save Rotron/f2428c34418e0bfd7e53618a13d77d78 to your computer and use it in GitHub Desktop.

Select an option

Save Rotron/f2428c34418e0bfd7e53618a13d77d78 to your computer and use it in GitHub Desktop.
PHP Telnet Server With Menu
#!/usr/local/bin/php -q
<?php
//a little bit of fun with a socket server
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 = '0.0.0.0';
$port = 10000;
$bash = "Rotron.net > ";
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
die();
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
die();
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
die();
}
//clients array
$clients = array();
do {
$read = array();
$read[] = $sock;
$read = array_merge($read,$clients);
$write = NULL;
$except = NULL;
// Set up a blocking call to socket_select
if(socket_select($read,$write, $except, $tv_sec = 5) < 1)
{
// SocketServer::debug("Problem blocking socket_select?");
continue;
}
// Handle new Connections
if (in_array($sock, $read)) {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
$clients[] = $msgsock;
$key = array_keys($clients, $msgsock);
$msg="Connected\n";
socket_write($msgsock, $msg, strlen($msg));
/* Send instructions. */
$msg = "\n
██▀███ ▒█████ ▄▄▄█████▓ ██▀███ ▒█████ ███▄ █ ███▄ █ ▓█████▄▄▄█████▓
▓██ ▒ ██▒▒██▒ ██▒▓ ██▒ ▓▒▓██ ▒ ██▒▒██▒ ██▒ ██ ▀█ █ ██ ▀█ █ ▓█ ▀▓ ██▒ ▓▒
▓██ ░▄█ ▒▒██░ ██▒▒ ▓██░ ▒░▓██ ░▄█ ▒▒██░ ██▒▓██ ▀█ ██▒ ▓██ ▀█ ██▒▒███ ▒ ▓██░ ▒░
▒██▀▀█▄ ▒██ ██░░ ▓██▓ ░ ▒██▀▀█▄ ▒██ ██░▓██▒ ▐▌██▒ ▓██▒ ▐▌██▒▒▓█ ▄░ ▓██▓ ░
░██▓ ▒██▒░ ████▓▒░ ▒██▒ ░ ░██▓ ▒██▒░ ████▓▒░▒██░ ▓██░ ██▓ ▒██░ ▓██░░▒████▒ ▒██▒ ░
░ ▒▓ ░▒▓░░ ▒░▒░▒░ ▒ ░░ ░ ▒▓ ░▒▓░░ ▒░▒░▒░ ░ ▒░ ▒ ▒ ▒▓▒ ░ ▒░ ▒ ▒ ░░ ▒░ ░ ▒ ░░
░▒ ░ ▒░ ░ ▒ ▒░ ░ ░▒ ░ ▒░ ░ ▒ ▒░ ░ ░░ ░ ▒░ ░▒ ░ ░░ ░ ▒░ ░ ░ ░ ░
░░ ░ ░ ░ ░ ▒ ░ ░░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░
".
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n" . $bash;
socket_write($msgsock, $msg, strlen($msg));
}
foreach ($clients as $key => $client) { // for each client
if (in_array($client, $read)) {
if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
echo "socket_read() Error: " . socket_strerror(socket_last_error($client)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == "\n") {
continue;
}
if ($buf == 'quit') {
$bye="\n Bye Bye! \n";
socket_write($msgsock, $bye, strlen($bye));
unset($clients[$key]);
socket_close($client);
break;
}
if ($buf == 'shutdown') {
socket_shutdown($client, 2);
socket_close($client);
break 2;
}
if ($buf == 'date') {
$data= date("Y-m-d"). "\n" . $bash;
socket_write($msgsock, $data, strlen($data));
}
if ($buf == 'clear') {
$data= chr(27).chr(91).'H'.chr(27).chr(91).'J' . $bash;
socket_write($msgsock, $data, strlen($data));
}
if ($buf == 'help') {
$data= "\n
┌─────────────┬──────────────────────────────────────────────────────────┐
│ Command │ Description │
├─────────────┼──────────────────────────────────────────────────────────┤
│ quit │ Close the connection │
├─────────────┼──────────────────────────────────────────────────────────┤
│ shutdown │ Shutdown the server │
├─────────────┼──────────────────────────────────────────────────────────┤
│ date │ Date time │
├─────────────┼──────────────────────────────────────────────────────────┤
│ │ │
├─────────────┼──────────────────────────────────────────────────────────┤
│ │ │
├─────────────┼──────────────────────────────────────────────────────────┤
│ help │ Help commands list │
└─────────────┴──────────────────────────────────────────────────────────┘
"
. $bash;
socket_write($msgsock, $data, strlen($data));
}
/*
$talkback = "Client {$key}: said '$buf'.\n";
foreach ($clients as $sub_key => $sub_client)
{
socket_write($sub_client, $talkback, strlen($talkback));
}
echo "$buf\n";
//exec('say "'. $buf . '"');
*/
}
}
} while (true);
socket_close($sock);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment