phpseclib - 尝试连接到HP procurve开关返回错误:不支持SSH命令执行

时间:2012-08-24 19:05:32

标签: php ssh phpseclib

我正在尝试使用phpseclib的NET_SSH2库连接到HP交换机。 只是为了测试/开始,我正在尝试登录,然后在交换机上运行'show interfaces brief'命令。 但在它登录后,我收到一条错误消息:

 SSH command execution is not supported. 

这是代码:

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../phpseclib');
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', true); //turn on logging.

$ssh = new Net_SSH2('10.10.10.10'); //starting the ssh connection to localhost
if (!$ssh->login('', 'password')) { //if you can't log on...
  exit('Login Failed');
}
else  {
echo 'logged in<br>';
}
echo 'Attempting command: <br>';
$output = $ssh->exec('show interfaces brief');    
echo $output.'<br>';
echo 'Error message is: <br>';
$log = $ssh->getLog(NET_SSH2_LOG_COMPLEX);
foreach ($log as $logitem)  {
echo $logitem.'<br>';
}
?>

返回的输出是:

 logged in
 Attempting command:

 Notice: Connection closed prematurely in /var/www/phpseclib/Net/SSH2.php on line 1941
 SSH command execution is not supported.
 Error message is:
 <-
 ->
 <- NET_SSH2_MSG_KEXINIT (0.0015s)
 -> NET_SSH2_MSG_KEXINIT (0s)
 -> NET_SSH2_MSG_KEXDH_INIT (0s)
 <- NET_SSH2_MSG_KEXDH_REPLY (0.5123s)
 -> NET_SSH2_MSG_NEWKEYS (0s)
 <- NET_SSH2_MSG_NEWKEYS (0s)
 -> NET_SSH2_MSG_SERVICE_REQUEST (0s)
 <- NET_SSH2_MSG_SERVICE_ACCEPT (0.1962s)
 -> NET_SSH2_MSG_USERAUTH_REQUEST (0.0001s)
 <- NET_SSH2_MSG_USERAUTH_BANNER (0.0014s)
 <- NET_SSH2_MSG_USERAUTH_SUCCESS (0.0392s)
 -> NET_SSH2_MSG_CHANNEL_OPEN (0s)
 <- NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION (0.0204s)
 -> NET_SSH2_MSG_CHANNEL_REQUEST (0s)
 <- NET_SSH2_MSG_CHANNEL_SUCCESS (0.1011s)
 <- NET_SSH2_MSG_CHANNEL_DATA (0s)
 -> NET_SSH2_MSG_CHANNEL_DATA (0s)
 <- NET_SSH2_MSG_CHANNEL_EOF (0s)
 <- NET_SSH2_MSG_CHANNEL_REQUEST (0s)
 <- NET_SSH2_MSG_CHANNEL_CLOSE (0s)

 Notice: Connection closed prematurely in /var/www/phpseclib/Net/SSH2.php on line 1941

ssh2.php中的第1941行是您在下面看到的“user_error”行:

 function _send_binary_packet($data)
{
    if (feof($this->fsock)) {
        user_error('Connection closed prematurely', E_USER_NOTICE);
        return false;
    }

到目前为止我做了什么:

  1. 我已经通过ssh手动登录并确保我可以运行相同的命令。
  2. 我已经浏览了交换机的网页配置页面,以确保没有其他任何我需要为ssh打开等等。
  3. 我一直在检查phpseclib的论坛是否有类似的问题。
  4. 我正在使用phpseclib的1.53版2010/10/24 01:24:30。

    任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

您无法在HP Procurve Switch上使用exec命令。您必须模拟交互式shell(不幸的是)。

这是我为了基本上拥有一个批处理控制台以便一次配置多个交换机而做的事情。我将一个IP地址列表放在一个名为switches.txt的文件中,用新行分隔每个地址(确保在文件的末尾留下一个新行)。它非常混乱,我只使用过一次并没有多想,但它确实节省了我很多时间,而不是手动登录超过一百个开关。我不能等到获得Procurve经理......

另外,我没有花时间正确实现和STDOUT读取,所以你看不到给开关的任何输出,但我相信它不会那么困难。

<?php

require ('Net/SSH2.php');
$cnt = 0;
$ssh = array();
$ips = array();
echo "\n";

$handle = fopen('switches.txt', 'r');
while (!feof($handle)) {
    $ip = trim(fgets($handle)); 
    $ips[$cnt] = $ip;

    //SSH Setup

    $ssh[$cnt] = new Net_SSH2($ip);
    echo "Logging into device: ".$ip."\n";
    if (!$ssh[$cnt]->login('USERNAMEHERE', 'PASSWORDHERE')) {
        exit ('Login Failed');
    }
    $cnt++;

}
fclose($handle);

//Initial Post Login Setup
sleep(1);
for ($i=0; $i<sizeof($ssh); $i++) {
echo "Performing Post Login Setup (1/2) on device: ".$ips[$i]."\n";
$ssh[$i]->write("\n");
}
sleep(1);
for ($i=0; $i<sizeof($ssh); $i++) {
echo "Performing Post Login Setup (2/2) on device: ".$ips[$i]."\n";
$ssh[$i]->write("conf\n");
}
sleep(1);


//Command Loop
while (true) {
    //Device Loop
    echo "\nBatch Input# ";
    $in = fopen('php://stdin', 'r');
    $buffer = fgets($in);
    for ($i=0; $i<sizeof($ssh); $i++) {
        $ssh[$i]->write($buffer);
        //echo "Wrinting command: $buffer  ;  To Device: ".$ips[$i].";\n";
    }   
}
fclose($handle);
?>