欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

shell脚本批量测试TCP/UDP端口,通过telnet或者nc命令测试

程序员文章站 2024-02-26 16:01:46
...

tcp端口测试用telnet

#!/bin/bash

for line in `cat iplist.txt | grep -v ^# |grep -v ^$`
do  
    ip=`echo $line | awk 'BEGIN{FS="|"} {print $1}'`
    port=`echo $line | awk 'BEGIN{FS="|"} {print $2}'`
    echo "(sleep 1;) | telnet $ip $port"
    (sleep 1;) | telnet $ip $port > telnet_result.txt
    result=`cat telnet_result.txt | grep -B 1 \] | grep [0-9] | awk '{print $3}' | cut -d '.' -f 1,2,3,4`
    echo "$result"
    if [ -n "$result" ]; then
        echo "$result | $port" >> telnet_alive.txt
    else
        echo "*********************************************************************************************** $ip $port not pass"
     fi
done
cat iplist.txt telnet_alive.txt |sort|uniq -u| grep -v ^# > telnet_died.txt
echo "telnet is over"

udp端口测试用nc -vuz

#!/bin/bash

for line in `cat iplist.txt | grep -v ^# |grep -v ^$`
do  
    ip=`echo $line | awk 'BEGIN{FS="|"} {print $1}'`
    port=`echo $line | awk 'BEGIN{FS="|"} {print $2}'`
    echo "(sleep 1;) | nc -vuz $ip $port"
    (sleep 1;) | nc -vuz $ip $port > telnet_result.txt
done
echo "telnet is over"

这里的iplist.txt是在同级目录下面创建一个txt,内容如下,这里需要添加两个 |  ,因为windows的txt内的文字复制到linux环境下面,如果只用一个 | ,端口号获取有问题,用两个就没这问题。

10.0.0.1|8080|
192.168.1.1|8080|
10.0.0.0|6379|

 

相关标签: shell命令