dongwei5740 2017-02-16 08:40
浏览 207

PHP SSH多个命令

I am trying to build a ssh script to run multiple commands on a host. My goal is get the output of each command. My target host is a cisco router and for the following script to execute more that one command i need to run it for each command i want to execute which is not a very elegant solution.

$cmd = array ('sh run int te 1/1', 'sh run int te 1/2');

for ($i = 0; $i <= 1; $i++) {
    $connection = ssh2_connect('10.1.1.1', 22);
    ssh2_auth_password($connection, 'user', 'pass');
    $stream = ssh2_exec($connection, $cmd[$i]);
    stream_set_blocking($stream, true);
    $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
    echo stream_get_contents($stream_out); }

I have created a loop because i was unable to get the output for each command in the same stream. Since i guess that the php is terminating the ssh connections at the end of each stream.

What i would like to achieve is to execute several commands and get the output in the same stream (if posible).

I am editing the post to answer the sugestions made by @Melvin Koopmans and @LSerni.

if i change the code as sugested (this was someting i also tried before) the second command returns an error. here is the cli output: the script changed:

$cmds = array ('sh run int te 1/1', 'sh run int te 1/2');

$connection = ssh2_connect('10.1.1.1', 22);
ssh2_auth_password($connection, 'user', 'pass');

foreach ($cmds as $cmd) {
    $stream = ssh2_exec( $connection, $cmd );
    stream_set_blocking( $stream, true );
    $stream_out = ssh2_fetch_stream( $stream, SSH2_STREAM_STDIO );
    echo stream_get_contents($stream_out);}

the output from cli

interface TenGigabitEthernet1/1
description trunk
switchport trunk allowed vlan 1,2,3,4,5,6,10
switchport mode trunk
auto qos trust
storm-control broadcast include multicast
storm-control broadcast level 1.00
spanning-tree guard loop
service-policy input AutoQos-4.0-Input-Policy
service-policy output AutoQos-4.0-Output-Policy
ip dhcp snooping trust
end

PHP Warning:  ssh2_exec(): Unable to request a channel from remote host       in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 13
PHP Warning:  stream_set_blocking() expects parameter 1 to be resource, boolean given in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 14
PHP Warning:  ssh2_fetch_stream() expects parameter 1 to be resource, boolean given in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 15
PHP Warning:  stream_get_contents() expects parameter 1 to be resource, null given in C:\Users\SMS\Downloads\php_scripts\ssh.php on line 16

I am only getting the output from the first command "sh run int te 1/1".

  • 写回答

2条回答 默认 最新

  • dongshun7312 2017-02-16 08:49
    关注

    You are repeating the connection stage. Try this instead:

    $cmds = array ('sh run int te 1/1', 'sh run int te 1/2');
    
    $connection = ssh2_connect('10.1.1.1', 22);
    ssh2_auth_password($connection, 'user', 'pass');
    
    foreach ($cmds as $cmd) {
        $stream = ssh2_exec($connection, $cmd);
        stream_set_blocking($stream, true);
        $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
        echo stream_get_contents($stream_out);
    }
    

    I tested now on a Ubuntu machine (14.04-LTS), and it works:

    I say 'who', answer was: lserni   :0           Jan 21 11:25 (console)
    I say 'date', answer was: Thu Feb 16 09:55:52 CET 2017
    

    ...well, apart from the fact that I forgot an open console login on that machine :-(

    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch