#!/bin/bash
####################
#### ASCH 2007-07-26# Script name: magent-ip-find.sh
# This script is for collecting of mail.ru agent services.
# It is possible to connect via mrim.jabber.ru, also.
WRK_DIR="$(dirname $0)"
file_with_mrim_ip_list="$WRK_DIR/magent_ip_list.txt"
file_tmp="/tmp/magent_ip_list.txt.tmp"
general_service_pool=([0]="mrim.mail.ru" [1]="mrim.jabber.ru" [2]="mrim.jabber.ealtai.ru" [3]="agent.mail.ru" [4]="mobile.agent.mail.ru")
agent_service_pre="mrim"
agent_service_post=".mail.ru"
max_serv_num=$1
max_serv_num=50 # maximum number of address, keep in mind that the list is not continuous
# the number was taken experimentally, may be there is a need to correct
if ! [ -d $WRK_DIR ] ; then
echo "[ERROR $0]: Tried to determine work directory, decide it is \"$WRK_DIR\", but couldn't found the directory."
echo "Doing exit. IPTables was not touched."
exit 1
fi
if [ -f $file_with_mrim_ip_list ] ; then
cp $file_with_mrim_ip_list "${file_with_mrim_ip_list}_$(date +%F_%k-%M_%S.%N).txt"
#rm $file_with_mrim_ip_list
fi
# doing some housekeeping
if [ -f $file_tmp ]; then rm $file_tmp
else touch $file_tmp ; fi
if [ -f $file_with_mrim_ip_list ]; then rm $file_with_mrim_ip_list
else touch $file_with_mrim_ip_list ; fi
# parsing general list
i=0
while ! [ -z "${general_service_pool[$i]}" ]; do # while strings in pool aren't void
agent_ip_address=$(nslookup ${general_service_pool[$i]} | grep "#" -v | grep "Address:" | awk '{print $2}')
echo "$agent_ip_address" >> $file_tmp
i=$(($i+1))
done
# parsing special list
i=0
while [ $i -lt $(($max_serv_num+1)) ] ; do
agent_ip_address=$(nslookup $agent_service_pre$i$agent_service_post | grep "#" -v | grep "Address:" | awk '{print $2}')
if ! [ -z $agent_ip_address ] # -n not void line; -z - line with zero length
then echo "$agent_ip_address" >> $file_tmp ; fi
i=$(($i+1))
done
# removing void lines
i=0
while read string_from_file ; do
if ! [ -z $string_from_file ] # -n not void line; -z - line with zero length
then echo "$string_from_file" >> $file_with_mrim_ip_list ; fi
done < $file_tmp
# doing some housekeeping
if [ -f $file_tmp ]; then rm $file_tmp
else touch $file_tmp ; fi
if ! [ -f $file_with_mrim_ip_list ] ; then
echo "[ERROR $0]: Attempted to create "$file_with_mrim_ip_list", to write into the file and now can't find the file."
exit 1
fi
exit 0
#############################################################
echo "##############################################"
echo "#### ####"
echo "#### blocking MRIM (mail.ru IM) section ####"
echo "#### BEGIN ####"
echo "#### ####"
echo "##############################################"
while read mrim_ip ; do
echo "\$IPTABLES -A FORWARD -d $mrim_ip -j DROP"
done < $file_with_mrim_ip_list
echo "##############################################"
echo "#### ####"
echo "#### blocking MRIM (mail.ru IM) section ####"
echo "#### END ####"
echo "#### ####"
echo "##############################################"