Skip to content

Instantly share code, notes, and snippets.

@jhqv
Last active October 22, 2018 02:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhqv/dbd59f5838ae8c83f736bfe951bd80ff to your computer and use it in GitHub Desktop.
Save jhqv/dbd59f5838ae8c83f736bfe951bd80ff to your computer and use it in GitHub Desktop.
Remote diff file using SSH, useful for comparing config files when migrating services between machines etc.
#!/bin/bash
# Compare a local file with a remote file with (default) the same path or a custom path
# by Janek Hellqvist, 2016-10-14
# Example 1: compare same file on two different hosts
# rdiff.sh /etc/nginx/nginx.conf
# Example 2: compare local file with other remote file
# rdiff.sh /etc/nginx/conf.d/http.conf /etc/nginx/conf.d/www.conf
bold=$(tput bold)
normal=$(tput sgr0)
remoteuser="username"
remoteserver="123.123.123.123"
if [ -z $1 ]; then
echo "Usage: rdiff.sh <source file> <optional remote file>"
exit 1;
fi
file=`readlink -f $1` # get absolute path to file in case it was relative
if [ -z $2 ]; then
remotefile=$file
else
remotefile=$2
fi
if [ ! -f $file ]; then
echo "Local file $file not found!"
exit
fi
echo "Comparing local ${bold}$file${normal} (>) with remote ${bold}$remoteuser@$remoteserver:$remotefile${normal} (<)"
ssh $remoteuser@$remoteserver "cat $file" | diff - $file
@vaporup
Copy link

vaporup commented Sep 4, 2017

Thank you for this idea. I changed it a little bit and put it in ssh-diff https://github.com/vaporup/ssh-tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment