Search code examples
phptelnetapc

Telnet with php. Control remote APC PDU


I'm trying to control remote power switch by executing some php commands.

There is a telnet library which I'm using to make a telnet connection: http://www.soucy.org/project/cisco/source.php

My connect function looks like this:

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);
}

Send function looks like this:

private function _send($command) 
{
    fputs($this->_connection, $command . "\r\n");
} 

So if we want to control remote power switch, there is a menu for this. In this menu user can navigate like this:

------- Control Console -------------------------------------------------------

 1- Device Manager
 2- Network
 3- System
 4- Logout

 <ESC>- Main Menu, <ENTER>- Refresh

When we press 1, we will go to another meniu:

------- Device Manager --------------------------------------------------------

 1- Bank Monitor
 2- Outlet Management
 3- Power Supply Status

 <ESC>- Back, <ENTER>- Refresh

and so on... So we can access the outlet which we want by just typing these numbers.

Function to reload the outlet (outlet number 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
}

So there is a problem. Next time , when I want to reload another outlet, for example outlet with a number 23, I can't successfully login to the APC PDU. I can see at the login prompt that there was a try to use '\e' as a Username and as a password.

So maybe guys you have an idea, why after successful reload my code doesn't work as it should? Why I can't go back to the main menu and logout?

Thank you for your time.


Solution

  • APC provides both the Control Console (text-based menu driven), and also their Command Line Interface ("CLI") which accepts a variety of command lines specific to controlling the PDU's outlets.

    To use the Command Line Interface instead of the Control Console, append " -c" to your password when logging in with telnet--i.e., if your telnet password is "abcdefg", then login with password "abcdefg -c" to enter the CLI. The CLI command prompt is "APC>".

    The CLI command to power cycle (reboot) an outlet is simply :

    APC> reboot x
    (x = the outlet number to power cycle)