当前位置:网站首页>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;
}
边栏推荐
- 少年成就黑客,需要这些技能
- 解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题
- 文献管理工具 | Zotero
- LCP 17. 速算机器人
- 注意!软件供应链安全挑战持续升级
- Deploy LVS-DR cluster [experimental]
- [Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
- Landing, the IFC, GFC, FFC concept, layout rules, forming method, use is analysed
- 4.3 Annotation-based declarative transactions and XML-based declarative transactions
- CentOS7 - yum install mysql
猜你喜欢

谷粒商城-基础篇(项目简介&项目搭建)

符号表

Can 't connect to MySQL server on' localhost3306 '(10061) simple solutions

Programming hodgepodge (4)

The cost of automated testing is high and the effect is poor, so what is the significance of automated testing?
![[Cocos 3.5.2]开启模型合批](/img/d9/9e8f71c9a26c8052b11291fe3ba7ac.png)
[Cocos 3.5.2]开启模型合批

Wwise入门和实战

Unity行为树AI分享

【问题解决】同一机器上Flask部署TensorRT报错记录
![[Evaluation model] Topsis method (pros and cons distance method)](/img/e7/c24241faced567f3e93f6ff3f20074.png)
[Evaluation model] Topsis method (pros and cons distance method)
随机推荐
string类简介
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
12、分页插件
嵌入式系统驱动初级【3】——字符设备驱动基础中_IO模型
Several ways to heavy
Interesting Kotlin 0x0E: DeepRecursiveFunction
4.2 Declarative Transaction Concept
How to view sql execution plan offline collection
Get the selected content of the radio box
7.18 Day23 - the markup language
5个开源组件管理小技巧
Cannot read properties of null (reading 'insertBefore')
OpenSSF 安全计划:SBOM 将驱动软件供应链安全
7.15 Day21---MySQL----索引
TSF微服务治理实战系列(一)——治理蓝图
8、自定义映射resultMap
触觉智能分享-SSD20X实现升级显示进度条
字节最爱问的智力题,你会几道?
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
static在不同位置定义变量居然还有不同的含义?