#!/bin/sh
ilosc_prob=1

# 1 - fping
# 2 - arping

ping_type=2

rm -f /tmp/kto.tmp
touch /tmp/kto.tmp
chown root.apache /tmp/kto.tmp
chmod 640 /tmp/kto.tmp

if [ $ping_type -eq 1 ];then
    nice -n 19 grep -E -v "^#|^$" /etc/shaper/iplist.0 | cut -d= -f1 | cut -d/ -f1 | nice -n 19 /usr/local/sbin/fping -c $ilosc_prob -q -f - 2>> /tmp/kto.tmp
    rm -f /tmp/kto.txt
    touch /tmp/kto.txt
    chown root.apache /tmp/kto.txt
    chmod 640 /tmp/kto.txt
    cat /tmp/kto.tmp | grep "max" | cut -d: -f1 | cut -d' ' -f1 >> /tmp/kto.txt
else
    rm -f /tmp/kto.txt
    serwiface=$(ip ro ls | grep default | awk '{print $5}')
    serwip=$(ifconfig $serwiface | grep "inet addr:" | awk '{print $2}' | cut -d':' -f2)
    echo $serwip > /tmp/kto.txt
    for ip in `grep -E -v "^#|^$" /etc/shaper/iplist.0 | cut -d= -f1 | cut -d/ -f1`
    do
	iface=$(grep "^$ip\>" /etc/shaper/iplist.0 | cut -d= -f2 | cut -d' ' -f1 | cut -d- -f2)
	nice -n 19 arping -I $iface -f -c 1 $ip | grep reply | cut -d' ' -f4 >> /tmp/kto.txt &
    done
    chown root.apache /tmp/kto.txt
    chmod 640 /tmp/kto.txt
fi
