#!/bin/bash
#功能: 域名解析,ip所屬營運商判斷,ip所屬機房判斷
#時間:2014/7/2
#作者:張樂傑
#定義檔案
#讀取腳本參數
if [ "$1" == "" -o "$2" == "" ]
then
echo "============================================================="
echo "Erro !!! Do it like this:"
echo "sh ip_check.sh domain-list-file ip-list-file "
exit
fi
domain_input=$1 #domain_name
IDC_input=$2 #IDC_name,起始IP,結束IP
domain_src_file=/tmp/domain_src.txt #domain_name
IDC_file=/tmp/IDC_ip.txt #IDC_name,起始IP,結束IP
domain_ip=/tmp/domain_ip.txt #domain_name,domain_ip
belong_file=/tmp/belong.txt #domain_name,domain_ip,belong
report=/tmp/report_tmp`date +%Y%m%d`.csv #domain_name,domain_ip,belong,IDC
report_end=/tmp/report`date +%Y%m%d`.csv #domain_name,domain_ip,belong,IDC
#檢查檔案
function file_check()
{
if [ -f $1 ]
rm -f $1
}
file_check $belong_file
file_check $domain_ip
file_check $report
file_check /tmp/IDC_ip_tmp.txt
file_check /tmp/IDC_ip_num.txt
function file_check1()
if [ ! -f $1 ]
echo "$1 file is not exist"
#指定域名清單檔案。格式:一行一個域名
#指定IP段檔案。格式:名字,起始IP,結束IP 空格隔開
#echo "============================================================="
#echo "域名清單檔案格式:一行一個域名 "
#echo "IP段檔案格式: 名字,起始IP,結束IP 空格隔開 "
#echo "Do it like this:"
#echo "sh ip_check.sh domain-list-file ip-list-file "
#read -p "準備好所需檔案,請選擇:繼續y,退出n:" stat
#case $stat in
#y|Y|yes|Yes|YES);;
#n|N|no|No|NO)exit;;
#*)
#echo "**************輸入錯誤,請重新執行腳本!!!*****************"
#exit;;
#esac
#file_check1 $domain_src_file
#file_check1 $IDC_file
file_check1 $domain_input
file_check1 $IDC_input
##準備檔案
yes|cp $domain_input $domain_src_file
yes|cp $IDC_input $IDC_file
#解析域名,判斷所屬,輸出檔案
#解析域名
function resolv()
domain_src=$1
while read line_name
do
/usr/bin/dig $line_name |egrep "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"|grep IN|awk '{print $5}' > /tmp/ip.tmp
while read line_ip
echo "$line_name,$line_ip" >> $domain_ip
done < /tmp/ip.tmp
done < $domain_src
#判斷所屬營運商
function belong()
rm -f /tmp/tmp
belong_file_tmp=$1
while read belong_line
donmain_ip=`echo $belong_line|awk -F "," '{print $2}'`
#belong_NET=`/usr/bin/elinks -dump "http://www.ip138.com/ips138.asp?ip=$donmain_ip&action=2"|iconv -f gb2312 -t utf8|grep "本站主資料"|awk -F ":" '{print $2}'`
/usr/bin/elinks -dump "http://www.ip138.com/ips138.asp?ip=$donmain_ip&action=2"|iconv -f gb2312 -t utf8|grep "本站主資料"|awk -F ":" '{print $2}' >> /tmp/tmp &
#echo $belong_line,$belong_NET >> $belong_file
done < $belong_file_tmp
paste -d "," $belong_file_tmp /tmp/tmp > $belong_file
#判斷所屬IDC
function IDC()
IDC_file_tmp=$1
touch /tmp/IDC_ip_num.txt
#生成ip to num的臨時
while read IDC_ip_line
min_tmp=`echo $IDC_ip_line|awk -F '{print $2}'`
max_tmp=`echo $IDC_ip_line|awk -F '{print $3}'`
ip_to_num $min_tmp
min=$ip_num
ip_to_num $max_tmp
max=$ip_num
echo "$IDC_ip_line,$min,$max" >> /tmp/IDC_ip_tmp.txt
sort -t "," -k 4 /tmp/IDC_ip_tmp.txt > /tmp/IDC_ip_num.txt
done < $2
#判斷ip
echo "域名,IP,營運商,IDC,内/外" >$report
while read IDC_line
domain_ip_tmp=`echo $IDC_line|awk -F "," '{print $2}'`
belongs_tmp=`echo $IDC_line|awk -F "," '{print $3}'`
ip_to_num $domain_ip_tmp
#echo $ip_num
IDC_name='無資料'
belongs='無資料'
###### 比較大小
while read num_line
IDC_name_tmp=`echo $num_line|awk -F "," '{print $1}'`
min_tmp=`echo $num_line|awk -F "," '{print $4}'`
max_tmp=`echo $num_line|awk -F "," '{print $5}'`
if [ $ip_num -ge $min_tmp ] && [ $ip_num -le $max_tmp ]
IDC_name=$IDC_name_tmp
done < /tmp/IDC_ip_num.txt
######判斷網内/網外,以在移動網内為内,外為外
echo $belongs_tmp|grep "移動" &> /dev/null
if [ $? -eq 0 ]
belongs="網内"
else
belongs="網外"
#輸出檔案
echo $IDC_line,$IDC_name,$belongs >> $report
#HEX_STRING=$(printf "0X%02X%02X%02X%02X\n" ${IP_ARRAY[0]} ${IP_ARRAY[1]} ${IP_ARRAY[2]} ${IP_ARRAY[3]})
#printf "%d\n" ${HEX_STRING}
done < $IDC_file_tmp
#ip to num
function ip_to_num()
ip_addr=$1
ip_list=${ip_addr//./ } #最後的空格很重要
read -a IP_ARRAY <<<${ip_list}
ip_num=$(( ${IP_ARRAY[0]}<<24 | ${IP_ARRAY[1]}<<16 | ${IP_ARRAY[2]}<<8 | ${IP_ARRAY[3]} ))
return $ip_num
#運作
echo "running......"
#檢查指令
yum install elinks bind-utils -y &> /dev/null
resolv $domain_src_file
file_check1 $domain_ip
belong $domain_ip
IDC $belong_file $IDC_file
/usr/bin/iconv -f utf8 -t gb2312 $report -o $report_end
echo "report is $report_end"