python-boxadmin is a set of modules for the Python programming language which allows you to execute tasks on remote hosts over SSH.

It is released under the terms of the GNU General Public License version 3 or later.

Features

  • Uses a standard SSH configuration file to list the hosts to connect to
  • Uses SSH connection multiplexing so that the penalty time for connecting to a host only occurs once.
  • Designed with flexibility in mind and it's easy to write your own commands.

Using python-boxadmin

Getting help

To display the commands available to you run, simply run:

scripts/boxadmin

This will give you the following help page:

Usage: boxadmin [options] <hosts_file> <command> [arguments...]

Options:
  -i <host>      Include host in this run
  -x <host>      Exclude host from this run
  -s <skip>      Start by skipping this many hosts
  -l <limit>     Stop after this many hosts have been processed

File commands.
  file::exists <file>            Test whether a file or directory exists.
  file::remove <file>            Recursively remove a file or directory.

Host commands.
  host::connect                  Connect to the host.
  host::disconnect               Disconnect from the host.
  host::run <command>            Run a command on the host.
  host::shell                    Open a shell on the host.

Package commands.
  package::distro                Get the distribution.
  package::install <package>     Install or upgrade the package.
  package::installdeb <deb>      Install or upgrade the package from the given archive.
  package::installed <package>   Check whether the package is installed.
  package::version <package>     Get the version of the specified package.

Service commands.
  service::disable <service>     Disable the service's init script.
  service::purge <service>       Disable and delete the service's init script.
  service::restart <service>     Restart the service.
  service::start <service>       Start the service.
  service::stop <service>        Stop the service.

The configuration file

To start using python-boxadmin, create an SSH configuration file called "hosts" for intance, containing:

Host some.server.org
  User someuser

Host other.server.org
  User otheruser

Running the interactive shell

Next, run:

scripts/boxadmin hosts

Writing your own commands

Create a directory called "commands", and create a file called "test.py", containing:

import os
import logging

import boxadmin.commands

class Commands(boxadmin.commands.Commands):
  """Test commands."""

    @staticmethod
    def checkssh(host):
        """Check whether the directory .ssh exists on the remote host."""

        logging.info("Checking whether .ssh exists on %s", host.host)
        if File.exists(host, ".ssh"):
            logging.info("It exists!")

Getting python-boxadmin

You can checkout the latest development version using Subversion as follows:

svn co https://svn.bolloretelecom.eu/opensource/python-boxadmin/trunk/ python-boxadmin