SSH Wrapper in PHP

I have created a slackbot a while ago that used PHP as language. Read more about that here.

Time has passed and after some migrations it is going to be time to update that bot and/or creating a PHP-based UI for the servers. In general you could wonder why I want to make something like that in PHP.  I actually agree, because PHP is not the the language (or script..whatever) that you can easy integrate with lets say Linux..

On the other hand I’m actually pretty ok in PHP plus it is darn easy to create a system for a interface displaying data. The only data I actually want is: information about apache or nginx (so the domains, aliases)  and maybe some extra stuff like the php-modules.

Honestly I can retrieve that information with a simple command. I do not need a fancy daemon running 24/7 on my server or a sick API giving all the results.

I’m fine with a simple class/function that can make a connection once every week/month and run a command to get the current domains. For instance apache2ctl -S ‘s output gives me more than enough information and I’m done.

So, lets connect to our server

It is kinda non-native(?) to do that with PHP but in essence it is really simple. You add the ‘ssh2’ php extension and you are set.

To make it a bit more easy, I have created a very simple wrapper which I shared on github: https://github.com/wiardvanrij/sshwrapper .

Just for the sake of it a basic example:

<?php

require('vendor/autoload.php');

use SshWrapper\SshCore;


$ssh = new SshCore('123.123.123.123');
$ssh->connect();

$result = $ssh->exec('cat /etc/hostname');

And voila, you are done.

 

 

 

 

 

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *