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