 |
รบกวนช่วยดู code php ที่ telnet ไปยัง switch มีปัญหาด้านคำสั่งใน switch |
|
 |
|
|
 |
 |
|
Code (PHP)
/////////////////////////////////////อันนี้คือหน้าคำสั่ง telnet.php///////////////////////////////////////
<?php
require_once "PHPTelnet.php";
$telnet = new PHPTelnet();
// if the first argument to Connect is blank,
// PHPTelnet will connect to the local host via 127.0.0.1
$result = $telnet->Connect('เลขไอพี',ยูสเซอร์,'พาสเวิด');
switch ($result) {
//////////////////////////////////////มีปัญหาตรงนี้////////////////////////////////
case 0:
$telnet->DoCommand('cc sw 1', $result);
// NOTE: $result may contain newlines
echo $result;
$result->DoCommand('TEMPerature', $result);
echo $result;
// say Disconnect(0); to break the connection without explicitly logging out
$telnet->Disconnect();
break;
//////////////////////////////////////////////////////////////////////////////////////////
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 PHP version does not support PHP Telnet';
break;
}
?>
///////////////////////////////////////อันนี้คือหน้าฟังก์ชั่น PHPtelnet.php///////////////////////////////////////////
Code (PHP)
<?php
/*
PHPTelnet 1.0
by Antone Roundy
adapted from code found on the PHP website
public domain
*/
class PHPTelnet {
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;
/*
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]) 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,5555)) {
fputs($this->fp,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));
if ($this->use_usleep) usleep($this->sleeptime);
else sleep(1);
fputs($this->fp,chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21));
if ($this->use_usleep) usleep($this->sleeptime);
else sleep(1);
$this->GetResponse($r);
$r=explode("\n",$r);
$this->loginprompt=$r[count($r)-1];
fputs($this->fp,"$user\r");
if ($this->use_usleep) usleep($this->sleeptime);
else sleep(1);
fputs($this->fp,"$pass\r");
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;
}
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\r");
if ($this->use_usleep) usleep($this->sleeptime);
else sleep(1);
$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,1000);
$s=socket_get_status($this->fp);
} while ($s['unread_bytes']);
}
}
?>
Tag : PHP, MySQL, CakePHP
|
ประวัติการแก้ไข 2011-04-22 11:57:01
|
 |
 |
 |
 |
Date :
2011-04-22 10:01:45 |
By :
hexzawanekz |
View :
3155 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ภาระงานนี้ก็คือ telnet ไปยัง switch โดยใส่ ip user และ password
ในเบื้องต้นสามารถ telnet ไปยัง switchได้ โดยใช้ port 5555 ซึ่งเป็น port management switch
1.คำสั่ง command ที่ใช้กับ switch ตัวนี้คือ cc sw 1 แล้วกด enter เพื่อ connect switch ตัวที่ 1
2.คำสั่ง temp แล้วกด enter เพื่อโชว์ อุณหภูมิของ switch แล้วแสดงผลทางหน้าจอ
ปัญหาที่เกิดขึ้นก็คือ
การแสดงผลทางหน้าจอ ออกเป็น >>>>> cc sw 1 SH1: <<<<<<<
cc sw 1 (อันนี้เป็นคำสั่ง ทำไมมาขึ้นที่ output)
SH1: (อันนี้คือการแสดงผลว่า connect switch ตัวที่ 1แล้ว)
แต่สิ่งที่ไม่ขึ้นก็คือ temp โดยปกติ หากใช้ telnet ด้วย cmd มันจะoutput เป็น SH1 : tempurature xxxxx
แต่หากใช้ code php มองจาก browser มันดันไม่แสดง output tempurature ออกมา
รบกวนช่วยดู code ที่ telnet.php หน่อยครับ
|
ประวัติการแก้ไข 2011-04-22 10:14:54
 |
 |
 |
 |
Date :
2011-04-22 10:08:39 |
By :
hexzawanekz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เป็นเหมือนกันครับ นอผู้รู้มาตอบหน่อยคราฟ อยากให้แสดง outout แต่มันไม่แสดง เหมือนมันโดนจำกัดคำออกมาเลย
|
 |
 |
 |
 |
Date :
2015-02-24 14:33:03 |
By :
นิรนาม |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพิ่ม sleep ขั้นมันครับ ผมยังรันผลไม่เสร็จแล้วเก็บข้อมูลออกมาครับ ผมใช้วิธีนี้นะ
|
 |
 |
 |
 |
Date :
2015-08-06 13:51:34 |
By :
www.facebook.com/chatchai.h2o |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$telnet->DoCommand("display service-port port 0/2/16", $output);
$telnet->DoCommand(' ', $output);
usleep(500000);
$telnet->DoCommand(' ', $output);
usleep(500000);
if(strlen($output)<100){
$telnet->DoCommand(' ', $output);
if(strlen($output)<100){
$telnet->DoCommand(' ', $output);
usleep(500000);
if(strlen($output)<100){
$telnet->DoCommand(' ', $output);
usleep(500000);
}
}
}
ผมใช้มุคนี้ครับ ลองดูนะครับ
|
 |
 |
 |
 |
Date :
2015-08-06 14:10:11 |
By :
ฉัตรชัย สิทธิวงค์ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|