在 Ubuntu 中,telnet 命令本身不支持通配符
创建一个名为 telnet_wildcard.sh
的脚本文件,并输入以下内容:
#!/bin/bash
# 通配符匹配的 IP 地址或域名列表
targets=("192.168.1.%d" "example.com")
# 端口号
port=23
# 遍历目标列表并尝试连接
for target in "${targets[@]}"; do
# 如果是数字,直接使用;如果是域名,解析为 IP 地址
if [[ $target =~ ^[0-9]+$ ]]; then
ip=$target
else
ip=$(dig +short $target)
fi
# 尝试连接
echo "Trying to connect to $ip on port $port..."
telnet $ip $port
# 检查连接是否成功
if [ $? -eq 0 ]; then
echo "Connected to $ip successfully!"
# 在此处添加你需要执行的操作
break
else
echo "Failed to connect to $ip."
fi
done
给予脚本执行权限:
chmod +x telnet_wildcard.sh
运行脚本:
./telnet_wildcard.sh
如果你不想创建脚本文件,可以直接在终端中使用以下命令:
for i in {1..255}; do
targets=("192.168.1.$i" "example.com")
for target in "${targets[@]}"; do
echo "Trying to connect to $target on port 23..."
telnet $target 23
if [ $? -eq 0 ]; then
echo "Connected to $target successfully!"
# 在此处添加你需要执行的操作
exit 0
fi
done
done
这个命令会尝试连接到 192.168.1.1 到 192.168.1.255 和 example.com 的 23 端口。如果连接成功,它将显示成功消息并执行相应的操作。