一个通用的端口检测脚本

2024-12-27 40℃

使用步骤

  1. vim checkport.sh
  2. 输入i 进入编辑模式
  3. 粘贴
  4. ecs+: 输入wq! 保存退出
#!/bin/bash

# 选择需要使用的命令
read -p "Choose the command(telnetcat/nc/telnet): " cmd

# 判断命令是否存在
if ! command -v $cmd &> /dev/null
then
    echo "$cmd command not found"
    exit 1
fi

# 最大的端口号
read -p "Enter the max port number: " max

# max最大值是65535
if [ $max -gt 65535 ]
then
    echo "max port number is 65535"
    exit 1
fi

# 输入IP地址
read -p "Enter the IP Address: " ip

# 检查ip地址是否合法
if ! echo $ip | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" &> /dev/null
then
    echo "IP Address is not valid"
    exit 1
fi

if [ $cmd = "telnetcat" ]
then
    for i in {1..$max}
    do
        telnetcat $ip $i > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
            echo "Port $i is open"
        fi
    done
elif [ $cmd = "nc" ]
then
    for i in {1..$max}
    do
        nc -z $ip $i > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
            echo "Port $i is open"
        fi
    done
elif [ $cmd = "telnet" ]
then
    for i in {1..$max}
    do
        telnet $ip $i > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
            echo "Port $i is open"
        fi
    done
else
    echo "Invalid command"
    exit 1
fi

标签: 命令

非特殊说明,本博所有文章均为博主原创。