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 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制