当前位置:网站首页>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;
}
边栏推荐
- Unity自动生成阻挡Collider的GameObject工具
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
- 力扣:343. 整数拆分
- 【JS】js给对象动态添加、设置、删除属性名和属性值
- 解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题
- flink cdc一启动,源端Oracle那台服务器的CPU就飙升到80%以上,会是啥原因呢?
- 心余力绌:企业面临的软件供应链安全困境
- Interesting Kotlin 0x0E: DeepRecursiveFunction
- leetcode 12. Integer to Roman numeral
- 7.18 Day23----标记语言
猜你喜欢

解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题

如何将 DevSecOps 引入企业?

【论文阅读笔记】无监督行人重识别中的采样策略

代码重构:面向单元测试

Summary of MySQL database interview questions (2022 latest version)

Grain Mall - Basics (Project Introduction & Project Construction)

字节最爱问的智力题,你会几道?

EntityComponentSystemSamples学习笔记

Get the selected content of the radio box

Cannot read properties of null (reading ‘insertBefore‘)
随机推荐
想低成本保障软件安全?5大安全任务值得考虑
Landing, the IFC, GFC, FFC concept, layout rules, forming method, use is analysed
读者让我总结一波 redis 面试题,现在肝出来了
Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
idea设置识别.sql文件类型以及其他文件类型
7.16 Day22---MYSQL(Dao模式封装JDBC)
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
Plus版SBOM:流水线物料清单PBOM
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
触觉智能分享-SSD20X实现升级显示进度条
一个对象引用的思考
企业需要知道的5个 IAM 最佳实践
As soon as flink cdc is started, the CPU of the source Oracle server soars to more than 80%. What is the reason?
如何低成本修bug?测试左移给你答案
力扣:746. 使用最小花费爬楼梯
Embedded system driver primary [3] - _IO model in character device driver foundation
Cannot read properties of null (reading ‘insertBefore‘)
4.1 JdbcTemplate for declarative transactions
Performance testing with Loadrunner
[Cocos] cc.sys.browserType可能的属性