Skip to content

Installing SSHPT to Automate SSH Commands & File Transfers on Multiple Hosts

SSHTP is a great little Python script that will allow you to run SSH commands or transfer files on multiple hosts with a single command.  It’s simple to use.  You simply create a hosts file with all of the remote systems you want to execute a task on, then create a file with credentials to login and run the task.  You can even configure it to be executed via a website in multiple configurations (more info on that at the bottom).

 

sshpt_command_line_window

 

How to Install

 

SSHTP supports sudo, uses SSH to execute commands and SCP to transfer files, so you only need have SSH access configured on remote hosts.  On the host you’re running SSHTP from, you will need Python and a few other quick-to-install packages.

  • Ubuntu 16.04 LTS (The process is the same on any Debian based distribution)
  • Python 2.5+ (3+ works fine also)
  • Paramiko – Pythonic SSH Implementation
  • pycrypto – Python Cryptography Toolkit

In addition to supporting standard username + password authentication, SSHTP also supports key based authentication.  To install, login to your host via SSH and follow this guide.

 

First, install Python and a couple other dependencies.

sudo apt-get install python python-pip libssl-dev

 

Let’s go ahead and upgrade pip as Aptitude usually doesn’t install the latest version.

sudo -H pip install --upgrade pip

 

Note:  Using sudo -H allows you to run the command as sudo (root), while retaining proper permissions in your users home directory, so pip can cache properly.  Without the -H option, the commands will run, but your pip cache will not be readable by your user.

 

Now, we will use pip to install SSHTP, Paramiko, and pycrypto.

sudo -H pip install paramiko

sudo -H pip install pycrypto

sudo -H pip install sshpt

 

That’s it!  You’ve installed Python, pip, and all of the dependencies needed to to run SSHPT.  Next you will need to create a hosts file and authentication file based on your environmental requirements and preferences.  To get more information on all available options, use the -h switch for help.

sshpt -h

 

As you can see, there are many options available to customize the process to suit your needs.

 

[email protected]:~$ sshpt -h
usage: usage: sshpt [options] "[command1]" "[command2]" ...

positional arguments:
Commands Commands

optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-f HOSTFILE, --file HOSTFILE
Location of the file containing the host list.
-S, --stdin Read hosts from standard input
--hosts HOSTS Specify a host list on the command line.
ex)--hosts="host1:host2:host3"
-k <file>, --key-file <file>
Location of the private key file
-K <password>, --key-pass <password>
The password to be used when use the private key
file).
-o <file>, --outfile <file>
Location of the file where the results will be saved.
-a <file>, --authfile <file>
Location of the file containing the credentials to be
used for connections (format is "username:password").
-T <int>, --threads <int>
Number of threads to spawn for simultaneous connection
attempts [default: 10].
-P <port>, --port <port>
The port to be used when connecting. Defaults to 22.
-u <username>, --username <username>
The username to be used when connecting. Defaults to
the currently logged-in user.
-p <password>, --password <password>
The password to be used when connecting (not
recommended--use an authfile unless the username and
password are transient).
-q, --quiet Don't print status messages to stdout (only print
errors).
-d <path>, --dest <path>
Path where the file should be copied on the remote
host (default: /tmp/).
-x, --execute Execute the copied file (just like executing a given
command).
-r, --remove Remove (clean up) the SFTP'd file after execution.
-t <seconds>, --timeout <seconds>
Timeout (in seconds) before giving up on an SSH
connection (default: 30)
-s, --sudo Use sudo to execute the command (default: as root).
-U <username>, --sudouser <username>
Run the command (via sudo) as this user.
-c <file>, --copy-file <file>
Location of the file to copy to and optionally execute
(-x) on hosts.

 

SSHTP is very powerful.  You can easily tie it into a PHP based website to automate various commands on many different servers or workstations.  For more Information on how to do that, and get other information, check the links below.

Google Code Site for SSHTP (detailed usage info):

https://code.google.com/archive/p/sshpt/

SSHTP Website:

https://pypi.python.org/pypi/sshpt

SSHTP GibHub Site:

https://github.com/mooflu/sshpt

If you have any questions or need a hand, please feel free to post in the comments below.  Thanks!