Using sftp with Sitecopy non-interactively

  Development

March 2020

Motivation

Sitecopy can be operated without user interaction when using ftp - but not when using sftp.

Description

Sitecopy is a nice tool to mirror websites to remote web servers. I've been using it successfully for many years. However, once I had to switch a site to sftp protocol, Sitecopy asked for the sftp password despite the password being specified in the config file. This did not fit my needs so that a solution needed to be found.

Implementation

Let's look at the expectation, the actual behavior, and then workaround the issue step by step.

Sitecopy with ftp

Here is an example of a simple Sitecopy config file for an ftp site. With such a file, no password needs to be entered any more when using Sitecopy for this site.

site example
  server mywebserver.example
  username myuser
  password mypassword
  local /home/webs/example
  remote ~/

Attempting to do the same with a site accessible via sftp

Here is the same simple Sitecopy config file but with sftp protocol specified.

site example
  protocol sftp
  server mywebserver.example
  username myuser
  password mypassword
  local /home/webs/example
  remote ~/

Unfortunately, Sitecopy is no longer working non-interactively. Instead, the tool is asking for the sftp password when attempting to access the remote site. The password specified in the config file is obviously ignored. This is neither the expected nor the desired behaviour.

Attempts to solve the issue using sshpass

I have found an undocumented parameter in forums. It specifies the command to be called by Sitecopy. My first idea was to provide the password with sshpass to the sftp command.

site example
  protocol sftp
  server mywebserver.example
  username myuser
  password mypassword
  local /home/webs/example
  remote ~/
  rcp "sshpass -p 'mypassword' sftp"

However, this does not work. Sitecopy expects a single command to be called.

Okay... Then let's make it a single command by wrapping everything into a Bash command.

site example
  protocol sftp
  server mywebserver.example
  username myuser
  password mypassword
  local /home/webs/example
  remote ~/
  rcp "bash -c \"sshpass -p 'mypassword' sftp\""

However, this still does not work. Sitecopy concatenates the remote server name to the given command. But that string is outside the quotes so that this attempt yields an error.

We need a single command to be called that is able to take arguments. Let's create one.

Working solution using sshpass

We provide a script as the command to be called by Sitecopy. Sitecopy provides the remote server name as argument to this script.

site example
  protocol sftp
  server mywebserver.example
  username myuser
  password mypassword
  local /home/webs/example
  remote ~/
  rcp "/home/sitecopy/sftp_profile_example"

We then need to create this script (here: "/home/siteopy/sftp_profile_example") that is able to forward any arguments to the sftp command. Here we go:

#!/bin/bash
sshpass -p 'mypassword' sftp $@

This works! Sitecopy calls the script as sftp command. The script calls the sftp command with any arguments provided by Sitecopy. sshpass is used to provide the password to sftp.

Note that you should not use sshpass with the "-p" argument. It is more secure to provide the password in a file (with "-f") or via environment (with "-e").

References