PHP SSH多个命令
我正在尝试构建一个ssh脚本,以便在主机上运行多个命令。我的目标是获得每个命令的输出。我的目标主机是一个cisco路由器,为了让下面的脚本执行多个命令,我需要为我要执行的每个命令运行它,这不是一个非常优雅的解决方案PHP SSH多个命令,php,ssh,Php,Ssh,我正在尝试构建一个ssh脚本,以便在主机上运行多个命令。我的目标是获得每个命令的输出。我的目标主机是一个cisco路由器,为了让下面的脚本执行多个命令,我需要为我要执行的每个命令运行它,这不是一个非常优雅的解决方案 $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);
$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); }
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
我只从第一个命令“sh run int te 1/1”获得输出。您正在重复连接阶段。请尝试以下方法:
$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);
}
我现在在Ubuntu机器(14.04-LTS)上进行了测试,它可以工作:
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
…嗯,除了我忘记了在那台机器上有一个打开的控制台登录之外:-(您正在重复连接阶段。请尝试以下操作:
$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);
}
我现在在Ubuntu机器(14.04-LTS)上进行了测试,它可以工作:
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
…嗯,除了我忘记了在那台机器上打开控制台登录之外:-(我不建议在每个循环上启动一个新连接,而是先设置连接,然后在命令数组上循环,并将输出推送到一个数组。类似这样:
$cmds = [ 'ls', 'ps ux' ];
$connection = ssh2_connect( '127.0.0.1', 22 );
ssh2_auth_password( $connection, 'username', 'password' );
$output = [];
foreach ($cmds as $cmd) {
$stream = ssh2_exec( $connection, $cmd );
stream_set_blocking( $stream, true );
$stream_out = ssh2_fetch_stream( $stream, SSH2_STREAM_STDIO );
$output[] = stream_get_contents($stream_out);
}
这将把所有输出推送到数组$output
现在,您可以循环$output,也可以选择为output指定YOUU命令的键,以便访问它:
$output[$cmd] = stream_get_contents($stream_out);
然后,例如,调用:
$output['ls']
我不建议在每个循环上启动一个新连接,而是先设置连接,然后在命令数组上循环,并将输出推送到一个数组中。如下所示:
$cmds = [ 'ls', 'ps ux' ];
$connection = ssh2_connect( '127.0.0.1', 22 );
ssh2_auth_password( $connection, 'username', 'password' );
$output = [];
foreach ($cmds as $cmd) {
$stream = ssh2_exec( $connection, $cmd );
stream_set_blocking( $stream, true );
$stream_out = ssh2_fetch_stream( $stream, SSH2_STREAM_STDIO );
$output[] = stream_get_contents($stream_out);
}
这将把所有输出推送到数组$output
现在,您可以循环$output,也可以选择为output指定YOUU命令的键,以便访问它:
$output[$cmd] = stream_get_contents($stream_out);
然后,例如,调用:
$output['ls']
是否可以使用“&&&”内爆数组,使其自动执行一个接一个的所有命令?是否可以使用“&&”内爆数组,使其自动执行一个接一个的所有命令?