php实现telnet访问端口

<?php
/********************************************************************************************
 *             Copy Right (c) 2021 Capsheaf Co., Ltd.
 *
 *  Author:          Archibald<yangjunjie@capsheaf.com.cn>
 *  Date:            2021-04-02 16:25:37 CST
 *  Description:     telnet.php's function description
 *  Version:         1.0.0.20210402-alpha
 *  History:
 *        Archibald<yangjunjie@capsheaf.com.cn> 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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值