当前位置:网站首页>php实现telnet访问端口
php实现telnet访问端口
2022-08-04 05:25:00 【eleven_-11】
<?php
/******************************************************************************************** * Copy Right (c) 2021 Capsheaf Co., Ltd. * * Author: Archibald<[email protected]> * Date: 2021-04-02 16:25:37 CST * Description: telnet.php's function description * Version: 1.0.0.20210402-alpha * History: * Archibald<[email protected]> 2021-04-02 16:25:37 CST initialized the file *******************************************************************************************/
error_reporting(E_ALL ^ E_NOTICE);
class Telnet
{
private $hSocket = null;
public function telnet($sHost, $nPort)
{
$this->hSocket = @fsockopen($sHost, $nPort);
if (empty($this->hSocket)){
throw new Exception("connect ip:{$sHost} port:{$nPort} failed.");
}
socket_set_timeout($this->hSocket, 2, 0);
}
public function close()
{
if ($this->hSocket){
fclose($this->hSocket);
}
$this->hSocket = null;
}
public function write($buffer)
{
$buffer = str_replace(chr(255), chr(255).chr(255), $buffer);
fwrite($this->hSocket, $buffer);
}
public function getc()
{
return fgetc($this->hSocket);
}
public function readTill($what)
{
$buffer = '';
while(true){
$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 $buffer;
}
if ($c == $theNULL) {
continue;
}
if ($c == "1") {
continue;
}
if ($c != $IAC) {
$buffer .= $c;
if ($what == (substr($buffer, strlen($buffer) - strlen($what)))) {
return $buffer;
} else {
continue;
}
}
$c = $this->getc();
if ($c == $IAC) {
$buffer .= $c;
} else if (($c == $DO) || ($c == $DONT)) {
$opt = $this->getc();
fwrite($this->hSocket, $IAC . $WONT . $opt);
} elseif (($c == $WILL) || ($c == $WONT)) {
$opt = $this->getc();
fwrite($this->hSocket, $IAC . $DONT . $opt);
}
}
}
}
try {
$telnet = new Telnet("192.168.173.2",28030);
if (empty($telnet)){
echo "connect failed.";
} else {
echo "connect success.";
}
// echo $telnet->readTill("login: ");
// $telnet->write("kongxx\r\n");
// echo $telnet->readTill("password: ");
// $telnet->write("KONGXX\r\n");
// echo $telnet->readTill(":> ");
// $telnet->write("ls\r\n");
// echo $telnet->readTill(":> ");
$telnet->close();
} catch(Exception $exception){
var_dump("connect failed: " . $exception->getMessage());
exit;
}
边栏推荐
- 9、动态SQL
- el-Select selector bottom fixed
- C1认证之web基础知识及习题——我的学习笔记
- npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
- 自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
- 解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题
- 【论文阅读笔记】无监督行人重识别中的采样策略
- Dynamic programming of the division of numbers
- OpenCV获取和设置图像的平均亮度
- JS基础--强制类型转换(易错点,自用)
猜你喜欢
sql server如何得到本条记录与上一条记录的差异,即变动值
8. Custom mapping resultMap
The difference between px, em, and rem
在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
The idea setting recognizes the .sql file type and other file types
代码重构:面向单元测试
想好了吗?
编程大杂烩(三)
Embedded system driver primary [4] - under the basis of character device driver _ concurrency control
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
随机推荐
触觉智能分享-SSD20X实现升级显示进度条
Tactile intelligent sharing - SSD20X realizes upgrade display progress bar
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
文献管理工具 | Zotero
7.15 Day21---MySQL----Index
LCP 17. Quick Calculation Robot
离线采集怎么看sql执行计划
TensorRT例程解读之语义分割demo
How to view sql execution plan offline collection
warning C4251: “std::vector&lt;_Ty&gt;”需要有 dll 接口由 class“Test”的客户端使用错误
DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片
Summary of MySQL database interview questions (2022 latest version)
Will the 2023 PMP exam use the new version of the textbook?Reply is here!
7.16 Day22---MYSQL (Dao mode encapsulates JDBC)
FFmpeg源码分析:avformat_open_input
Interesting Kotlin 0x0E: DeepRecursiveFunction
Redis common interview questions
The symbol table
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
About yolo7 and gpu