Diff on Output of Remote Commands
Let’s say you run the same command on two remote servers and you want to compare the output. Here’s a quick example:
diff <(ssh -qtT server01 "sudo su - root -c 'fdisk -l 2>/dev/null | grep ^Disk'") <(ssh -qtT server02 "sudo su - root -c 'fdisk -l 2>/dev/null | grep ^Disk'")
I suggest using “vimdiff” for a more easy-to-read layout:
yum -y install vim
vimdiff <(ssh -qtT server01 "sudo su - root -c 'fdisk -l 2>/dev/null | grep ^Disk'") <(ssh -qtT server02 "sudo su - root -c 'fdisk -l 2>/dev/null | grep ^Disk'")
Here’s another vimdiff
example that ignores white spaces and uses a small script to auto-close in three seconds. The /tmp/vimscript
looks like this:
:sleep 3 :qa!
And the whole command is this:
vimdiff -c 'set diffopt+=iwhite' -s /tmp/vimscript <(ssh -qtT server2 "sudo su - root -c 'cat /etc/hosts'") <(ssh -qtT server1 "sudo su - root -c 'cat /etc/hosts'")
This may be useful if you need to do a quick visual comparison of multiple files. For example, find
*.conf
in /etc
on server1
and compare them to equivalent files on server2
:for i in $(ssh -qtT server1 "sudo su - root -c 'find /etc -maxdepth 2 -type f -name "*\.conf"'" -mtime -7) do vimdiff -c 'set diffopt+=iwhite' -s /tmp/vimscript <(ssh -qtT server1 "sudo su - root -c 'cat ${i}'") <(ssh -qtT server2 "sudo su - root -c 'cat ${i}'") done