Wednesday, December 3, 2014

Bash script to compute time difference between two systems

#!/bin/sh

## Script to compute and log time difference between a remote host and a local host
## Please note that password less authentication is already set for the remote host

FILE=/home/dev/time.log
SLEEP=300 #seconds
rm -f $FILE
while [ true ]
do
    #Use `command` to store output of a command in a variable
    SERVER_TIME=`ssh root@10.112.81.41 date +"%s"`

    # $(command) syntax can also be used to store output of a command into a variable
    CLIENT_TIME=$(date +"%s")

    # Get diff of two variables. Note double ((
    DIFF=$(($SERVER_TIME-$CLIENT_TIME))

    # Echo to file and also concatnate command to text
    echo "Difference = [$DIFF] seconds at $(date)" >> $FILE

    sleep $SLEEP
done


No comments :

Post a Comment