当前位置:网站首页>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;
}
边栏推荐
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
- Gartner 权威预测未来4年网络安全的8大发展趋势
- npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.5 Other Differences Between Arrays and Pointers
- MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
- TensorRT例程解读之语义分割demo
- 力扣:70. 爬楼梯
- 力扣:62.不同路径
- 如何低成本修bug?测试左移给你答案
- Wwise入门和实战
猜你喜欢
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
FPGA学习笔记——知识点总结
擎朗智能全国研发创新中心落地光谷:去年曾获2亿美元融资
TSF微服务治理实战系列(一)——治理蓝图
7.18 Day23 - the markup language
触觉智能分享-SSD20X实现升级显示进度条
【问题解决】同一机器上Flask部署TensorRT报错记录
The cost of automated testing is high and the effect is poor, so what is the significance of automated testing?
Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
canal实现mysql数据同步
随机推荐
el-Select selector bottom fixed
4.1 JdbcTemplate for declarative transactions
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
企业需要知道的5个 IAM 最佳实践
利用Jenkins实现Unity自动化构建
JS basics - forced type conversion (error-prone, self-use)
符号表
动态规划总括
7.16 Day22---MYSQL(Dao模式封装JDBC)
7、特殊SQL的执行
读者让我总结一波 redis 面试题,现在肝出来了
12、分页插件
[Cocos] cc.sys.browserType可能的属性
入坑软件测试的经验与建议
The idea setting recognizes the .sql file type and other file types
触觉智能分享-SSD20X实现升级显示进度条
7. Execution of special SQL
Tactile intelligent sharing - SSD20X realizes upgrade display progress bar
Landing, the IFC, GFC, FFC concept, layout rules, forming method, use is analysed
npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法