[英]Telnet with php. Control remote APC PDU
我试图通过执行一些PHP命令来控制远程电源开关。
有一个telnet库,我用来建立telnet连接: http : //www.soucy.org/project/cisco/source.php
我的connect函数如下所示:
public function connect()
{
$this->_connection = fsockopen($this->_hostname, $this->_port, $errno, $errstr, $this->_timeout);
if ($this->_connection === false) {
die("Error: Connection Failed for $this->_hostname\n");
} // if
stream_set_timeout($this->_connection, $this->_timeout);
$this->_readTo(':');
if (substr($this->_data, -9) == 'Username:') {
$this->_send($this->_username);
$this->_readTo(':');
} // if
$this->_send($this->_password);
$this->_send(''); //blank space, because we need to press <Enter> for the second login prompt
//Login Second time
$this->_send($this->_username2);
$this->_send($this->_password2);
}
发送功能如下所示:
private function _send($command)
{
fputs($this->_connection, $command . "\r\n");
}
因此,如果我们想控制远程电源开关,就有一个菜单。 在此菜单中,用户可以像这样导航:
-------控制台----------------------------------------- --------------
1- Device Manager
2- Network
3- System
4- Logout
<ESC>- Main Menu, <ENTER>- Refresh
当我们按1时,我们将去另一个meniu:
- - - - 装置经理 - - - - - - - - - - - - - - - - - - - - - ---------------
1- Bank Monitor
2- Outlet Management
3- Power Supply Status
<ESC>- Back, <ENTER>- Refresh
等等......所以我们只需输入这些数字即可访问我们想要的插座。
重新加载插座的功能(插座号22):
public function ReloadOutlet22()
{
$this->_send('1'); // Access Device Manager
$this->_send('2'); // Access Outlet Management
$this->_send('1'); // Outlet Control/Configuration
$this->_send(); // '<Enter> to continue'
$this->_send('22'); // Access Outlet number 22
$this->_send('6'); // Delayed Reboot (reboot with 5 sec delay)
$this->_send('YES'); // 'Yes' to continue
$this->_send(); // <Enter> to continue'
//Everything is working till there. I can successfully reload the outlet which I want. After the reload I want to go to the main menu and logout from this console.
$this->_send('\e'); // <Esc> - back
$this->_send('\e'); // <Esc> - back
$this->_send('\e'); // <Esc> - back
$this->_send('\e'); // <Esc> - back
$this->_send('\e'); // <Esc> - back
$this->_send('4'); // Logout
}
所以有一个问题。 下次,当我想重新加载另一个插座时,例如带有编号23的插座,我无法成功登录到APC PDU。 我可以在登录提示中看到尝试使用'\\ e'作为用户名和密码。
所以也许你们有一个想法,为什么在成功重新加载后我的代码不能正常工作? 为什么我不能回到主菜单并注销?
感谢您的时间。
APC提供控制台(基于文本的菜单驱动),以及它们的命令行界面(“CLI”),它接受专用于控制PDU插座的各种命令行。
要使用命令行界面而不是控制台,请在使用telnet登录时将“-c”附加到您的密码 - 即,如果您的telnet密码是“abcdefg”,则使用密码“abcdefg -c”登录进入CLI。 CLI命令提示符为“APC>”。
用于重新启动(重启)插座的CLI命令就是:
APC> reboot x
(x = the outlet number to power cycle)
你可能需要在“\\ e”周围加上双引号 - 单引号将它视为文字字符串(没有转义序列,如\\ n)
$this->_send("\e"); // <Esc> - back
如果那不起作用
$this->_send(chr(27)); // <Esc> - back
使用数字ASCII值发送转义键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.