功能描述
- 利用 timeout 和 telnet对输入的网段进行特定端口的扫描
- 扫描过程应显示正在执行的命令
- 扫描过程应显示执行进度条,进度达到100%时的,进度条右侧应该达到终端最右侧
- 扫描到的可访问地址与端口应该记录到report.txt中
- 扫描过程完成后应该在屏幕上输出扫描结果,并提示扫描结束
代码
#!/bin/bash
# 如果提前结束程序,请使用tput cnorm命令恢复光标
# 接收用户输入前清除屏幕
clear && true >report.txt
width=$(($(tput cols) - 6))
# 接收用户输入的网段信息
read -rp "网段(默认值:192.168.1):" segment
# 网段信息未输入时的默认值设置
segment=${segment:-"192.168.1"}
# 接收用户输入的多端口信息
read -rp "需要扫描的端口(默认值:80 22 3306):" -a port
# 多端口信息信息未输入时的默认值设置
port=("${port[@]:-80}" "${port[@]:-22}" "${port[@]:-3306}")
# 设置光标不可见
tput civis
for ip in ${segment}.{1..254}; do
for a_port in "${port[@]}"; do
tput cup 2 0
# 特定位置输出检测信息
echo "检测:timeout -s 9 0.1 telnet ${ip} ${a_port} "
# 端口扫描
if timeout -s 9 0.1 telnet "${ip}" "${a_port}" | grep -q "Connected"; then
# 可访问端口信息写入文件report.txt
echo "结论: ${ip} 在 ${a_port} 号端口提供服务" >>report.txt
fi
tput cup 3 0
# 特定位置输出进度条
echo "进度:"
# 进度条计算
col=$(printf "%.0f" "$((width * ${ip##*.} / 254))")
tput cup 3 "$((col + 6))"
echo "#"
done
done
echo -e "扫描报告如下\n"
# 扫描日志结果输出
cat report.txt
echo -e "\n扫描结束"
# 设置光标可见
tput cnorm