Skip to content

Instantly share code, notes, and snippets.

@chewmanfoo
Created December 7, 2011 23:04
Show Gist options
  • Save chewmanfoo/1445193 to your computer and use it in GitHub Desktop.
Save chewmanfoo/1445193 to your computer and use it in GitHub Desktop.
/usr/bin/check_remote
#!/bin/bash
# check_remote
SET="/etc/interop/backupapp/backupset.txt"
TMPSET="/tmp/set"
. /usr/bin/backupapp.h
START=$(date +%s)
i=0
# if ( ! getopts "ih" opt); then
# echo "Usage: `basename $0` options (-i) -h for help";
# exit $E_OPTERROR;
# fi
while getopts "ih" Option
do
case $Option in
i ) echo "option -i- [OPTIND=${OPTIND}]"; INTERACTIVE=1;;
h ) echo "option -$Option- [OPTIND=${OPTIND}]"; help "check_remote";;
* ) echo "Unimplemented option chosen.";; # Default.
esac
done
[ ! -f $SET ] && cecho "$SET does not exist" $red && exit 1
# strip off remarks
sed '/^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//' $SET >$TMPSET
cecho "Remote Host file checker" $yellow
cecho "Files to be checked: ($TMPSET)..." $green
cat $TMPSET
echo
cecho "Continue with the check now? (Yyn)" $magenta
Acknowledge
i=0
while IFS=, read file host
do
echo "file: $file, host: $host"
echo "check_remote - checking $file..."
/usr/bin/remote_diff -r $host -f $file
RETVAL=$?
if [ $RETVAL -gt 0 ];
then echo "remote_diff failed - breaking out!" && break
fi
((i++))
done < $TMPSET
rm -f $TMPSET
END=$(date +%s)
DURATION=$(( $END - $START ))
cecho "This check, in whole, took $DURATION seconds." $yellow
exit $RETVAL
(using bash -x)
# bash -x check_remote
+ SET=/etc/interop/backupapp/backupset.txt
+ TMPSET=/tmp/set
+ . /usr/bin/backupapp.h
++ VERSION=2.0.4
++ black='\E[30;40m'
++ red='\E[31;40m'
++ green='\E[32;40m'
++ yellow='\E[33;40m'
++ blue='\E[34;40m'
++ magenta='\E[35;40m'
++ cyan='\E[36;40m'
++ white='\E[37;40m'
++ date +%s
+ START=1323299691
+ i=0
+ getopts ih Option
+ '[' '!' -f /etc/interop/backupapp/backupset.txt ']'
+ sed '/^[[:blank:]]*#/d; s/[[:blank:]][[:blank:]]*#.*//' /etc/interop/backupapp/backupset.txt
+ cecho 'Remote Host file checker' '\E[33;40m'
+ local 'default_msg=No message passed.'
+ message='Remote Host file checker'
+ color='\E[33;40m'
+ echo -e '\E[33;40m'
+ echo 'Remote Host file checker'
Remote Host file checker
+ tput sgr0
+ return
+ cecho 'Files to be checked: (/tmp/set)...' '\E[32;40m'
+ local 'default_msg=No message passed.'
+ message='Files to be checked: (/tmp/set)...'
+ color='\E[32;40m'
+ echo -e '\E[32;40m'
+ echo 'Files to be checked: (/tmp/set)...'
Files to be checked: (/tmp/set)...
+ tput sgr0
+ return
+ cat /tmp/set
/tmp/iptables,10.10.0.42
/tmp/all_rpms.txt,10.10.0.42
/etc/sms.cfg,10.10.0.42
/etc/ss7_siu/system.txt,10.10.0.42
/etc/ss7_siu/siu_configs/config_10.10.0.50.txt,10.10.0.42
/etc/ss7_siu/siu_configs/config_10.10.0.51.txt,10.10.0.42
/etc/ss7_siu/tcap_license.lic,10.10.0.42
/etc/httpd/conf/httpd.conf,10.10.0.42
/etc/httpd/conf.d/00-fullstatus.conf,10.10.0.42
/etc/httpd/conf.d/00-mod_passenger.conf,10.10.0.42
/etc/httpd/conf.d/01-ota_services2.conf,10.10.0.42
/etc/httpd/conf.d/01-sp_api.conf,10.10.0.42
/etc/httpd/conf.d/proxy_ajp.conf,10.10.0.42
/var/www/ota_services/config/auth_keys.yml,10.10.0.42
+ echo
+ cecho 'Continue with the check now? (Yyn)' '\E[35;40m'
+ local 'default_msg=No message passed.'
+ message='Continue with the check now? (Yyn)'
+ color='\E[35;40m'
+ echo -e '\E[35;40m'
+ echo 'Continue with the check now? (Yyn)'
Continue with the check now? (Yyn)
+ tput sgr0
+ return
+ Acknowledge
+ test '' == 0
+ read answer
y
+ test y '!=' Y -a y '!=' y
+ cecho 'You have instructed me to continue. Good luck.' '\E[32;40m'
+ local 'default_msg=No message passed.'
+ message='You have instructed me to continue. Good luck.'
+ color='\E[32;40m'
+ echo -e '\E[32;40m'
+ echo 'You have instructed me to continue. Good luck.'
You have instructed me to continue. Good luck.
+ tput sgr0
+ return
+ echo
+ i=0
+ IFS=,
+ read file host
+ echo 'file: /tmp/iptables, host: 10.10.0.42'
file: /tmp/iptables, host: 10.10.0.42
+ echo 'check_remote - checking /tmp/iptables...'
check_remote - checking /tmp/iptables...
+ /usr/bin/remote_diff -r 10.10.0.42 -f /tmp/iptables
localhost:/tmp/iptables -vs- 10.10.0.42:/tmp/iptables
files are identical
+ RETVAL=0
+ '[' 0 -gt 0 ']'
+ (( i++ ))
+ IFS=,
+ read file host <--- there were plenty more lines to read, dude!
+ rm -f /tmp/set
++ date +%s
+ END=1323299694
+ DURATION=3
+ cecho 'This check, in whole, took 3 seconds.' '\E[33;40m'
+ local 'default_msg=No message passed.'
+ message='This check, in whole, took 3 seconds.'
+ color='\E[33;40m'
+ echo -e '\E[33;40m'
+ echo 'This check, in whole, took 3 seconds.'
This check, in whole, took 3 seconds.
+ tput sgr0
+ return
+ exit 0
[iot-fmy-prod root@ota-dom-app1 backupapp]# cat /usr/bin/remote_diff
#!/bin/bash
. /usr/bin/backupapp.h
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if ( ! getopts "r:f:h" opt); then
echo "Usage: `basename $0` options (-r remote_host) (-f full/path/to/file) -h for help";
exit $E_OPTERROR;
fi
while getopts "r:f:h" opt; do
case $opt in
r) host=$OPTARG;;
f) file=$OPTARG;;
h) help remote_diff; exit 0;;
esac
done
RETFNF=2
REFDIFF=1
if [ -d $file ]; then
for x in $file/*; do
cecho "localhost:$x -vs- $host:$x" $red
rem_file=`ssh root@$host "cat $x 2>&-"`
if [ $? -eq 0 ]
then diff $x <(echo "$rem_file") && echo "files are identical" && exit 0
else cecho "$x not found on remote $host" $red && exit $RETFNF
fi
done
elif [ -f $file ]; then
cecho "localhost:$file -vs- $host:$file" $red
rem_file=`ssh root@$host "cat $file 2>&-"`
if [ $? -eq 0 ]
then diff $file <(echo "$rem_file") && echo "files are identical" && exit 0
else cecho "$file not found on remote $host" $red && exit $RETFNF
fi
else
cecho "$file not found on localhost" $red && exit $RETFNF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment