Skip to content

Package to use Laravel Process over an SSH connection

License

Notifications You must be signed in to change notification settings

lguichard/process-ssh

Repository files navigation

Laravel Process over SSH

GitHub Workflow Status (master) Latest Version


Laravel Process over SSH

Laravel Process over SSH is a Laravel package that extends the Illuminate\Process functionality to allow command execution via SSH.

Features

  • Execute shell commands on remote servers using SSH.
  • Full compatibility with Laravel's Process features.
  • Easily configurable options like custom ports, passwords, private keys, and more.

Installation

Install the package via Composer:

composer require lguichard/process-ssh

Usage

To execute a command over SSH, use the Process facade:

Basic Usage

use Illuminate\Support\Facades\Process;

$result = Process::ssh([
        'host' => '192.168.1.10',
        'user' => 'username',
        'password' => 'your_password',
    ])
    ->run('ls -al');

if ($result->successful()) {
    echo $result->output();
} else {
    echo $result->errorOutput();
}

Using Private Key Authentication

$result = Process::ssh([
        'host' => '192.168.1.10',
        'user' => 'username',
        'private_key' => '/path/to/private_key',
    ])
    ->run('ls -al');

Disabling Strict Host Key Checking

$result = Process::ssh([
        'host' => '192.168.1.10',
        'user' => 'username',
        'private_key' => '/path/to/private_key',
    ])
    ->disableStrictHostKeyChecking()
    ->run('ls -al');

Adding Extra SSH Options

$result = Process::ssh([
        'host' => '192.168.1.10',
        'user' => 'username',
        'private_key' => '/path/to/private_key',
    ])
    ->addExtraOption('-o LogLevel=ERROR')
    ->addExtraOption('-o ConnectTimeout=10')
    ->run('ls -al');

Use the favorites method provided by Laravel's Process class.

For more information, refer to the official documentation : https://laravel.com/docs/11.x/processes

$process = Process::ssh([
        'host' => '192.168.1.10',
        'user' => 'username',
        'password' => 'your_password',
    ])
    ->start('bash import.sh');

$result = $process->wait();
[$result1, $result2] = Process::ssh([
        'host' => '192.168.1.10',
        'user' => 'username',
        'password' => 'your_password',
    ])
    ->concurrently(function (Pool $pool) {
        $pool->command('ls -al');
        $pool->command('whoami');
    });
$result = Process::ssh([
        'host' => '192.168.1.10',
        'user' => 'username',
        'password' => 'your_password',
    ])
    ->pool(function (Pool $pool) {
        $pool->command('ls -al');
        $pool->command('whoami');
    });

SSH multiplexing

If you want to execute multiple commands over the same SSH connection, SSH multiplexing allows you to reuse an existing TCP connection, improving efficiency and reducing overhead.

$process = Process::ssh([
    'host' => '192.168.85.5',
    'user' => 'ubuntu',
    'port' => 22,
])->useMultiplexing();

$commands = [
    'ls -al', 'whoami',
    'pwd', 'uname -a',
    'df -h', 'top -bn1',
    'cat /etc/os-release', 'netstat -tuln',
    'uptime', 'tail -n 20 /var/log/syslog',
];

foreach ($commands as $command) {
    $process->run($command)->output();
}

Testing

To run the package's tests:

composer test

Contributing

Contributions are welcome! Please submit a pull request or open an issue on GitHub.

License

This package is open-source software licensed under the MIT license.

For more details, visit the GitHub repository.

Acknowledgments

Special thanks to Spatie's SSH package for inspiring the creation of this package.

Skeleton PHP was created by Nuno Maduro under the MIT license.

About

Package to use Laravel Process over an SSH connection

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages