PHP SSH 多条命令 [英] PHP SSH multiple commands
问题描述
我正在尝试构建 ssh 脚本以在主机上运行多个命令.我的目标是获取每个命令的输出.我的目标主机是一台 cisco 路由器,为了让下面的脚本执行更多的命令,我需要为我想要执行的每个命令运行它,这不是一个非常优雅的解决方案.
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); }
我创建了一个循环,因为我无法获得同一流中每个命令的输出.因为我猜 php 在每个流的末尾终止了 ssh 连接.
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).
我正在编辑帖子以回答@Melvin Koopmans 和@LSerni 提出的建议.
I am editing the post to answer the sugestions made by @Melvin Koopmans and @LSerni.
如果我按照建议更改代码(这是我之前也尝试过的),第二个命令会返回错误.这是 cli 输出:脚本改变了:
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);}
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"的输出.
I am only getting the output from the first command "sh run int te 1/1".
推荐答案
我不建议在每个循环上都启动一个新的连接,而是先设置连接,然后循环一系列命令并将输出推送到一个数组.像这样:
I wouldn't recommend initiating a new connection on every single loop, instead set the connection first then loop over an array of commands and push the output to an array. Like so:
$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
.
This will push all of the output to the array $output
.
现在你可以遍历 $output,或者你可以选择给 output 提供 yoru 命令的键,以便你可以访问它:
Now you can loop over $output, or you can choose to give output the key of yoru command so you can access it:
$output[$cmd] = stream_get_contents($stream_out);
然后,例如,调用:$output['ls']
这篇关于PHP SSH 多条命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!