I get it a work script for multiple commands. This script return output of each command on array, indexed by the "command".
Att: ssh2_cmd() dont work for multiple commands, thus ssh2_shell(). Attencion for de EOL on witch type of SO. I used "\n" at the end of witch command, but maybe other options like "<br>, ; , etc);
$connection = ssh2_connect($input_host_ip, 22);
if (false === $connection) {
error_log("ERROR função SSH_UMTELECOM: Connection failed!\n",3,$log_file);
exit();
}else{
if($debugar){error_log("SSH_UMTELECOM: Connection success to $input_host_ip!\n",3,$log_file);}
$auth = @ssh2_auth_password($connection, $user, $pass);
if (false === $auth) {
error_log("ERROR função SSH_UMTELECOM: Authentication failed on $input_host_ip (user: $user)!\n",3,$log_file);
exit();
}else{
if($debugar){error_log("SSH_UMTELECOM: Authentication success to $input_host_ip!\n",3,$log_file);}
error_log("Iniciando Stream: $connection\n",3,$log_file);
$output_array = array();
$shell = ssh2_shell($connection, 'vt100');
foreach($input_cmd as $cmd){
if($debugar){error_log("shell: $cmd\n",3,$log_file);}
$output_stream = ""; # Limpa variável para cada interação
fwrite($shell, "$cmd\n"); # Não esquecer o "\n" ao término de cada comando
#stream_set_blocking($shell, true); # Não funciona, aguarda infinitamente
sleep(2); # 2 segundos para redes com alta latência
while($output_streams = fgets($shell)){
$output_stream .= $output_streams;
flush();# Limpa o conteúdo para a próxima interação
}
$output_array[] = $output_stream; # Reune cada retorno em seu respectivo comando
}
fclose($shell); # Gracius logout from shell
}# Fim if false === $auth
}# Fim if false === $conenection