Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于在Cisco路由器上执行命令的php脚本_Php_Cisco - Fatal编程技术网

用于在Cisco路由器上执行命令的php脚本

用于在Cisco路由器上执行命令的php脚本,php,cisco,Php,Cisco,我需要写一个脚本来连接到Cisco路由器并执行命令。linux服务器和Cisco路由器使用ssh密钥,因此不需要用户名/密码。我已经知道了如何建立连接,但我不知道如何发出命令和读取响应 注意:它必须在php中与我们正在进行的其他东西集成 以下是我到目前为止的情况:- <?php $connection = ssh2_connect("n.n.n.n", 22, array("hostkey"=>"ssh-rsa")); if(!$connection) { die("Coul

我需要写一个脚本来连接到Cisco路由器并执行命令。linux服务器和Cisco路由器使用ssh密钥,因此不需要用户名/密码。我已经知道了如何建立连接,但我不知道如何发出命令和读取响应

注意:它必须在php中与我们正在进行的其他东西集成

以下是我到目前为止的情况:-

<?php

$connection = ssh2_connect("n.n.n.n", 22, array("hostkey"=>"ssh-rsa"));

if(!$connection)
{
  die("Could not connect to the Router \n\r");
  exit();
}

if (ssh2_auth_none ($connection, "username"))
{
  echo("Public Key Authentication Successful\n\r");
}
else
{
  die("Public Key Authentication Failed");
  exit();
}


// New commands based on help received
try
{
  $command = "dir";
  $stream = ssh2_exec($connection, $command);

  // added to test if the ssh2_exec command returns false - which it does, issue is with the command ???
  if(!$stream)
  {
    echo("Command returned False\n\r");
    exit();
  }

  $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
  stream_set_blocking($errorStream, true);
  stream_set_blocking($stream, true);
  $errorStreamContent = stream_get_contents($errorStream);
  $streamContent = stream_get_contents($stream);
  echo("ErrorStream : " . $errorStreamContent . "\n" . "Stream : " . 
  $streamContent . "\n\r");
}

catch(exception $e)
{
  echo("Error : " . $e);
  exit();
}

?>

有人能告诉我发出命令和读取结果的正确方法吗?谢谢。

我通过使用ssh2\u auth\u pubkey\u文件将ssh密钥传递给路由器并进行身份验证,成功地解决了这个问题。在这之后,它工作正常。

试试shell_exec Hi@paskl,它只是向linux操作系统而不是路由器发出命令。哎呀,你说得对。怎么样
ssh2_exec
?;)@paskl-I发出了以下命令
$result=ssh2\u exec($connection,$command)我这次没有收到错误,但是$result是空的。$命令是一个
dir
,因此我希望看到一个文件列表(如果我在路由器上手动发出dir,我会看到这个列表)。检查文档以了解如何读取流。php.net上的函数文档页面上的注释非常有用。:)(在这里重复没有意义)
Public Key Authentication Successful
Command returned False