怎么用php实现telnet

发布时间:2021-10-27 09:04:34 作者:iii
来源:亿速云 阅读:151

这篇文章主要介绍“怎么用php实现telnet”,在日常操作中,相信很多人在怎么用php实现telnet问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用php实现telnet”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1、创建一个PHP示例文件。

2、通过“class PHPTelnet {function Connect($server,$user,$pass) {...}}”方式实现telnet功能即可。

实例

<?php
class PHPTelnet {
 
    var $show_connect_error=1;
 
    var $use_usleep=0;  // change to 1 for faster execution
 
        // don't change to 1 on Windows servers unless you have PHP 5
 
    var $sleeptime=125000;
 
    var $loginsleeptime=1000000;
 
    var $fp=NULL;
 
    var $loginprompt;
 
    var $conn1;
 
    var $conn2;
 
    /*
 
    0 = success
 
    1 = couldn't open network connection
 
    2 = unknown host
 
    3 = login failed
 
    4 = PHP version too low
 
    */
 
    function Connect($server,$user,$pass) {
 
        $rv=0;
 
        $vers=explode('.',PHP_VERSION);
 
        $needvers=array(4,3,0);
 
        $j=count($vers);
 
        $k=count($needvers);
 
        if ($k<$j) $j=$k;
 
        for ($i=0;$i<$j;$i++) {
 
            if (($vers[$i]+0)>$needvers[$i]) break;
 
            if (($vers[$i]+0)<$needvers[$i]) {
 
                $this->ConnectError(4);
 
                return 4;
 
            }
 
        }
 
        $this->Disconnect();
 
        if (strlen($server)) {
 
            if (preg_match('/[^0-9.]/',$server)) {
 
                $ip=gethostbyname($server);
 
                if ($ip==$server) {
 
                    $ip='';
 
                    $rv=2;
 
                }
 
            } else $ip=$server;
 
        } else $ip='127.0.0.1';
 
        if (strlen($ip)) {
 
            if ($this->fp=fsockopen($ip,23)) {
 
                fputs($this->fp,$this->conn1);
 
                $this->Sleep();
 
                fputs($this->fp,$this->conn2);
 
                $this->Sleep();
 
                $this->GetResponse($r);
 
                $r=explode("\n",$r);
 
                $this->loginprompt=$r[count($r)-1];
 
                fputs($this->fp,"$user\n");
 
                $this->Sleep();
 
                fputs($this->fp,"$pass\n");
 
                if ($this->use_usleep) usleep($this->loginsleeptime);
 
                else sleep(1);
 
                $this->GetResponse($r);
 
                $r=explode("\n",$r);
 
                if (($r[count($r)-1]=='')||($this->loginprompt==$r[count($r)-1])) {
 
                    $rv=3;
 
                    $this->Disconnect();
 
                }
 
            } else $rv=1;
 
        }
 
        if ($rv) $this->ConnectError($rv);
 
        return $rv;
 
    }
 
    function Disconnect($exit=1) {
 
        if ($this->fp) {
 
            if ($exit) $this->DoCommand('exit',$junk);
 
            fclose($this->fp);
 
            $this->fp=NULL;
 
        }
 
    }
 
    function DoCommand($c,&$r) {
 
        if ($this->fp) {
 
            fputs($this->fp,"$c\n");
 
            $this->Sleep();
 
            $this->GetResponse($r);
 
            $r=preg_replace("/^.*?\n(.*)\n[^\n]*$/","$1",$r);
 
        }
 
        return $this->fp?1:0;
 
    }
 
    function GetResponse(&$r) {
 
        $r='';
 
        do {
 
            $r.=fread($this->fp, 1024);
 
            $s=socket_get_status($this->fp);
 
        } while ($s['unread_bytes']);
 
    }
 
    function Sleep() {
 
        if ($this->use_usleep) usleep($this->sleeptime);
 
        else sleep(1);
 
    }
 
    function PHPTelnet() {
 
        $this->conn1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
 
            chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
 
            chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
 
            chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
 
            chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
 
            chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
 
            chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
 
            chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
 
            chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
 
            chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
 
            chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
 
            chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
 
        $this->conn2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
 
            chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);
 
    }
 
    function ConnectError($num) {
 
        if ($this->show_connect_error) switch ($num) {
 
        case 1: echo '
[PHP Telnet] Connect failed: Unable to open network connection
'; break;
 
        case 2: echo '
[PHP Telnet] Connect failed: Unknown host
'; break;
 
        case 3: echo '
[PHP Telnet] Connect failed: Login failed
'; break;
 
        case 4: echo '
[PHP Telnet] Connect failed: Your server\'s PHP version is too low for PHP Telnet
'; break;
 
        }
 
    }
 
}
 
?>

到此,关于“怎么用php实现telnet”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. telnet ,openssh以及openssl
  2. 怎么用Python连接telnet到网络设备

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php telnet

上一篇:vscode中如何配置并使用.vue代码模板

下一篇:怎么用php去除数组中为0的元素

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》