Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Diff on Output of Remote Commands

Submitted by on January 25, 2016 – 1:47 pm

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'")

vimdiff_01

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

 

Print Friendly, PDF & Email

Leave a Reply