Agnus Dei (jackal) wrote,
Agnus Dei
jackal

bash script to diff files across two different servers (over ssh connection)

I was tired of downloading files locally and running diff after downloading them. If they had the same name I'd have to rename them first. So I wrote a script to diff two files across two different servers by cat'ing their contents to standard input and reading that into diff.

Example: I can diff something like /etc/ssh/sshd_config across two different servers without pulling the files down locally, renaming them, and then running diff.


[myhost]$ cat diffx 
#!/bin/bash

if [[ (($# -eq 2 && $1 =~ ":" && $2 =~ ":")) ]]; then
	/bin/true  # all good
else
	# did not submit two arguments in the correct format
	echo Usage: $0 hostA:fileA hostB:fileB
	exit
fi

HOST_A=`echo $1|cut -f1 -d:`
FILE_A=`echo $1|cut -f2 -d:`

HOST_B=`echo $2|cut -f1 -d:`
FILE_B=`echo $2|cut -f2 -d:`

diff \
<(ssh $HOST_A cat $FILE_A) \
<(ssh $HOST_B cat $FILE_B) 

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 0 comments