I had been using PHP's ssh_connect() and multiple ssh_exec() between two Ubuntu VMs without any problems. However, now I need to call multiple ssh_exec() from Ubuntu Server to Windows machine via OpenSSH/Cygwin. The result from ssh2_exec() in the following code prints an Array of files located at /var/www the first time, but returns empty array the second or more times.
If I use ssh2_connect before the second ssh_exec() it returns an array of files again. I even attempted to use phpseclib but had the same problem. Also, I need to execute other commands like Git so PHP functions like opendir() or readdir() won't be sufficient to solve this problem.
<?php
$host = "10.xx.x.xx";
$username = "sagunms";
$password = "password";
$conn = ssh2_connect($host, 22);
if (ssh2_auth_password($conn, $username, $password) === false) {
throw new Exception('Login is invalid');
}
//First time execution - returns array of files successfully
$stream = ssh2_exec($conn, 'cd /var/www && ls');
stream_set_blocking($stream, true);
$cmdOutput = fread($stream, 4096);
fclose($stream);
$result = explode("\n", $cmdOutput); // Convert string to array
print_r($result); // Print array
echo "<hr/>";
//Second time execution - returns an "empty array" but no errors seen
$stream = ssh2_exec($conn, 'cd /var/www && ls');
stream_set_blocking($stream, true);
$cmdOutput = fread($stream, 4096);
fclose($stream);
$result = explode("\n", $cmdOutput); // Convert string to array
print_r($result); // Print array
?>
Is there something in Cygwin OpenSSH that is causing this problem? Thanks.