?? phptelnet.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 + -