Diff the same file on two remote servers

Suppose you are on a vpn with two identical servers, for need to ensure there are no differences between the two httpd.conf file on the remote servers.

The basic command is

diff <(ssh user@host1 "sudo cat/etc/httpd/conf/httpd.conf") <(ssh user@host2 "sudo cat /etc/httpd/conf/httpd.conf")

However if you needed to compare a folder of files on 8 servers you can compare the first against the other 7, if it fails you know at least one is different

 

#!/bin/bash

for server in {2..8};
   do
     for file in `ssh user@host$server "sudo ls /foldertoscan/*.xml"`;
        do
           diff <(ssh user@host1 "sudo cat $file") <(ssh user@host$server "sudo cat $file")
        done 
   done

 

Or to check they are actually the same file is way more efficient

#!/bin/bash

MSG=""

    for server in {2..8};
     do
        for file in `ssh user@host$server "sudo ls /foldertoscan/*.xml"`;
          do

             checksum1=`ssh user@host1 "md5sum $file"`
             checksum2=`ssh user@host$server "md5sum $file"`

                 if [ "$checksum1" != "$checksum2" ]; then
                   MSG="$MSG ERROR The file $file on server host1 has an md5 checksum of $checksum1 which differs from that of server host$server which is $checksum2 "
                 fi

          done

   done

echo $MSG

 



From bland to brand, get your business online
Website hosting and design by Dedicated to Servers, contact info@dedicatedtoservers.com

Click to send a quick inquiry

All rights Reserved © 2014-2024