您的位置:首页 > 编程语言 > PHP开发

php来实现telnet的连接、传递命令、获取返回值等功能!

2012-08-05 13:28 447 查看
<?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.
//
echo "we wont ".ord($opt)."\n";


69.
fwrite(
$this
->sock,
$IAC
.
$WONT
.
$opt
);


70.
}


71.
elseif
((
$c
==
$WILL
)
|| (
$c
==
$WONT
))
{


72.
$opt
=
$this
->getc();


73.
//
echo "we dont ".ord($opt)."\n";


74.
fwrite(
$this
->sock,
$IAC
.
$DONT
.
$opt
);


75.
}


76.
else
{


77.
//
echo "where are we? c=".ord($c)."\n";


78.
}


79.
}


80.
}


81.
}


82.


83.
/*


84.
$telnet
=new telnet("192.168.0.1",23);


85.
echo
$telnet->read_till("login: ");


86.
$telnet->write("kongxx\r\n");


87.
echo
$telnet->read_till("password: ");


88.
$telnet->write("KONGXX\r\n");


89.
echo
$telnet->read_till(":> ");


90.
$telnet->write("ls\r\n");


91.
echo
$telnet->read_till(":> ");


92.
echo
$telnet->close();


93.


94.
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: