Giter Site home page Giter Site logo

ssh-client's Introduction

Invoke OpenSSH from PHP.

$ cat example.php
require_once 'path/to/vendor/autoload.php';

use SSHClient\ClientConfiguration\ClientConfiguration;
use SSHClient\ClientBuilder\ClientBuilder;

# Create a config for the target host

$config = new ClientConfiguration('myhost.example.com', 'boite');
$config->setOptions(array(
    'IdentityFile' => '~/.ssh/id_ed25519',
    'IdentitiesOnly' => 'yes',
));

# Use the config to create a client builder for the target host

$builder = new ClientBuilder($config);

# build a Secure Copy client and copy some files to the target host

$config->setSCPOptions(array('r'));
$scp_client = $builder->buildSecureCopyClient();
$scp_client->copy(
    'path/to/somedir',
    $scp_client->getRemotePath('~/')
);

# build an SSH client and run a command on the target host

$ssh_client = $builder->buildClient();
print($ssh_client->exec(array('cat', '~/somedir/test.txt'))->getOutput());
# end example.php

$ mkdir -p path/to/somedir
$ echo "Let's perform a quirkafleeg." > path/to/somedir/test.txt
$ php example.php
Let's perform a quirkafleeg.
$

OpenSSH's config file can be relied on for configuration of SSH and SCP clients:-

$ cat ~/.ssh/config
Host attic attic.example.com
    Hostname attic.example.com
    Port 10022
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_ed25519
    User willy

$ cat another-example.php
require_once 'path/to/vendor/autoload.php';

use SSHClient\ClientConfiguration\ClientConfiguration;
use SSHClient\ClientBuilder\ClientBuilder;

$config = new ClientConfiguration('attic', 'willy');
$builder = new ClientBuilder($config);

$ssh_client = $builder->buildClient();
print($ssh_client->exec(array('hostname'))->getOutput());
print($ssh_client->exec(array('whoami'))->getOutput());
# end another-example.php

$ php another-example.php
attic.example.com
willy
$

ssh-client's People

Contributors

boite avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.