使用步骤
- vim checkport.sh
- 输入i 进入编辑模式
- 粘贴
- 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
![](/upload/20241227/9bc7d2b91d5af0.png)