1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
<?php
error_reporting(-1);
class Telnet
{
var $sock = NULL;
function telnet($host,$port)
{
$this->sock = fsockopen($host,$port);
socket_set_timeout($this->sock,2,0);
}
function close()
{
if ($this->sock)
fclose($this->sock);
$this->sock = NULL;
}
function write($buffer)
{
$buffer = str_replace(chr(255),chr(255).chr(255),$buffer);
fwrite($this->sock,$buffer);
}
function getc()
{
return fgetc($this->sock);
}
function lire_juska($what)
{
$buf = '';
while (1) {
$IAC = chr(255);
$DONT = chr(254);
$DO = chr(253);
$WONT = chr(252);
$WILL = chr(251);
$theNULL = chr(0);
$c = $this->getc();
if ($c === false)
return $buf;
if ($c == $theNULL)
{
continue;
}
if ($c == "\021")
{
continue;
}
if ($c != $IAC)
{
$buf .= $c;
if ($what == (substr($buf,strlen($buf)-strlen($what))))
{
return $buf;
}
else
{
continue;
}
}
$c = $this->getc();
if ($c == $IAC)
{
$buf .= $c;
}
else if (($c == $DO) || ($c == $DONT))
{
$opt = $this->getc();
// echo "we wont ".ord($opt)."\n";
fwrite($this->sock,$IAC.$WONT.$opt);
}
elseif (($c == $WILL) || ($c == $WONT))
{
$opt = $this->getc();
// echo "we dont ".ord($opt)."\n";
fwrite($this->sock,$IAC.$DONT.$opt);
}
else
{
// echo "where are we? c=".ord($c)."\n";
}
}
}
}
?> |