php-ssh

An experimental object oriented SSH api in PHP

  • Owner: Herzult/php-ssh
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

PHP SSH

Build Status (master)

Provides an object-oriented wrapper for the php ssh2 extension.

Requirements

You need PHP version 5.3+ with the SSH2 extension.

Installation

The best way to add the library to your project is using composer.

$ composer require herzult/php-ssh:~1.0

Usage

Configuration of the connection

To establish an SSH connection, you must first define its configuration.
For that, create a Configuration instance with all the needed parameters.

<?php

// simple configuration to connect "my-host"
$configuration = new Ssh\Configuration('my-host');

The available configuration classes are:

  • Configuration
  • SshConfigFileConfiguration

Both connection configuration and public/private key authentication can be obtained from a ssh config file such as ~/.ssh/config

<?php

// simple configuration to connect "my-host"
$configuration = new Ssh\SshConfigFileConfiguration('/Users/username/.ssh/config', 'my-host');
$authentication = $configuration->getAuthentication('optional_passphrase', 'optional_username');

Create a session

The session is the central access point to the SSH functionality provided by the library.

<?php

// ... the configuration creation

$session = new Ssh\Session($configuration);

Authentication

The authentication classes allow you to authenticate over a SSH session.
When you define an authentication for a session, it will authenticate on connection.

<?php

$configuration = new Ssh\Configuration('myhost');
$authentication = new Ssh\Authentication\Password('John', 's3cr3t');

$session = new Session($configuration, $authentication);

The available authentication are:

  • None for username based authentication
  • Password for password authentication
  • PublicKeyFile to authenticate using a public key
  • HostBasedFile to authenticate using a public hostkey
  • Agent to authenticate using an ssh-agent

Authentication from SshConfigFileConfiguration

If you use an ssh config file you can load your authentication and configuration from it as follows:

<?php

$configuration = new Ssh\SshConfigFileConfiguration('~/.ssh/config', 'my-host');

$session = new Session($configuration, $configuration->getAuthentication());

This will pick up your public and private keys from your config file Host and Identity declarations.

Subsystems

Once you are authenticated over a SSH session, you can use the subsystems.

Sftp

You can easily access the sftp subsystem of a session using the getSftp() method:

<?php

// the session creation

$sftp = $session->getSftp();

See the Ssh\Sftp class for more details on the available methods.

Publickey

The session also provides the getPublickey() method to access the publickey subsystem:

<?php

// ... the session creation

$publickey = $session->getPublickey();

See the Ssh\Publickey class for more details on the available methods.

Exec

The session provides the getExec() method to access the exec subsystem

<?php

// ... the session creation

$exec = $session->getExec();

echo $exec->run('ls -lah');

See the Ssh\Exec class for more details.

Main metrics

Overview
Name With OwnerHerzult/php-ssh
Primary LanguagePHP
Program languagePHP (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2011-03-27 18:02:01
Pushed At2023-05-11 20:27:20
Last Commit At2016-06-06 08:43:38
Release Count6
Last Release Namev1.1.1 (Posted on )
First Release Namev0.1.0 (Posted on )
用户参与
Stargazers Count360
Watchers Count21
Fork Count80
Commits Count100
Has Issues Enabled
Issues Count30
Issue Open Count17
Pull Requests Count25
Pull Requests Open Count10
Pull Requests Close Count10
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private