⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phptelnet.txt

📁 php的TELNET程序 针对HUAWEI的交换机的一个TELNET程序,可以用以对开发BS模式的控制程序
💻 TXT
字号:
我用php编写了一个从华为路由器中读取配置信息的php程序。问题说明如下: 
华为路由器登陆方式为:telnet, 注意这个telnet有一点特点: 
........... 
UserName: 
Password: 
[router-name] 
如果提示UserName:就用php完成填写用户名, 提示Password: 就输入口令。 
如果出现提示[......]就表示登陆成功,否则会继续提示输入UserName, 登陆完成以后,输入命令 disp cur 显示配置信息,注意,输出的配置文件如果超过一屏,则提示------------(More) Ctrl-C break -----------,输入空格继续显示,然后用logout退出登陆。 
php脚本如下: 
#!/usr/local/bin/php 
<?PHP 
class nHUAWEI { 
var $hostname; 
var $config; 

var $debug=true; // or true to display all message 
var $host=false; 
var $port; 
var $username; 
var $password; 
var $timeout; 
var $error; 

var $ControlChar=0x1b; 

var $conn; 
var $loginok=false; 

function nHUAWEI($host,$port=23,$username,$password,$timeout=30) { 

$this->host=$host; 
$this->port=$port; 
$this->username=$username; 
$this->password=$password; 
$this->timeout=$timeout; 
$this->config=null; 
$this->hostname=null; // sysname ????? 
$this->error=array("error" => "", "errno" => 0, "errstr" => ""); 

$this->connect(); if($this->error["errno"]!=0) return ; 
$this->login(); if($this->error["errno"]!=0) return ; 
$this->getconfigure();if($this->error["errno"]!=0) return ; 
$this->close(); if($this->error["errno"]!=0) return ; 
} 


function connect() { // 连接路由器 
$this->conn = fsockopen($this->host,$this->port,&$errno,&$errstr,$this->timeout); 
if ($this->debug) echo $errstr; 
if ( empty($this->conn) || $this->conn==false) { 
$this->error=array("error" => "Error 1: 连接路由器不通。", 
"errno" => -1,"errstr" => $errstr); 
return false; 
} 
if ($this->debug) echo "==Connect Ok...\n"; 
return true; 
} 


function login() { // 登陆路由器 
while ( $c=fgetc($this->conn) ) { 
if ($this->debug) echo $c; 
if ($c==':') { break; } 
if ($c==']') { 
if ($this->debug) echo "==This router not need username and password , by telnet login...\n"; 
return true; 
} 

} // wait to "Username:" prompt 
if ($this->debug) echo "==put [$this->username]\n"; 
fputs($this->conn,"$this->username\r\n"); 
while ( $c=fgetc($this->conn) ) { 
if ($this->debug) echo $c; 
if ($c==':') break; 
} // wait to "Password:" prompt 
if ($this->debug) echo "==put [$this->password]\n"; 
fputs($this->conn,"$this->password\r\n"); 
while ( $c=fgetc($this->conn)) { 
if ($this->debug) echo $c; 

if ($c==']') break; // 正常登陆 

if ($c==':') { // prompt: [User logged Fail!] and display [Username:] again. 
$this->error=array("error" => "Error 2: 无效的用户名或密码。", 
"errno" => -2,"errstr" => "Error 2: bad user name or password!"); 
return false; 
} 

} // wait for "command prompt [hostname]" 
if ($this->debug) echo "==user login Ok...\n"; 
} // end of function login() 

function getconfigure() { 
$ESC=sprintf("%c",0x1b); 
$SPACE=sprintf("%c",0x20); 
$data=''; 
if ($this->debug) echo "==put [display current-configuration]\n"; 
fputs($this->conn,"disp cur\r\n"); 

while ( $c=fgetc($this->conn)) { 
if ( $c==$ESC) { // ESC [ 7 C ==begin 
$c1=fgetc($this->conn); 
$c2=fgetc($this->conn); 
$c3=fgetc($this->conn); 
// echo "[$c1$c2$c3]\n"; 
break; 
} 
} 

while ( $c=fgetc($this->conn)) { 
$a=unpack("c",$c); 
echo $a; 
if ( $c==$ESC) { 
echo "^"; 
} 

if ($c==']') { echo "&&&&&" ;break; } 
$data .= $c; 
} 
$this->config=$data; 
} 

function close() { 
if ($this->conn) { 
fputs($this->conn,"logout\r\n"); 
fclose($this->conn); 
} 
return true; 
} 


} // end of class new huawei router 




// ============================================================================== 

error_reporting (E_ERROR | E_PARSE ); //去掉《警告》提示 
set_time_limit (5); /* Allow the script to hang around waiting for connections. */ 
ob_implicit_flush ();/* Turn on implicit output flushing so we see what we're getting as it comes in */ 

// 读输入文件, 格式: 
// Router_Type , ipaddress, pass1(or username), pass2(or huawei password), comment 
// 测试取 Cisco 


$r=new nHUAWEI('192.168.0.99',23,'pzls','1234',10); 
// echo $r->error["errno"]; 
switch ($r->error["errno"] ) { 
case 0: echo " ---- \n"; break; 
case -1: echo " E--- \n"; break; 
case -2: echo " -E-- \n"; break; 
case -3: echo " --E- \n"; break; 
default: echo " EEEE \n"; break; 
} 
if ($r->debug) if ($r->error["errno"]!=0) echo $r->error["error"]; 
if ( $r->config && $r->error["errno"]==0 ) { 
$filename=$r->nhost."a.cis"; 
$fo=fopen($filename,"w"); 
if (!$fo) exit; 
fwrite($fo,$r->config); 
fclose($fo); 
} 
?> 

问题是: 现在登陆可以完成,用disp cur 命令只能取到路由器输出的前3行,然后就退出了,不知道为什么? 能不能帮我查找一下错误! 另外我发现通过fsockopen打开的流,由于路由器最后提示符后面没有“crlf”回车换行提示,所以fgets(...)使用有问题,故我的程序用的是fgetc。 
=========帮我看看错误, 我急需要!

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -