I have a script to detect if a host is pingable or not. The problem is that I would like it to put the nonpingable hosts in one file and the pingable hosts in another. I have come up with this so far:
for ip in `cat /tmp/testlist2`; do ping -c 3 $ip >/dev/null && echo "$ip is up" || echo "$ip is down"; done>/tmp/s4.file
cat /tmp/s4.file |grep -i down >/tmp/f1.file
cat /tmp/s4.file |grep -i up >/tmp/sup.file
This works but it is clumsy. If I try to just do a redirect after the ping it doesn't work, though. What I get is only the first host name that the ping is tried on. Would I need another loop to get the hosts in two different files? Any suggestions?
Example: You can change output of up/down to separete files if you like
#!/bin/bash
for ip in $(cat /tmp/testlist2); do
if [[ "$(ping -c 1 -w 1 $ip | awk '/received/ {print $4}')" == "0" ]]
then
echo "$(date) $ip is down" >> /tmp/log
else
echo "$(date) $ip is up" >> /tmp/log
fi
done
rm -f /tmp/f1.file /tmp/sup.file
for ip in $(cat /tmp/testlist2)
do ping -c 3 $ip >/dev/null && echo "$ip is up" >> /tmp/sup.file || echo "$ip is down" >> /tmp/f1.file
done