slack slackbot

Creating a slackbot to manage VPS’s in openstack – Part 3

So in our previous post here we made discussed the basics on how to make a connection over SSH via PHP.

I continue here by explaining I have a jump tower. Basicly the only way to connect to ‘any’ VPS is via this jump tower. There is no (ssh) connection to these servers other than this tower. In my case to execute something on a VPS, I would have to connect to the tower, then “jump” (just ssh) to the actual server and execute the command there. Return the output somehow and hoping we have hit the correct server.

This is madness to do with just PHP. End of story.

Soooo I thought a bit out of the box by creating some bash scripts on the tower. Executing certain commands by parameters on different servers. There is an easy to way to do this:

root@TOWER:~# ssh root@vps1337 'service apache2 status'
 * apache2 is running

So by adding a command to the ssh command, we actually return the output from a server-x to our tower.

Therefor I created a script like this:

ssh root@$1 $2

So I can “call” this script with 2 arguments. The first argument is the server, the second is the “command” I want to execute.

Going back to PHP I have made some checks on what type of input I’m allowing.

$this->allowedTypes = [ 'apache2', 'nginx', 'php-fpm', 'mysql', 'load'];

Adding some more logic I end up with a “run command” that connects to my tower, and initializes the correct bash script with its arguments.

    private function runCommand($vps, $type, $command) {

        if($type == 'load') {
            $msg = $this->exec("./ ".$vps."");
        } else {
            $msg = $this->exec("./ " . $vps . " 'service " . $type . " " . $command . "'");

        return $msg;

The result is something like this:

simon slackbot commands

simon slackbot commands

And when in doubt ask for help:

simon slackbot help

simon slackbot help

This post is part of a series of 3

part 1
part 2
This page

Leave a Reply

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