批量telnet,动态ip及端口版

#!/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
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wlcill

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值