I encountered a "General Error" when using Windows and executing a command. After investigating, I came across PR #64 and experimented with a few tweaks to resolve the issue. Here's what worked for me:
The Issue
The << syntax doesn't work in the Windows Command Shell. However, you can use double quotes (") instead.
The Solution
I extended the SSH class in my project and made the following changes:
<?php
namespace App\Ssh;
use Spatie\Ssh\Ssh as SpatieSsh;
class Ssh extends SpatieSsh
{
public function getExecuteCommand($command): string
{
$commands = $this->wrapArray($command);
$commandString = implode(' &&'.PHP_EOL, $commands);
if (in_array($this->host, ['local', 'localhost', '127.0.0.1'])) {
return $commandString;
}
$passwordCommand = $this->getPasswordCommand();
$extraOptions = implode(' ', $this->getExtraOptions());
$target = $this->getTargetForSsh();
$bash = $this->addBash ? "'bash -se'" : '';
return "{$passwordCommand}ssh {$extraOptions} {$target} {$bash} \"".PHP_EOL
.$commandString.PHP_EOL
."\"";
}
}
Key Changes
- Removed the
EOF part and replaced it with double quotes around the commands.
- Added
&& between commands to ensure they run sequentially on the server.
Example Usage
Here’s how I use the modified class:
$process = Ssh::create('forge', 'host')
->removeBash()
->execute([
'composer install',
'php artisan key:generate --force',
'php artisan migrate:fresh --seed --force',
'npm install && npm run build',
'php artisan storage:link --force',
]);
Example Output
ssh forge@host "
composer install &&
php artisan key:generate --force &&
php artisan migrate:fresh --seed --force &&
npm install && npm run build &&
php artisan storage:link --force
"
Additional Notes
To ensure compatibility, I added a flag in my project that uses this custom SSH class if the application is running on a Windows machine.
You might wonder: "Why not open a PR to fix this issue?" Unfortunately, I don’t currently have the time to contribute a proper fix with tests. Since this solution was developed for my hobby project, I didn’t prioritize making it a formal PR. Besides, I suspect there aren't many Windows users in the PHP community who would encounter this issue.
I hope this helps someone facing a similar problem. If you have suggestions or improvements, feel free to share. Thanks!
I encountered a "General Error" when using Windows and executing a command. After investigating, I came across PR #64 and experimented with a few tweaks to resolve the issue. Here's what worked for me:
The Issue
The
<<syntax doesn't work in the Windows Command Shell. However, you can use double quotes (") instead.The Solution
I extended the
SSHclass in my project and made the following changes:Key Changes
EOFpart and replaced it with double quotes around the commands.&&between commands to ensure they run sequentially on the server.Example Usage
Here’s how I use the modified class:
Example Output
Additional Notes
To ensure compatibility, I added a flag in my project that uses this custom SSH class if the application is running on a Windows machine.
You might wonder: "Why not open a PR to fix this issue?" Unfortunately, I don’t currently have the time to contribute a proper fix with tests. Since this solution was developed for my hobby project, I didn’t prioritize making it a formal PR. Besides, I suspect there aren't many Windows users in the PHP community who would encounter this issue.
I hope this helps someone facing a similar problem. If you have suggestions or improvements, feel free to share. Thanks!