Search the Community
Showing results for tags 'php telnet control'.
-
Hi guys, I'm at a standstill with this telnet script I'm trying to perfect. The scripts job is to interpret a command on the telnet connection and execute different commands based upon it. It's executed as a standalone script by the system, so no HTTP requests are involved. The script works for what I need, although after about a minute or so the script just freezes. Though the process still exists and I'm forced to re-open it. The server doesn't seem to hang at all, just the client side stuff. Hopefully I can get some insight as to why it hangs, thanks! <?php set_time_limit(0); //error_reporting(E_ALL); error_reporting(0); /* Get the port for the WWW service. */ //$service_port = getservbyname(86, 'tcp'); $service_port = 86; /* Get the IP address for the target host. */ $address = gethostbyname('removed'); /* Create a TCP/IP socket. */ $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if ($socket === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; } else { echo "Creating Socket... [OK]\n"; } echo "Attempting to connect to '$address' on port '$service_port'..."; $result = socket_connect($socket, $address, $service_port); if ($result === false) { echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n"; } else { echo "[OK]\n"; echo "Script Running...\n"; } $in = "Connect"; echo "Sending connect to server..."; socket_write($socket, $in, strlen($in)); echo "OK.\n"; //echo "Reading response:\n\n"; function probe($data) { $remote = $data; /* Code intentionally removed. Just saw variable and executed something based upon what it saw. */ if(strstr($remote, "rpi_reboot")) { shell_exec("sudo reboot"); } usleep('1000'); //10ms } $outnew = "0"; while ($out = socket_read($socket, 1000, PHP_BINARY_READ)) { echo $out; if($out != $outnew) { probe($out); $outnew = $out; usleep(1000); //To have a fast response time } else { } } /* When connection fails, kill all processes */ shell_exec("shell code here"); sleep(1); echo "Closing socket..."; socket_close($socket); echo "OK.\n\n"; sleep(1); exec("nohup sudo php /var/www/telnet.php > /dev/null 2>&1 &"); //Close process and open another exit(); ?>