dtjbcda841554 2019-03-04 09:35 采纳率: 0%
浏览 83

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

I need to write a script to connect to a Cisco router and execute commands. The linux server and the Cisco router use ssh keys so no username/password are required. I have worked out how to make the connection, but I don't know how to issue the commands and read the responses.

Note: It has to be in php to integrate with some other stuff we have going on.

Here is what I have so far :-

<?php

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

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

if (ssh2_auth_none ($connection, "username"))
{
  echo("Public Key Authentication Successful
");
}
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
");
    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 . "
" . "Stream : " . 
  $streamContent . "
");
}

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

?>

Output is now as follows :-

Public Key Authentication Successful
Command returned False

Can anyone give me the correct way to issue commands and read any result ? Thanks.

  • 写回答

1条回答 默认 最新

  • douhui3330 2019-05-16 12:29
    关注

    I managed to fix this by using ssh2_auth_pubkey_file to pass the ssh keys to the router and authenticate. After that it works fine.

    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计