- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 php 远程登录到 cisco 交换机并执行 show interface status
命令并获得结果。我尝试了一些在互联网上找到的 php 类,但它们都无法连接到设备。所以我尝试自己编写脚本,但我有同样的问题,我无法连接到设备。
主持人向我发送横幅消息,然后用 username:
换行.
我用 \r\n
发送我的用户名,等待一段时间并尝试读取数据,但在我看来主机只是忽略了我的换行符。这是我得到的回复(explode('\n') on response):
Array
(
[0] => %
[1] => User Access Verification
[2] => Username: timeout expired!
)
<?
$host = "switchName";
$name = "name";
$pass = "pass";
$port = 23;
$timeOut = 15;
$connected = false;
$skipNullLines = true;
$timeout = 125000;
$header1=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(0x41).chr(0x4E).chr(0x53).chr(0x49).chr(0xFF).chr(0xF0);
$header2=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 read_string()
{
global $fw,$host,$skipNullLines;
$string = "";
while( !feof($fw) )
{
$read = fgets($fw);
$string .= $read;
// Probably prompt, stop reading
if( strpos($read, ':') !== FALSE || strpos($read, '> (enable)') !== FALSE || strpos($read, $host.'#') !== FALSE)
{ break; }
}
$string = explode("\n", $string);
// Get rid of null lines
$ret = array();
for($i = 0; $i<count($string); $i++)
{
if( trim($string[$i]) == '' && $skipNullLines ) continue;
$ret[] = $string[$i];
}
return $ret;
}
function send_string($string, $force=false)
{
GLOBAL $timeout,$fw;
$string = trim($string);
// execute only strings that are preceded by "show" (if not forced)
if(!$force && strpos($string, 'show ') !== 0)
{
return 1;
}
fputs($fw, $string."\r\n");
echo("SEND:".$string."\r\n");
usleep($timeout);
}
$fw = fsockopen($host, $port, $errno, $errorstr, $timeOut);
if($fw == false)
{
echo("Cant connect");
}
else
{
echo("Connected<br>");
$connected = true;
stream_set_timeout($fw, $timeout);
// fputs($fw, $header1);
// usleep($timeout);
// fputs($fw, $header2);
// usleep($timeout);
print_r(read_string());
send_string("test", true);
print_r(read_string());
}
fclose($fw);
?>
...
$connected = true;
stream_set_timeout($fw, $timeout);
send_string("name", true);
send_string("password", true);
print_r(read_string());
...
最佳答案
好的,所以我不知道问题出在哪里,但是经过“几次”测试后,我能够编写适合我的类(class)。我不知道为什么其他 telnet 类不工作,尽管它们几乎相同。所以如果有人会有类似的问题,你可以试试这个:
class TELNET
{
private $host;
private $name;
private $pass;
private $port;
private $connected;
private $connect_timeout;
private $stream_timetout;
private $socket;
public function TELNET()
{
$this->port = 23;
$this->connected = false; // connected?
$this->connect_timeout = 10; // timeout while asking for connection
$this->stream_timeout = 380000; // timeout between I/O operations
}
public function __destruct()
{
if($this->connected) { fclose($this->socket); }
}
// Connects to host
// @$_host - addres (or hostname) of host
// @$_user - name of user to log in as
// $@_pass - password of user
//
// Return: TRUE on success, other way function will return error string got by fsockopen()
public function Connect($_host, $_user, $_pass)
{
// If connected successfully
if( ($this->socket = @fsockopen($_host, $this->port, $errno, $errorstr, $this->connect_timeout)) !== FALSE )
{
$this->host = $_host;
$this->user = $_user;
$this->pass = $_pass;
$this->connected = true;
stream_set_timeout($this->socket, 0, 380000);
stream_set_blocking($this->socket, 1);
return true;
}
// else if coulnt connect
else return $errorstr;
}
// LogIn to host
//
// RETURN: will return true on success, other way returns false
public function LogIn()
{
if(!$this->connected) return false;
// Send name and password
$this->SendString($this->user, true);
$this->SendString($this->pass, true);
// read answer
$data = $this->ReadTo(array('#'));
// did we get the prompt from host?
if( strtolower(trim($data[count($data)-1])) == strtolower($this->host).'#' ) return true;
else return false;
}
// Function will execute command on host and returns output
//
// @$_command - command to be executed, only commands beginning with "show " can be executed, you can change this by adding
// "true" (bool type) as the second argument for function SendString($command) inside this function (3rd line)
//
function GetOutputOf($_command)
{
if(!$this->connected) return false;
$this->SendString($_command);
$output = array();
$work = true;
//
// Read whole output
//
// read_to( array( STRINGS ) ), STRINGS are meant as possible endings of outputs
while( $work && $data = $this->ReadTo( array("--More--","#") ) )
{
// CHeck wheter we actually did read any data
$null_data = true;
foreach($data as $line)
{
if(trim($line) != "") {$null_data = false;break;}
}
if($null_data) { break;}
// if device is paging output, send space to get rest
if( trim($data[count($data)-1]) == '--More--')
{
// delete line with prompt (or "--More--")
unset($data[count($data)-1]);
// if second line is blank, delete it
if( trim($data[1]) == '' ) unset($data[1]);
// If first line contains send command, delete it
if( strpos($data[0], $_command)!==FALSE ) unset($data[0]);
// send space
fputs($this->socket, " ");
}
// ak ma vystup max dva riadky
// alebo sme uz nacitali prompt
// IF we got prompt (line ending with #)
// OR string that we've read has only one line
// THEN we reached end of data and stop reading
if( strpos($data[count($data)-1], '#')!==FALSE /* || (count($data) == 1 && $data[0] == "")*/ )
{
// delete line with prompt
unset($data[count($data)-1]);
// if second line is blank, delete it
if( trim($data[1]) == '' ) unset($data[1]);
// If first line contains send command, delete it
if( strpos($data[0], $_command)!==FALSE ) unset($data[0]);
// stop while cyclus
$work = false;
}
// get rid of empty lines at the end
for($i = count($data)-1; $i>0; $i--)
{
if(trim($data[$i]) == "") unset($data[$i]);
else break;
}
// add new data to $output
foreach($data as $v)
{ $output[] = $v; }
}
// return output
return $output;
}
// Read from host until occurence of any index from $array_of_stops
// @array_of_stops - array that contains strings of texts that may be at the end of output
// RETURNS: output of command as array of lines
function ReadTo($array_of_stops)
{
$ret = array();
$max_empty_lines = 3;
$count_empty_lines = 0;
while( !feof($this->socket) )
{
$read = fgets($this->socket);
$ret[] = $read;
//
// Stop reading after (int)"$max_empty_lines" empty lines
//
if(trim($read) == "")
{
if($count_empty_lines++ > $max_empty_lines) break;
}
else $count_empty_lines = 0;
//
// Does last line of readed data contain any of "Stop" strings ??
$found = false;
foreach($array_of_stops AS $stop)
{
if( strpos($read, $stop) !== FALSE ) { $found = true; break; }
}
// If so, stop reading
if($found) break;
}
return $ret;
}
// Send string to host
// If force is set to false (default), function sends to host only strings that begins with "show "
//
// @$string - command to be executed
// @$force - force command? Execute if not preceeded by "show " ?
// @$newLine - append character of new line at the end of command?
function SendString($string, $force=false, $newLine=true)
{
$t1 = microtime(true);
$string = trim($string);
// execute only strings that are preceded by "show"
// and execute only one command (no new line characters) !
if(!$force && strpos($string, 'show ') !== 0 && count(explode("\n", $string)) == 1)
{
return 1;
}
if($newLine) $string .= "\n";
fputs($this->socket, $string);
$t2 = microtime(true);
}
}
// EXAMPLE
$host = "hostname";
$name = "username";
$pass = "password";
$t = new TELNET();
echo("CONNECT:".$t->Connect($host, $name, $pass)."<br>");
echo("LOGIN:".(int)$t->LogIn());
echo("<br>OUTPUT:<br>");
print_r($t->GetOutputOf("show snmp"));
print_r($t->GetOutputOf("show users"));
print_r($t->GetOutputOf("show interface status"));
关于php - 使用 php Telnet 到 cisco 交换机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10650037/
所以我可以在 WSL2 中使用 X11 转发,例如 How to set up working X11 forwarding on WSL2 .然后我在连接到 Cisco AnyConnect 服务器
我最近进入了固件编程领域,我需要您的专家建议来保持我在该领域的初步发展。 我想修改CISCO E2500路由器的固件以增强IPv4和IPv6的路由,其中包括在此过程中更改路由表。 我决定使用 dd
Cisco IOS设置口令—启用口令 可以在全局配置模式下设置启用口令: Router(config)#enable ? last-res
我正在尝试从 Cisco ACI 获取路由表。我收到重复的路线。有办法解决它们吗? 例子: "10.127.32.70/32", "10.127.32.70/32",
我在 Cisco CVP 中有一个外部项目,我已将该项目导入到 Cisco CVP IDE 中。这个项目有一些自定义元素,我也有它们的相关类。我已将它们的相关类放在 cisco\cvp\plugin\
我只想获取打入我们调用中心的所有调用的 3 位信息:调用者、他们调用的号码以及实际接听电话的人的号码(不同于外界调用的号码)。 直到最近,我们通过监听 JTAPI 日志文件更改(使用 Microsof
我有一个文本思科配置。我应该匹配的主机名行是“125-hostname billdevice”。我正在使用下面的模式,但不匹配 true。 Pattern ciscohostname = Patter
只有第一个命令的输出才会写入文件。 如何让它将所有命令的输出写入文件? --- - name: run show commands hosts: nexus1 gather_facts: Fa
我在尝试解析路由器的 mrib 表时遇到了一些问题。我已经能够解析其中的一些但有问题。例如我有以下输出: (192.168.1.1,232.0.6.8) RPF nbr: 55.44.23.1 Fla
我无法让 CharlesProxy 在 Android 或 iOS 设备上运行。笔记本电脑和移动设备位于 Anyconnect VPN 之后。这里的任何指示都会有所帮助。谢谢。 最佳答案 我在 Cha
我有一个 C++ 程序,它使用 Microsoft TAPI 接口(interface)从 Cisco Call Manager PBX 收集调用事件。为了获得 Cisco 事件,我从调用管理器下载了
在我的网页上,我需要检查给定用户是否可以在 Cisco Jabber 客户端上使用。我有号码和用户名。我已经能够发送聊天消息并开始语音对话,但最好检查用户是否在线,例如显示很酷的图标或其他东西。有没有
我已成功使用其他几个 Webex API,但“LstrecordaccessDetailHistory XML 请求”不起作用。 我从 XML 响应中收到此消息: unable to instanti
我目前正在尝试让 RHEL 5 机器通过 radius 访问 cisco acs。我安装了 freeradius 包并使用 radtest 命令尝试访问 radius 服务器。当我进入 cisco r
我希望你能帮助我,目标是使用 pysnmp 获取已连接到 ap 的客户端数量,我想我很接近,我知道我可能必须使用 pyasn1,但我得到了一个给我以下错误的部分: ('-------->', Disp
我正在尝试通过 SNMP 从 ucs-6100 获得答案。 通过控制台:(模拟我的请求) >>> snmpget -v3 -l authPriv -u usr-sha-aes -A authkey1
我在 Ubuntu 20.04 上遇到 Cisco AnyConnect 问题。 当我启动 docker 容器(使用非主机网络驱动程序)时,Cisco AnyConnect 立即重新连接。 启动 do
我正在使用基本的 Cisco VPN 客户端(我相信是 v.5)。无论如何以编程方式确定特定配置文件(或与此相关的任何配置文件)是否已连接? 我希望以某种方式从客户端本身获得状态。我不想尝试 ping
我需要从我的 .NET 应用程序配置和查询 Cisco 路由器,我需要通过 Cisco WSMA 来完成网络服务接口(interface)。 这些服务通过 HTTP(在我的例子中)公开,并使用 SOA
我正在寻找命令行参数,以使用 CISCO H264 将一系列图像转换为 H264。 (类似于 FFMPEG 图像到视频功能)。不幸的是,我不能使用 FFMPEG,因为我正在创建一个商业应用程序,我在其
我是一名优秀的程序员,十分优秀!