4

批量检测端口连通性

 1 year ago
source link: https://blog.kelu.org/tech/2022/09/14/batch-telnet-ports.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

批量检测端口连通性

2022-09-14     tech linux shell
linux.jpg

最简单的办法当然还是使用ansible。

不过客户的一些生产环境没有使用ansible,就写了个简单的脚本测试了下。

  1. 开头取了eth0这个网卡的IP地址。
  2. 在同一个目录下保存 ip_info这个文件,每行填写ip:port
#!/bin/bash

LOCALIP=`ifconfig -a | grep eth0 -A 2|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"` 

check_telnet(){
for ip_port in $(cat ./ip_info|grep -v '^#')
do
    CHECK_PORT=$(echo $ip_port|awk -F: '{print $2}')
    CHECK_IP=$(echo $ip_port|awk -F: '{print $1}')
    echo -e "\n"| telnet $CHECK_IP $CHECK_PORT |grep "Connected to\|Escape character" > /dev/null
    if [ $? -eq 0 ];then
        echo -e "$CHECK_IP\t $CHECK_PORT\t ok"
    else
        echo -e "$CHECK_IP\t $CHECK_PORT\t error!!!"
    fi
done
}

check_telnet > result.log

echo "=========$LOCALIP============="
cat result.log

如果本文对你有帮助,欢迎投食

alipay.gif
qpay.gif
圣杯の寄るべに従い。この意、この理に従うならば応えよ!

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK