Skip to content

Why spatie ssh not working and not any type of error is showing to me? #115

@sameedkun

Description

@sameedkun

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions