Download the PHP package detain/sshwitch without Composer

On this page you can find all versions of the php package detain/sshwitch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package sshwitch

sshwitch — SSH Switch / Router Helper

License: GPL v3 PHP Version

sshwitch is a small PHP library that runs commands on network devices (switches, routers, firewalls) by connecting from a relay box to the device through RANCID's clogin script.

It's built around an all-static, optionally-chainable API that's pleasant to embed in operations scripts — pass a switch IP and a list of commands, get back the parsed per-command output.

clogin itself supports a long list of vendors: Cisco, Juniper, Extreme Networks, Procket Networks, Redback, A10, Alteon, Avocent (Cyclades), Bay Networks (Nortel), ADC-kentrox, Foundry, HP, Hitachi, MRV, Mikrotik, Netscreen, Nokia (Alcatel-Lucent), Netscaler, Riverstone, Netopia, Xirrus, Arrcus, and more. This library has been used most heavily against Cisco IOS and Juniper but the transport is identical for any device clogin knows.

Table of Contents

Requirements

Installation

Install via Composer:

Then ensure the ssh2 extension is loaded:

If it's missing, install it (Debian/Ubuntu: apt install php-ssh2).

Quick Start

Configuration

The library reads four define()'d constants. They must be defined before calling connect() or run():

Constant Description Example
CLOGIN_SSH_HOST IP/hostname of the relay box '10.0.0.1'
CLOGIN_SSH_PORT SSH port on that relay 22
CLOGIN_SSH_USER SSH username on the relay 'sshuser'
CLOGIN_SSH_KEY Path to private key (public key sits at .pub) '/home/sshuser/.ssh/id_rsa'

The relay box is also where /usr/libexec/rancid/clogin must exist. RANCID's .cloginrc on that box is what controls device-side credentials.

How it works

  1. connect() opens an SSH session from your PHP process to the relay box using the CLOGIN_SSH_* constants.
  2. run($switch, $commands) builds a clogin command line, prepends show hostname so the parser can anchor on the device prompt, and runs it through ssh2_exec().
  3. The combined stream is parsed: the prelude/banner is dropped, each command's output is captured into its own array entry, and noisy IOS progress bars ([##### ] N%) are collapsed to their final entry to keep logs sane.
  4. When $autoDisconnect is true (default), the SSH session is closed.

API Reference

Methods

Method Description
Sshwitch::connect() Opens (and authenticates) the SSH session if not already open. Returns true/false.
Sshwitch::disconnect() Closes the SSH session if one is open. Safe to call when nothing is connected.
Sshwitch::run(string $switch, $commands, string $type='cisco', array &$return=[]) Runs $commands against $switch, returns the parsed output array (or false on failure).
Sshwitch::__callStatic($name, $args) Magic dispatch for the auto-generated getXxx()/setXxx() accessors.

run()'s $type parameter is currently unused — it's reserved for future overloads that branch on device family without a signature change. Leave the default unless you're a contributor wiring a vendor-specific code path.

run()'s $return is an out-parameter that mirrors the return value; either is fine, depending on which style you prefer.

Properties (via magic accessors)

Every public static property has an auto-generated get/set pair. Setters return true on success (or the class name in chaining mode); getters always return the value:

Property Type Default Get / Set
$connection resource\|false false getConnection(), setConnection($r)
$timeout int 10 getTimeout(), setTimeout(int) (clogin -t)
$switch string '' getSwitch(), setSwitch(string)
$commands array [] getCommands(), setCommands(array)
$output string '' getOutput(), setOutput(string)
$hostname string '' getHostname(), setHostname(string)
$motd string '' getMotd(), setMotd(string)
$commandOutputs array [] getCommandOutputs(), setCommandOutputs(array)
$autoDisconnect bool true getAutoDisconnect(), setAutoDisconnect(bool)
$chaining bool false getChaining(), setChaining(bool)

Calling a setXxx() for a property that doesn't exist throws BadMethodCallException (so does any other unknown method).

Static chaining

Set Sshwitch::$chaining = true (or Sshwitch::setChaining(true)) to make setters and action methods (connect, disconnect, run) return the class name string instead of their natural return value, allowing PHP's :: chaining syntax:

Getters never chain — calling getOutput() always returns the value, even when chaining is on, because chained getters would be useless.

Examples

Run a single command

Pass commands as a semicolon-separated string

Reuse one SSH session for several run() calls

Inspect raw vs parsed output

Handle failures gracefully

connect() returns false on connect/auth failure; run() returns false when connect() fails, when ssh2_exec itself returns false, or when the output can't be parsed:

Testing

The test suite mocks every ssh2_* and stream function in the Detain\Sshwitch namespace with php-mock/php-mock-phpunit, so no real SSH server is ever contacted.

The PHPUnit configuration lives in phpunit.xml.dist and bootstraps tests/bootstrap.php, which defines stub CLOGIN_SSH_* constants for the test run.

Troubleshooting

Undefined constant CLOGIN_SSH_HOST — define the four constants listed under Configuration before calling any Sshwitch method.

ssh2_connect: Could not connect — verify you can SSH from the host running PHP to the relay box manually using the same private key and user.

run() returns false but getOutput() shows data — the parser anchors on <hostname># <command> prompt lines. If the device prompt isn't present (e.g. cmdline ran on a non-clogin shell), parsing won't match.

Trait "phpmock\phpunit\PHPMock" not found — run composer install --dev so php-mock/php-mock-phpunit is available.

Idle, hung connections from earlier scripts — call Sshwitch::disconnect() explicitly when $autoDisconnect is off, otherwise PHP will eventually close the resource on shutdown.

License

GNU General Public License v3.0 — see LICENSE.


All versions of sshwitch with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package detain/sshwitch contains the following files

Loading the files please wait ...