#! /bin/bash

### script to generate ssh-reachable nodes, /share/tmp/ssh-reachable.dat
### list of hosts ################
#cat /etc/hosts | awk 'NF==4 {print $4}' | uniq | sort > /share/tmp/temp.dat

#cat /etc/hosts | awk '{print $2}' | grep -v -e localhost |  uniq | sort > /share/tmp/temp.dat
cat /etc/hosts | awk '{print $2}' |  uniq | sort > /share/tmp/temp.dat


#echo 192.168.1.10 > /share/tmp/temp2.dat
cat /share/tmp/temp.dat >> /share/tmp/temp2.dat
hosts=$(cat /share/tmp/temp2.dat | uniq)
rm -rf /share/tmp/temp.dat /share/tmp/temp2.dat
### end of list of hosts #########

rm -rf  /share/tmp/ssh-reachable.dat /share/tmp/ssh-reachable2.dat

#echo $hosts
for i in $hosts
	do
		checkping=$(ping -q -c1 -W 1 $i | awk 'NR==4 {print $4}')

		if [ $checkping -eq 0 ]
		then
			dummy=dummy
#			echo $i is not responding to ping after 1 second
		fi

		if [ $checkping -eq 1 ]
		then
			dummy=dummy
#			echo $i is responding to ping within 1 second
			echo $i >> /share/tmp/ssh-reachable.dat
			number=$(echo $i | awk -F"c" '{print $2}')
			if [ -n "$number" ]
			then
			echo 192.168.1.$number >> /share/tmp/ssh-reachable2.dat
			fi
		fi
	done
echo ' '
echo 'hosts in the clusters as recorded in /etc/hosts are' $hosts
echo ' '
echo 'ssh-reachable hosts are' $(cat /share/tmp/ssh-reachable.dat)
### script to generate ssh-reachable nodes, /share/tmp/ssh-reachable.dat

