lifo/remote-control

网络设备远程控制

v1.0 2014-05-06 18:02 UTC

This package is auto-updated.

Last update: 2024-09-14 02:43:11 UTC


README

"Remote Control" 是一个 PHP 类库,允许您通过其 CLI 接口(通常通过 SSH 或 Telnet)或通过 STDINSTDOUT 使用 Expect 以简单易用的面向对象方式来程序化地控制远程设备。

我基于此处 GitHub 上的自己的 Cisco Automation Perl 库设计了此类。然而,我已尽力使此库尽可能通用,以与任何进程或设备一起使用。

开发注意事项

PHP Expect 库非常有限,甚至无法与在 CPAN 上可用的 Perl Expect 模块相提并论。截至本文撰写时,最新版本的 php-expect 是 v0.3.1(更新了近两年!)两个主要限制是 php-expect 处理捕获输出和模式匹配的方式。

  • php-expect 支持一个 INI 设置 expect.logfile 来捕获输出,但由于缓冲,在需要实时读取/写入进程的实时程序中,其用途有限。为了允许实时捕获输出,我必须在运行时不断设置和重置 expect.logfile INI 设置。这导致文件刷新,然后类智能地知道从文件中读取的位置,以确定先前命令的输出。到目前为止,我没有注意到任何性能问题。
  • php-expect 声称支持 REGEXP 风格的模式,但并未明确说明这究竟是 PREG 模式还是其他。在我的测试中,某些模式会导致 Segfault 或简单地无法按预期工作。最值得注意的是,您不能使用修饰符(多行:m),因此限制提示匹配等会稍微困难一些。

项目设置

依赖

  1. PHP 5.3+
  2. Expect PECL 扩展

安装

Composer 是下载和维护您库副本的推荐方式(使用 packagist.org)。直接使用 Github 也是一个合理的选项,但您将必须手动创建类自动加载器或自行包含它们(很丑!)

Composer 安装

  1. 将 "lifo/remote-control" 添加到您的项目 composer.json 文件

    {
        "require": {
            "lifo/remote-control": "dev-master"
        }
    }
  2. 运行 composer update: php composer.phar update lifo/remote-control

GitHub 安装

  1. 克隆 remote-control 仓库

    git clone git://github.com/lifo101/remote-control.git

  2. 根据需要将 Lifo/RemoteControl 添加到您的项目自动加载器。

示例

有关更多可运行的示例,请参阅 示例 目录。

use Lifo\RemoteControl\RemoteControl;
use Lifo\RemoteControl\Type\NetworkDevice;

// Raw control object for low level access (see 2nd example for an easier approach)
$rc = new RemoteControl("ssh username@hostname", array(
    'auto_start' => true,
    'log_stdout' => true,
    // more options available
));

// generic prompt for "cisco" devices; your milage may vary.
$prompt = '[a-zA-Z0-9._-]+ ?(\(config[^\)]*\))? ?[$#>] ?(\(enable\))? *$';

// wait for output from the process and act upon various patterns.
// Each pattern can be a closure or simple variable. If any closure
// returns true or RemoteControl::WAIT_DONE then the wait loop will end.
$res = $rc->wait(array(
    //    REGEX/GLOB/STR => Closure or mixed value
    array($prompt        => true ),
    array('yes/no\)\?'   => function($rc){ $rc->writeln('yes'); } ),
    array('[Pp]assword:' => function($rc){ $rc->writeln('password'); } ),
));

// attempt another command and wait for prompt
$rc->writeln("show clock");
$rc->wait($prompt);

// ----------------------------------------------------------
// OR ... (this is preferred over the low-level method above)
// ----------------------------------------------------------

// High level control object for easier access of network type devices
$d = new NetworkDevice(array(
    'protocol' => 'ssh',
    'host'     => 'hostname',
    'username' => 'username',
    'password' => 'password',
    'enable'   => 'enablepw',
    // more options ...
    //'remote_control_options' => array(
    //    'log_stdout' => true,
    //)
));
$d->verbose(true); // for debugging/troubleshooting only; same as setting log_stdout = true

// login method handles a lot of variations for different devices and 
// will enable if possible. You can call $d->enable() manually too.
if (!$d->login()) {
    die("Error logging in!\n");
}

// basic method to send a command and wait for a prompt. Returns 
// any output received after the commmand.
echo $d->send("show version");