Topic: [Script] Simple PHP function for D2GS telnet commands
Inspired by https://github.com/pvpgn/d2smanager-api:
<?php
// Command
$telnet = Telnet('maxuser 200');
if($telnet == 'Yes') {
echo 'Maximum users has been updated.';
}
else {
echo $telnet;
}
// Function
function Telnet($command, $host = '127.0.0.1', $port = '8888', $password = 'abcd123') {
$handler = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($handler) {
socket_set_option($handler, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 3, 'usec' => 0));
if(@socket_connect($handler, $host, $port)) {
while($out = @socket_read($handler, 1024)) {
if(preg_match('/Password:/i', $out)) {
break;
}
}
socket_write($handler, $password . "\n", strlen($password) + 1);
while($out = @socket_read($handler, 1024)) {
if(preg_match('/D2GS>/i', $out)) {
$logged = 1;
}
if(preg_match('/Sorry!/i', $out)) {
$logged = 0;
}
}
if($logged == 1) {
socket_write($handler, $command . "\n", strlen($command) + 1);
$result = 'Yes';
}
else {
$result = 'Incorrect password for socket.';
}
}
else {
$result = 'Error Connecting Telnet: ' . socket_strerror(socket_last_error());
}
socket_close($handler);
}
else {
$result = 'Error Creating Socket: ' . socket_strerror(socket_last_error());
}
return $result;
}
?>