当前位置 : 主页 > 网络编程 > PHP >

PHP模拟telnet

来源:互联网 收集:自由互联 发布时间:2021-06-28
PHP模拟telnet function creatSock($host="127.0.0.1",$port=23){ /*建立并返回网络连接*/ $fp = fsockopen($host,$port, $errno, $errstr, 30); if (!$fp) { return false; } else { stream_set_timeout($fp, 30); return $fp; } }function read_
PHP模拟telnet
function creatSock($host="127.0.0.1",$port=23){
    /*建立并返回网络连接*/
    $fp = fsockopen($host,$port, $errno, $errstr, 30);
    if (!$fp) {
        return false;
    } else {
        stream_set_timeout($fp, 30);
        return $fp;
    }    
}


function read_till($sock,$what="Username:",$seWhat=''){
    $buf = '';
    if($seWhat=='')$seWhat=$what;
    /*
    使用2个搜索字以应对出现的2种情况
    */
    while (true) {
        /*
        chr()返回相对应于 ascii 所指定的单个字符
        从 0 到 31 的数字表示标准的不可打印的 ASCII 代码
        通过查ASCII码表知 0 = NULL(空)	只有0-127
        */
        $IAC = chr(255);//
        $DONT = chr(254);//
        $DO = chr(253);//
        $WONT = chr(252);//
        $WILL = chr(251);//
        $theNULL = chr(0);//空字符
        /*
        fgetc从文件指针中读取一个字符.
        怎么判断已经读完了所有字符?
        if ($c === false)
        if ($c == $theNULL)
        if ($c != $IAC){}else{}
        */
        $c = fgetc($sock);
        //echo ord($c).'';
        if ($c === false) return $buf;//如果文件指针返回假则直接返回终止
        if ($c == $theNULL) {continue;}
        /*
        空字符,本次终止,进入下一次循环
        空字符说明,连接正常,只是当前连接没有收到返回的字符。
        */
        //if ($c == "1") {continue;}//? 这样所有的1都不会被读出来!
        if ($c != $IAC) {
            //此时连接返回的字符是正常字符
            $buf .= $c;//让之前读取的字符与本次读取的字符连接
            /*
                strlen($buf)字符串的总长度
                strlen($what)查找字符串的长度
                substr(string,start,length)返回字符串 string 由 start 和 length 参数指定的子字符串
                即从所有已经返回的字符中,从总长-查询长的位置截取字符串?
                $buf=abcdefg  $what=cde 返回efg
                即:只从接收到的字符串中返回,最后的,与查询字符串长度相同的子字符串与查询字符相比较!
                所以,输入的要查询的字符串应当在返回字符串的最后面!
            */
            if("Error:"==substr($buf,strlen($buf)-6)){
                return $buf;//检索到错误后返回结果。
            }
            if($seWhat==substr($buf,strlen($buf)-strlen($seWhat))){
                return $buf;//检索到第2关键字后返回结果。
            }
            if($what == (substr($buf,strlen($buf)-strlen($what)))) {
                return $buf;//检索到第1关键字后返回结果,否则进入下一次循环。
            }else{
                continue;
            }
        }
        $c = fgetc($sock);
        if ($c == $IAC) {
            $buf .= $c;
        }else if (($c == $DO) || ($c == $DONT)) {
            $opt = fgetc($sock);
            // echo "we wont ".ord($opt)."\n";
            fwrite($sock,$IAC.$WONT.$opt);
        }elseif (($c == $WILL) || ($c == $WONT)) {
            $opt =fgetc($sock);
            // echo "we dont ".ord($opt)."\n";
            fwrite($sock,$IAC.$DONT.$opt);
        }else {
            // echo "where are we? c=".ord($c)."\n";
        }
        

    }
}


public function write($sock,$buffer) {
    /*使用网络连接写入数据*/
    $buffer = str_replace(chr(255),chr(255).chr(255),$buffer);
    fwrite($sock,$buffer);
}
网友评论