thats my controller function
public function ssh()
{
$privateKey = file_get_contents(storage_path('app/key/id_rsa'));
try {
$output = Ssh::create('root', 'c***.com')
->usePrivateKey($privateKey)
->disableStrictHostKeyChecking()
->execute('ls -la');
if ($output->isSuccessful()) {
dd($output->getOutput()); // Shows output if command runs successfully
} else {
dd('Command failed: ' . $output->getErrorOutput()); // Shows errors if command fails
}
} catch (\Exception $e) {
dd($e->getMessage());
}
}
Route::get('/ssh', [SshController::class, 'ssh']);
and if i try to run using terminal
ssh -i storage/app/key/id_rsa root@c**t.com
it works without requiring password but not in laravel
and then i tried Running SSH Manually in PHP
public function ssh()
{
$cmd = 'ssh -i ' . escapeshellarg(storage_path('app/key/id_rsa')) . ' root@c***t.com "ls -la" 2>&1';
exec($cmd, $output, $status);
if ($status === 0) {
dd($output);
} else {
dd('Command failed: ' . implode("\n", $output));
}
}
and suprisingly worked? can anyone help me whats the issue here?
thats my controller function
and if i try to run using terminal
it works without requiring password but not in laravel
and then i tried Running SSH Manually in PHP
and suprisingly worked? can anyone help me whats the issue here?