Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Telnet class<BR>
- * PHP class for l2jserver what connect to telnet server and execute some command.
- *
- * @author mochitto
- * @version 1.0.0.0
- */
- final class telnet
- {
- private $ip;
- private $port;
- private $timeout;
- private $password;
- private $socket;
- public $authed;
- function __construct($ip, $port, $password, $timeout = 1)
- {
- $this->ip = $ip;
- $this->port = $port;
- $this->timeout = $timeout;
- $this->password = $password;
- $this->authed = false;
- }
- function __destruct()
- {
- $this->disconnect();
- }
- /**
- * This function initializing connection to telnet server and log-in to that server.
- */
- public function init()
- {
- // Open socket to server
- $this->socket = @fsockopen($this->ip, $this->port, $errno, $errstr, $this->timeout);
- // When connection was failed say error and die...
- if ($this->socket === false)
- {
- echo '[TELNET]: Can\'t connect to server '.$this->ip.':'.$this->port;
- $this->socket = false;
- return;
- }
- // We can read data what sending telnet server but why? We dont need it at this time...
- $data = $this->read();
- // Yea, so send password and check it..
- if(!$this->auth())
- {
- echo '[TELNET]: The password was incorrect, please check your password...';
- return;
- }
- }
- /**
- * Function for disconnect from the server...
- */
- public function disconnect()
- {
- if ($this->socket)
- {
- // If we are authed, send the quit command for serious disc.
- if($this->authed)
- fwrite($this->socket, "quit\r");
- // close socket
- fclose($this->socket);
- $this->socket = false;
- }
- }
- /**
- * Here send password to telnet server and check if we are authed or not..
- *
- * @return boolean
- */
- private function auth()
- {
- $data = $this->write($this->password, true);
- if(strpos($data, "Password Correct!") !== false)
- $this->authed = true;
- else
- $this->disconnect();
- return $this->authed;
- }
- /**
- * Basic read data from telnet server with max lenght 512 bytes...
- *
- * @param int $size size of data to read
- * @return data from steam
- */
- private function read($size = 512)
- {
- if ($this->socket)
- {
- return @fread($this->socket, $size);
- }
- }
- /**
- * Write a command, automatic add \r at end of command what means enter after command and we check if socket exist and if we are authed.<BR>
- * We can send force command like password w/o auth.
- *
- * @param String $command command for telnet server
- * @param boolen $force true - send command w/o auth.
- * @return $data from server as reply, there using $this->read() func
- */
- public function write($command, $force = false)
- {
- if (!$this->socket)
- return;
- if (!$this->authed && !$force)
- {
- $this->disconnect();
- return;
- }
- @fwrite($this->socket, $command . "\r");
- return $this->read();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement