ev5unleash1 Posted December 3, 2012 Share Posted December 3, 2012 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/ Share on other sites More sharing options...
requinix Posted December 3, 2012 Share Posted December 3, 2012 I'm not entirely sure what you're trying to do here. The client isn't just a simple telnet connection? Is the script trying to connect to itself? What's the "shell code here" you're executing? Why are you nohuping anything? Why port 86? Are you simulating a "BroadCam Video Streaming Server" or "Micro Focus Cobol" (as mentioned here)? Quote Link to comment https://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/#findComment-1397057 Share on other sites More sharing options...
ev5unleash1 Posted December 3, 2012 Author Share Posted December 3, 2012 1. This script will connect to another telnet server and wait for commands to be sent. It will interpret what is sent and execute certain commands based upon it. The shell code can range from anything to executing another PHP script to sending a reboot command. I'm nohuping because I found it was the best way to restart the script when the connection fails, thus waiting for a connection when the previous one fails. Quote Link to comment https://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/#findComment-1397061 Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 My first guess would be that it's waiting for a sudo password. That said, I don't see any reason why you should kill the script and restart it as a new process, for each time it executed a command. Run the main body inside an endless loop, and only exit if given the proper command. Quote Link to comment https://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/#findComment-1397082 Share on other sites More sharing options...
ev5unleash1 Posted December 3, 2012 Author Share Posted December 3, 2012 I thought about this, but the commands work as soon as a start the script. Only after I send a few commands and wait a little bit is when the script will stop responding. I did a packet mirror on the Pi and it seems that it stops accepting or kills the socket connection at that time. I double checked my server by having putty connected to the same script but on a different port and that doesn't seem to stop responding. Quote Link to comment https://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/#findComment-1397134 Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 Might be that the server doesn't allow you to override the max runtime of a PHP script, or otherwise limit the max running time of a process. Shared hosts tend to do that, in order to prevent that a single customer uses too much resources at any given time. Quote Link to comment https://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/#findComment-1397142 Share on other sites More sharing options...
ev5unleash1 Posted December 3, 2012 Author Share Posted December 3, 2012 The server is configured with default_socket_timeout = 0 and both are my own personal servers. The socket host is a WAMP server and the client that I'm having trouble with is a Linux server. Quote Link to comment https://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/#findComment-1397145 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.