Command line utility to compare a local directory structure of files against the root of a remote server over an SSH connection.
Well, I like to keep production server configuration files under version control in local repositories (example) - this utility was created to allow for quick discovery of any file-based differences between the two. Only local files that are different or missing from the remote server are checked for.
Differences are found by comparing SHA1 keys of a file content, using PHP's
sha1_file() on local filesystem and
sha1sum [filename] on the remote server, optionally server files that are found to be different can be transferred back to your local machine over SCP for difference checking via Meld, KDiff3, etc.
All SSH authorization is via RSA public/private keys (as should be the case with any SSH).
- PHP 5.4+.
- PHP Secure Shell2 extension (PECL).
- Bindings for PHP7 via GitHub.
- SSH connectivity to your remote server(s) via RSA public/private keys.
Also shown by running
sshdiff.php without command line option(s).
Usage: sshdiff.php -s SERVER --root-dir DIR Required: -s SERVER Target SSH server address/host --root-dir DIR Source root directory Optional: -p PORT Alternative SSH port number, default is 22 -u USERNAME User for SSH login, if not given current shell username used -v Increase verbosity --diff-dir DIR If file differences found target file(s) will be placed into this directory --priv-key FILE Private key file location - default [/home/username/.ssh/id_rsa] --pub-key FILE Public key file location - default [/home/username/.ssh/id_rsa.pub]
Taking the following scenario:
- Local server configuration files in
- Remote server is at
- Current local user for login username (
magnetikin this case);
- Any remote file differences SCP'ed back to
Would result in something like this:
$ ./sshdiff.php -s myserver.domain \ --root-dir /myserver.domain/filesystem \ --diff-dir /tmp/diffs Missing: /etc/redis.conf Difference: /etc/nginx/nginx.conf ================================= All done - differences were found $ diff \ /myserver.domain/filesystem/etc/nginx/nginx.conf \ /tmp/diffs/etc/nginx/nginx.conf