#!/bin/bash
#this script test UP to other host.
#Iplist=/opt/script_test/iplist.data
#将telnet成功的写入到telnet_alive.txt中,失败则写入telnet_die.txt中
#ip_list.dat中为“ip 端口”,动态ip及端口版
#PORT="9100"
IP_LIST="./ip_list.dat"
#PORT=$(head -n1 $IP_LIST|awk '{print $2}')
[ -f $IP_LIST ] || touch $IP_LIST
> telnet_alive.txt
> telnet_die.txt
count=0
while read i
do
((count++))
echo "count = $count"
i=$(echo $i|tr -s ' ')
PORT=$(echo $i |awk '{print $2}')
# 关键代码,1s自动结束telnet
# (sleep 1) | telnet $i $PORT >> telnet_result.txt
#timeout --signal=9 5 telnet $i > telnet_result.txt
timeout --signal=9 5 nc -vz $i >> telnet_result.txt 2>&1
done < $IP_LIST
# 根据结果判断出正常可以ping通的ip
#echo $(cat telnet_result.txt | grep -B 1 \] | grep [0-9] | awk '{print $3}' | cut -d '.' -f 1,2,3,4) $PORT >> telnet_alive.txt
cat telnet_result.txt | awk '{print $3,$4}' > telnet_alive.txt
# 差集,得到ping不同的ip
cat $IP_LIST telnet_alive.txt | sort | uniq -u > telnet_die.txt
rm -rf telnet_result.txt
exit 0